Raxkin
Raxkin

Reputation: 397

Insert with cascade Hibernate

I have 2 classes with a relation of @OneToMany.

The class Player:

I have a List of items and I mapped like this:

@OneToMany(mappedBy="player", cascade = CascadeType.ALL)
private List<InventoryItem> inventory;

And in my class InventoryItem:

@ManyToOne
@JoinColumn(name="id_player")
private Player player;

This is my InventoryItem table:

enter image description here

When i save a Player, i also want to save the InventoryItem i had inserted in the inventory List of the player, but it create the item in the table and set the id_player to Null

Upvotes: 0

Views: 100

Answers (1)

Veselin Davidov
Veselin Davidov

Reputation: 7081

The inventory item has a Player object member which is mapped by id_player and it is null so the id_player in the column is null. You need to set the Player= yourPlayer in the inventory object and it will fill the column in the database.

Upvotes: 1

Related Questions