我有一个类学生和类分数之间的一对一hibernate映射:
@Entity
@Table(name = "Users")
public class Student implements IUser {
@Id
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "password")
private String password;
@OneToOne(fetch = FetchType.EAGER, mappedBy = "student")
private Points points;
@Column(name = "type")
private int type = getType();
//gets and sets...
@Entity
@Table(name = "Points")
public class Points {
@GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "student"))
@Id
@GeneratedValue(generator = "generator")
@Column(name = "id", unique = true, nullable = false)
private int Id;
@OneToOne
@PrimaryKeyJoinColumn
private Student student;
//gets and sets
然后我执行以下操作:
Student student = new Student();
student.setId(1);
student.setName("Andrew");
student.setPassword("Password");
Points points = new Points();
points.setPoints(0.99);
student.setPoints(points);
points.setStudent(student);
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(student);
session.getTransaction().commit();
Hibernate会将学生信息保存在表中,但不会保存相应的分数。这样可以吗?我应该单独保存分数吗?