简体   繁体   English

SpringBoot ManyToOne 保存到数据库问题 - 出现错误:字段'user_id'没有默认值

[英]SpringBoot ManyToOne saving to database issue - getting error: Field 'user_id' doesn't have a default value

My situation is this: I am creating a marketplace where users can buy and sell products.我的情况是这样的:我正在创建一个用户可以买卖产品的市场。 I am trying to list a product with a ManyToOne relationship with the logged in user.我正在尝试列出与登录用户具有 ManyToOne 关系的产品。 I am getting the error: Field 'user_id' doesn't have a default value.我收到错误消息:字段“user_id”没有默认值。 Im guessing this is because I haven't set the user_id but I'm not sure how to.我猜这是因为我没有设置 user_id 但我不确定如何设置。

Here is the code:这是代码:

@Entity
@Table(name = "msItem")
public class Item {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long itemId;

    @Column(nullable = false, length = 45)
    private String itemName;

    @Column(nullable = false)
    private int itemPrice;

    @Column(nullable = false, length = 100)
    private String itemDesc;

    @Column(nullable = false, length = 100)
    private String category;

    @Column(nullable = false, length = 100)
    private String image;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false, insertable=false, updatable=false)
    private User user;

    public long getItemId() {
        return itemId;
    }

    public void setItemId(long itemId) {
        this.itemId = itemId;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public int getItemPrice() {
        return itemPrice;
    }

    public void setItemPrice(int itemPrice) {
        this.itemPrice = itemPrice;
    }

    public String getItemDesc() {
        return itemDesc;
    }

    public void setItemDesc(String itemDesc) {
        this.itemDesc = itemDesc;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public User user() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}







@Service
public class ItemServiceImp{

    @Autowired
    private ItemRepository itemRepository;

    public List<Item> listItems(User user) {
         return itemRepository.findByUser(user);
    }
}







@Repository
public interface ItemRepository extends JpaRepository<Item, Long> {

    public List<Item> findByUser(User user);
}







@Controller
public class ProductController {

    @Autowired
    private ItemRepository itemRepository;





    @GetMapping("/listItem")
    public String listing(Model model) {

        model.addAttribute("item", new Item());
        return "addItem";


    }

    @PostMapping("/process_Item")
    public String itemAdd(Item item)  {

        itemRepository.save(item);
        return "home_page";


    }

} }

Error is happening at line 27 of controller class错误发生在 controller 的第 27 行 class

Please help.. Thank you in advance!请帮助..提前谢谢你!

There are a few ways to get a User connected to an Item.有几种方法可以让用户连接到项目。 Since you are using the current logged in user, one way is to retrieve that user's User object information from the database and then attaching it to the Item you plan to save right before calling: itemRepository.save(item) .由于您使用的是当前登录的用户,一种方法是从数据库中检索该用户的用户 object 信息,然后将其附加到您计划在调用之前保存的项目: itemRepository.save(item)

As I don't know how you store session information I cannot give an exact way but as an example if you are using Spring Security you could use the following:因为我不知道你是如何存储 session 信息的,所以我无法给出确切的方法,但作为一个例子,如果你使用的是 Spring Security,你可以使用以下方法:

@PostMapping("/process_Item")
public String itemAdd(Item item, Principal principal)  {
    User user = userRepository.findByUsername(principal.getName());

    item.setUser(user);
    itemRepository.save(item);
    return "home_page";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM