Reputation: 13
guys ! I have problem with saving on the "child" table parent's id When persist the child object, the column with parent id is null..
Parent ( User class)
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private long userId;
@Column(name = "name")
private String name;
@Column(name = "address")
private String address;
@Column(name = "postcode")
private int postCode;
Child (Credential class)
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="credential")
public class Credentials {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="username")
private String username;
@Column(name="password")
private String password;
@OneToOne(cascade= CascadeType.ALL )
@JoinColumn(name="user_id")
public User user;
Application class
User user = new User();
user.setAddress("asdkapoda");
user.setName("xaxaxaxa");
user.setPostCode(3423);
Credentials credit = new Credentials();
credit.setPassword("sadsad");
credit.setUsername("sdadsa");
session.save(credit);
And in database Credential is with user_id = NULL. Hibernate 4.3
Upvotes: 0
Views: 2304
Reputation: 5087
Hibernate is faithfully saving the fact that you, in fact, never associated the Credentials
object with a User
object. Try adding credit.setUser(user);
before the save.
Upvotes: 1