我有以下ER:
我想知道如何使用hibernate映射第三个表。 我有这个代码:
@ManyToOne(optional = false) @JoinColumns({ @JoinColumn(name = "tbl3_tbl1_id", referencedColumnName = "tbl2_tbl1_id", insertable = false, updatable = false), @JoinColumn(name = "tbl3_tbl2_id", referencedColumnName = "tbl2_id", insertable = false, updatable = false)}) public TableDB getTableDB() { return tableDB; } @OneToOne(optional = false) @JoinColumns({ @JoinColumn(name = "tbl3_tbl1_id", referencedColumnName = "tbl2_tbl1_id", insertable = false, updatable = false), @JoinColumn(name = "tbl3_tbl2_id_2", referencedColumnName = "tbl2_id", insertable = false, updatable = false)}) public TableDB getTableDB2() { return tableDB2; } @OneToOne(optional = false) @JoinColumns({ @JoinColumn(name = "tbl3_tbl1_id", referencedColumnName = "tbl2_tbl1_id", insertable = false, updatable = false), @JoinColumn(name = "tbl3_tbl2_id_3", referencedColumnName = "tbl2_id", insertable = false, updatable = false)}) public TableDB getTableDB3() { return tableDB3; }问题在于一对一2和3.当我调用方法“保存”时,插入代码是这样的:
insert into table3 (tbl3_desc, tbl3_id, tbl3_tbl1_id, tbl3_tbl2_id) values (?, ?, ?, ?)列tbl3_tbl2_id_2, tbl3_tbl2_id_3在哪里?
有什么意见或建议吗?
I have the following ER:
I want to know how I can map the third table using hibernate. I have this code:
@ManyToOne(optional = false) @JoinColumns({ @JoinColumn(name = "tbl3_tbl1_id", referencedColumnName = "tbl2_tbl1_id", insertable = false, updatable = false), @JoinColumn(name = "tbl3_tbl2_id", referencedColumnName = "tbl2_id", insertable = false, updatable = false)}) public TableDB getTableDB() { return tableDB; } @OneToOne(optional = false) @JoinColumns({ @JoinColumn(name = "tbl3_tbl1_id", referencedColumnName = "tbl2_tbl1_id", insertable = false, updatable = false), @JoinColumn(name = "tbl3_tbl2_id_2", referencedColumnName = "tbl2_id", insertable = false, updatable = false)}) public TableDB getTableDB2() { return tableDB2; } @OneToOne(optional = false) @JoinColumns({ @JoinColumn(name = "tbl3_tbl1_id", referencedColumnName = "tbl2_tbl1_id", insertable = false, updatable = false), @JoinColumn(name = "tbl3_tbl2_id_3", referencedColumnName = "tbl2_id", insertable = false, updatable = false)}) public TableDB getTableDB3() { return tableDB3; }The problem lies in the one to one 2 and 3. When I call the method "save" the insertion code is something like this:
insert into table3 (tbl3_desc, tbl3_id, tbl3_tbl1_id, tbl3_tbl2_id) values (?, ?, ?, ?)Where is the column tbl3_tbl2_id_2, tbl3_tbl2_id_3 ?
Any comments or advice?
最满意答案
如果我们采取具体的例子如下:
table1 - > COMPANIES :
import java.io.Serializable; import java.util.List; import javax.persistence.Basic; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @Entity @Table(name = "COMPANIES") public class Company implements Serializable { private static final long serialVersionUID = 1L; @Id @Basic(optional = false) @NotNull @Size(min = 1, max = 4) @Column(name = "COMP_KEY") private String key; @Basic(optional = false) @NotNull @Size(min = 1, max = 100) @Column(name = "COMP_DESCRIPTION") private String description; @OneToMany(cascade = CascadeType.ALL, mappedBy = "company", fetch = FetchType.LAZY) private List<Branch> branches; //GETTERS, SETTERS... }table2 - > BRANCHES地图
import java.io.Serializable; import java.util.List; import javax.persistence.Basic; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @Entity @Table(name = "BRANCHES") public class Branch implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId protected BranchPK branchPK; @Basic(optional = false) @NotNull @Size(min = 1, max = 100) @Column(name = "BRAN_DESCRIPTION") private String description; @JoinColumn(name = "BRAN_COMP_KEY", referencedColumnName = "COMP_KEY") @ManyToOne(optional = false, fetch = FetchType.LAZY) private Company company; @OneToMany(cascade = CascadeType.ALL, mappedBy = "branch", fetch = FetchType.LAZY) private List<Employee> staff; //GETTERS, SETTERS, ETC } import java.io.Serializable; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Embeddable; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @Embeddable public class BranchPK implements Serializable { @Basic(optional = false) @NotNull @Size(min = 1, max = 4) @Column(name = "BRAN_COMP_KEY") private String companyKey; @Basic(optional = false) @NotNull @Size(min = 1, max = 4) @Column(name = "BRAN_KEY") private String brachKey; //GETTERS, SETTERS, ETC }table3 - > STAFF地图
import java.io.Serializable; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.JoinColumns; import javax.persistence.ManyToOne; import javax.persistence.OneToOne; import javax.persistence.Table; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @Entity @Table(name = "STAFF") public class Employee implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId protected EmployeePK employeePK; @Basic(optional = false) @NotNull @Size(min = 1, max = 15) @Column(name = "EMPL_NAME") private String name; @JoinColumns({ @JoinColumn(name = "EMPL_COMP_KEY", referencedColumnName = "BRAN_COMP_KEY", insertable = false, updatable = false), @JoinColumn(name = "EMPL_BRAN_KEY", referencedColumnName = "BRAN_KEY", insertable = false, updatable = false)}) @ManyToOne(optional = false, fetch = FetchType.EAGER) private Branch branch; @JoinColumns({ @JoinColumn(name = "EMPL_COMP_KEY", referencedColumnName = "BRAN_COMP_KEY", insertable = false, updatable = false), @JoinColumn(name = "EMPL_BRAN_SUPER", referencedColumnName = "BRAN_KEY", insertable = false, updatable = false)}) @OneToOne(optional = false) private Branch branchSupervice; @JoinColumns({ @JoinColumn(name = "EMPL_COMP_KEY", referencedColumnName = "BRAN_COMP_KEY", insertable = false, updatable = false), @JoinColumn(name = "EMPL_BRAN_VISIT", referencedColumnName = "BRAN_KEY", insertable = false, updatable = false)}) @OneToOne private Branch branchVisitor; //GETTERS, SETTERS, ETC } import java.io.Serializable; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Embeddable; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @Embeddable public class EmployeePK implements Serializable { @Basic(optional = false) @NotNull @Size(min = 1, max = 4) @Column(name = "EMPL_COMP_KEY") private String companyKey; @Basic(optional = false) @NotNull @Size(min = 1, max = 4) @Column(name = "EMPL_BRAN_KEY") private String branchKey; @Basic(optional = false) @NotNull @Size(min = 1, max = 4) @Column(name = "EMPL_KEY") private String employeeKey; //GETTERS, SETTERS, ETC }对于这种情况,我有一个“家庭补救”解决方案。 我为每个关联对象的主键创建了2个额外字段。
@Entity @Table(name = "STAFF") public class Employee implements Serializable { private static final long serialVersionUID = 1L; //Besides the above mentioned code @Basic(optional = false) @NotNull @Size(min = 1, max = 4) @Column(name = "EMPL_BRAN_SUPER") private String keyBranchSupervice; @Size(max = 4) @Column(name = "EMPL_BRAN_VISIT") private String keyBranchVisitor; public void setBranchSupervice(Branch branchSupervice) { this.keyBranchSupervice = branchSupervice.branchPK.getBrachKey(); this.branchSupervice = branchSupervice; } }并且在对象的setBranchSupervice为同一个FK中的每个字段指定,我等于值。 有了这个,我可以顺利添加,编辑,删除。
但我仍然怀疑应该是(良好做法)。
If we take a concrete example would be as follows:
Map for table1--> COMPANIES:
import java.io.Serializable; import java.util.List; import javax.persistence.Basic; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @Entity @Table(name = "COMPANIES") public class Company implements Serializable { private static final long serialVersionUID = 1L; @Id @Basic(optional = false) @NotNull @Size(min = 1, max = 4) @Column(name = "COMP_KEY") private String key; @Basic(optional = false) @NotNull @Size(min = 1, max = 100) @Column(name = "COMP_DESCRIPTION") private String description; @OneToMany(cascade = CascadeType.ALL, mappedBy = "company", fetch = FetchType.LAZY) private List<Branch> branches; //GETTERS, SETTERS... }Map for table2-->BRANCHES
import java.io.Serializable; import java.util.List; import javax.persistence.Basic; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @Entity @Table(name = "BRANCHES") public class Branch implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId protected BranchPK branchPK; @Basic(optional = false) @NotNull @Size(min = 1, max = 100) @Column(name = "BRAN_DESCRIPTION") private String description; @JoinColumn(name = "BRAN_COMP_KEY", referencedColumnName = "COMP_KEY") @ManyToOne(optional = false, fetch = FetchType.LAZY) private Company company; @OneToMany(cascade = CascadeType.ALL, mappedBy = "branch", fetch = FetchType.LAZY) private List<Employee> staff; //GETTERS, SETTERS, ETC } import java.io.Serializable; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Embeddable; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @Embeddable public class BranchPK implements Serializable { @Basic(optional = false) @NotNull @Size(min = 1, max = 4) @Column(name = "BRAN_COMP_KEY") private String companyKey; @Basic(optional = false) @NotNull @Size(min = 1, max = 4) @Column(name = "BRAN_KEY") private String brachKey; //GETTERS, SETTERS, ETC }Map for table3-->STAFF
import java.io.Serializable; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.JoinColumns; import javax.persistence.ManyToOne; import javax.persistence.OneToOne; import javax.persistence.Table; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @Entity @Table(name = "STAFF") public class Employee implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId protected EmployeePK employeePK; @Basic(optional = false) @NotNull @Size(min = 1, max = 15) @Column(name = "EMPL_NAME") private String name; @JoinColumns({ @JoinColumn(name = "EMPL_COMP_KEY", referencedColumnName = "BRAN_COMP_KEY", insertable = false, updatable = false), @JoinColumn(name = "EMPL_BRAN_KEY", referencedColumnName = "BRAN_KEY", insertable = false, updatable = false)}) @ManyToOne(optional = false, fetch = FetchType.EAGER) private Branch branch; @JoinColumns({ @JoinColumn(name = "EMPL_COMP_KEY", referencedColumnName = "BRAN_COMP_KEY", insertable = false, updatable = false), @JoinColumn(name = "EMPL_BRAN_SUPER", referencedColumnName = "BRAN_KEY", insertable = false, updatable = false)}) @OneToOne(optional = false) private Branch branchSupervice; @JoinColumns({ @JoinColumn(name = "EMPL_COMP_KEY", referencedColumnName = "BRAN_COMP_KEY", insertable = false, updatable = false), @JoinColumn(name = "EMPL_BRAN_VISIT", referencedColumnName = "BRAN_KEY", insertable = false, updatable = false)}) @OneToOne private Branch branchVisitor; //GETTERS, SETTERS, ETC } import java.io.Serializable; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Embeddable; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @Embeddable public class EmployeePK implements Serializable { @Basic(optional = false) @NotNull @Size(min = 1, max = 4) @Column(name = "EMPL_COMP_KEY") private String companyKey; @Basic(optional = false) @NotNull @Size(min = 1, max = 4) @Column(name = "EMPL_BRAN_KEY") private String branchKey; @Basic(optional = false) @NotNull @Size(min = 1, max = 4) @Column(name = "EMPL_KEY") private String employeeKey; //GETTERS, SETTERS, ETC }I had a "home remedy" solution for this case. I created 2 extra fields to the primary key of each associated object.
@Entity @Table(name = "STAFF") public class Employee implements Serializable { private static final long serialVersionUID = 1L; //Besides the above mentioned code @Basic(optional = false) @NotNull @Size(min = 1, max = 4) @Column(name = "EMPL_BRAN_SUPER") private String keyBranchSupervice; @Size(max = 4) @Column(name = "EMPL_BRAN_VISIT") private String keyBranchVisitor; public void setBranchSupervice(Branch branchSupervice) { this.keyBranchSupervice = branchSupervice.branchPK.getBrachKey(); this.branchSupervice = branchSupervice; } }And specified for each field in the same FK on the setBranchSupervice of the objects, I equaled values. With this I can add, edit, delete smoothly.
But I still have the doubt as it should be (good practice).
更多推荐
发布评论