简体   繁体   English

嵌套n:m + JPA属性

[英]Nested n:m + attributes JPA

I have a strange need in a project. 我在一个项目中有一个奇怪的需求。 Joining two n:m+attributes table (I will present the behavior with dummy attributes). 连接两个n:m + attributes表(我将使用虚拟属性介绍行为)。

  1. FirstTable (idPlace, idAddress,idSchool, wage) joined 1:m; FirstTable(idPlace,idAddress,idSchool,工资)加入了1:m;
  2. SecondTable (idPlace, idAddress,idSchool, qty, idEnterprise) SecondTable(idPlace,idAddress,idSchool,qty,idEnterprise)

EDIT (example schema): 编辑(示例架构): 在此处输入图片说明

Of course that I have the tables Place, Address, School, Enterprise with theirs respective Ids, gets, sets and attributes implemented in the entity classes. 当然,我有表Place,Address,School,Enterprise,以及在实体类中实现的各自的ID,获取,集合和属性。

CODE: 码:

Place 地点

@Entity
@Table(name = "Place")
public class Place implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idLine")
private Long idLine;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pk.place")
private List<FirstTable> firstTables;
}

Address 地址

@Entity
@Table(name = "Address")
public class Address implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idAddress")
private Long idAddress;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pk.address")
private List<FirstTable> firstTables;
}

School 学校

@Entity
@Table(name = "School")
public class School implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idSchool")
private Long idSchool;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pk.school")
private List<FirstTable> firstTables;
}

FirstTable 第一表

@Entity
@Table(name = "FirstTable")
@AssociationOverrides({ @AssociationOverride(name = "pk.school", joinColumns = @JoinColumn(name = "idSchool")),
    @AssociationOverride(name = "pk.address", joinColumns = @JoinColumn(name = "idAddress")),
    @AssociationOverride(name = "pk.place", joinColumns = @JoinColumn(name = "idPlace")) })
public class FirstTable implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@EmbeddedId
protected FirstTablePK pk = new FirstTablePK();
 }

FirstTablePK 第一表PK

@Embeddable
public class FirstTablePK implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;
@ManyToOne
private Address address;
@ManyToOne 
private Place place;
@ManyToOne 
private School school;
}

The above mentioned tables and joins are working perfectly. 上面提到的表和联接工作正常。 Now I want to join the FirstTable with the Second Table. 现在,我想将FirstTable与Second Table连接起来。

Enterprise 企业

@Entity
@Table(name = "Enterprise")
public class Enterprise implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idEnterprise")
private Long idEnterprise;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pk.enterprise") private List secondTables; @OneToMany(获取= FetchType.LAZY,级联= CascadeType.ALL,mappedBy =“ pk.enterprise”)私有列表secondTables; } }

Now for the SecondTable I've followed the same logic to connect to the Enterprise. 现在,对于SecondTable,我遵循相同的逻辑来连接到Enterprise。 For connecting with the FirstTable I've tried this: 为了与FirstTable连接,我尝试了以下方法:

@Entity
@Table(name = "SecondTable")
@AssociationOverrides({
        @AssociationOverride(name = "pk.firstTable", joinTable = @JoinTable(
                name = "FirstTable", inverseJoinColumns = {
                @JoinColumn(name = "idSchool", referencedColumnName = "idSchool"),
                @JoinColumn(name = "idAddress", referencedColumnName = "idAddress"),
                @JoinColumn(name = "idPlace", referencedColumnName = "idPlace") })),
        @AssociationOverride(name = "pk.enterprise", joinColumns = @JoinColumn(name = "idEnterprise")) })
public class SecondTable implements Serializable{}

Something is not working in my annotation, I'm trying to do an inverseJoin to the FirstTable table. 我的注释中无法正常工作,我正在尝试对FirstTable表进行inverseJoin。 The compilation shows this error: 编译显示此错误:

"org.hibernate.AnnotationException: A component cannot hold properties split into 2 different tables"

I've tried to provide a MV example. 我试图提供一个MV示例。 Thanks in advance and I really need your help. 在此先感谢您,我真的需要您的帮助。

Hours later and many tries before I've managed to solve the problem. 数小时后,在我设法解决问题之前进行了许多尝试。 Actually the solution was much simpler that I was thinking initially. 实际上,解决方案比我最初考虑的要简单得多。

Here it is: 这里是:

 @AssociationOverride(name = "pk.firstTable", joinColumns = {
            @JoinColumn(name = "idSchool"),
            @JoinColumn(name = "idAddress"),
            @JoinColumn(name = "idPlace") }),
    @AssociationOverride(name = "pk.enterprise", joinColumns = @JoinColumn(name = "idEnterprise")) })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM