Skip to content
Advertisement

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. I am getting the error: Field ‘user_id’ doesn’t have a default value. Im guessing this is because I haven’t set the user_id but I’m not sure how to.

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

Please help.. Thank you in advance!

Advertisement

Answer

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).

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:

@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";
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement