3

クラス Student とクラス Points の間に 1 対 1 の休止状態のマッピングがあります。

@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();

また、休止状態は学生をテーブルに保存しますが、対応するポイントは保存しません。大丈夫ですか?ポイントは別に貯めるべき?

4

3 に答える 3

2
@OneToOne(fetch = FetchType.EAGER, mappedBy = "student", cascade=CascadeType.PERSIST)
private Points points;

これを試すことができますか。

于 2012-12-08T16:53:01.757 に答える
2

デフォルトでは、Hibernate でカスケードされる操作はありません。1 対 1 の関係では CascadeType を指定する必要があります。

于 2012-12-08T16:53:49.670 に答える
1

Studentと を保存したい場合は、注釈でPointsを使用する必要があります。ドキュメントCascadeTypeについては、こちらを参照してください。

于 2012-12-08T16:55:38.233 に答える