I have two simple domain objects as follows:
USER:
@Entity
@Table(name="USER")
@IdClass(UserPK.class)
public class User implements Serializable {
//...
@Id
@Column(name = "FISCALCODE")
private String fiscalCode;
@Id
@Column(name = "USERNUMBER")
private String userNumber;
@OneToMany(mappedBy="user", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Items> items;
// getters and setters
}
UserPK:
public class UserPK implements Serializable {
@Column(name = "FISCALCODE")
private String fiscalCode;
@Column(name = "USERNUMBER")
private String userNumber;
// getter and setter
}
ITEMS:
@Entity
@Table(name="ITEMS")
public class Items implements Serializable {
//...
@Id
@Column(name = "ID_ITEM")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_item_generator")
@SequenceGenerator( name = "id_item_generator", sequenceName = "ITEM_SEQ", allocationSize = 1)
private Integer id;
@ManyToOne
@JoinColumns({
@JoinColumn(name="FISCALCODE"),
@JoinColumn(name="USERNUMBER")
})
private User user;
// getters and setters
}
DB Table:
user { fiscalcode, usernumber, other columns... } // fiscalcode+usernumber = PK
items { id, fiscalcode, usernumber, other columns... } // fiscalcode,usernumber is a foreign key
CONTROLLER:
@RequestMapping(value="/user", method = RequestMethod.POST, produces = "application/json")
public Object postUser(@RequestBody(required = false) User user){
//connection etc..
session.save(user);
//...
return new ResponseEntity<>(HttpStatus.OK);
}
Why when I run the command session.save(user)
Hibernate insert value null in the columns FISCALCODE
and USERNUMBER
of the ITEMS
table?
I tried to set the ManyToOne
and the JoinColumns
on the getter, but the result is the same.
EDIT: i have added my method for POST operation
@JoinColumns
:When the
JoinColumns
annotation is used, both thename
and thereferencedColumnName
elements must be specified in each suchJoinColumn
annotation.
So, you should correct your mapping like below:
@ManyToOne
@JoinColumns({
@JoinColumn(name="FISCALCODE", referencedColumnName = "FISCALCODE"),
@JoinColumn(name="USERNUMBER", referencedColumnName = "USERNUMBER")
})
private User user;
So, you should have in your User
entity the addItem()
and removeItem()
utility methods that synchronize both ends whenever a child element is added or removed like below.
@Entity
@Table(name="USER")
@IdClass(UserPK.class)
public class User implements Serializable {
//...
@OneToMany(mappedBy="user", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Items> items;
// getters and setters
public void addItem(Items item) {
this.items.add(item);
item.setUser(this);
}
public void removeItem(Items item) {
this.items.remove(item);
item.setUser(null);
}
}
Example of saving:
User user = new User();
user.setFiscalCode("Code1");
user.setUserNumber("User1");
// ...
Items item1 = new Items();
Items item2 = new Items();
// ...
user.addItem(item1);
user.addItem(item2);
session.save(user);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.