家长(用户类)
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;
儿童(证件类)
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;
申请类
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);
在数据库中,Credential使用user_id = NULL 。 Hibernate 4.3
答案 0 :(得分:1)
Hibernate忠实地保存了这样一个事实:事实上,你从未将Credentials
对象与User
对象相关联。尝试在保存之前添加credit.setUser(user);
。