使用现有记录的外键创建hibernate实体

问题描述 投票:-1回答:1

我正在使用hibernate 4.3.8。在Java Spring Web应用程序和postgres db中。

我有记录A和两个外键来记录B和C.

记录B已存在于系统中。记录C是新的,将在保存记录A时添加。记录布局就是这样。

A.primaryKey
A.foreignKey to B.primaryKey (already exists in system)
A.foreignKey to C.primaryKey (new record)
A.feildX

如何保存记录A?

谢谢你的帮助

这是实体类。我是hibernate的新手,它解释了错误。

@Entity
@Table(name="atable")
public class Aclass {

    @Id
    @Column(name="arec_id")
    private String id;

    //Do not create a reference to the bclass in this object. however do write the bclass object with a foreign key reference back to this aclass object
?   @Transient
?    @OneToOne(cascade=?)
?    @JoinTable(name="btable",inverseJoinColumns=@JoinColumn(name="brec_id"))
?   private Bclass bclass;

    //Create a reference to the cclass object in this record and write the cclass object as well
    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="crec_foreign_key",referencedColumnName="crec_id")
    private Cclass cclass;

    @Column(name="description")
    private String description;

    private VoiceEngineModel voiceEngineModel;

}

@Entity
@Table(name="btable")
public class Bclass {

    @Id
    @Column(name="brec_id")
    private String id;

    @Column(name="aref")
    private String aref;

    @Column(name="description")
    private String description;


}

@Entity
@Table(name="ctable")
public class Cclass {

    @Id
    @Column(name="crec_id")
    private String id;

    @Column(name="description")
    private String description;


}
java spring postgresql hibernate
1个回答
0
投票

我找到了解决方案。很容易。 Bclass的原始代码是错误的。抱歉。

  1. 删除对A类中Class的任何引用
  2. 对于Bclass修改列引用 @Entity @Table(name =“btable”)public class Bclass { @Id @Column(name =“brec_id”)private String id; @ManyToOne @JoinColumn(name =“aref”)private Aclass aref; @Column(name =“description”)私有字符串描述; }
© www.soinside.com 2019 - 2024. All rights reserved.