我在一个项目中有一个奇怪的需求。连接两个n:m + attributes表(我将使用虚拟属性来呈现行为)。


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


编辑(示例架构):
java - 嵌套n:m + JPA属性-LMLPHP

当然,我有表Place,Address,School,Enterprise,以及在实体类中实现的各自的ID,获取,集合和属性。

码:

地点

@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;
}


地址

@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;
}


学校

@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;
}


第一表

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


第一表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;
}


上面提到的表和联接工作正常。现在,我想将FirstTable与Second Table连接起来。

企业

@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(提取= FetchType.LAZY,级联= CascadeType.ALL,mappedBy =“ pk.enterprise”)
   私有列表secondTables;
   }

现在,对于SecondTable,我遵循相同的逻辑来连接到Enterprise。为了与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{}


我的注释中无法正常工作,我正在尝试对FirstTable表进行inverseJoin。编译显示此错误:

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


我试图提供一个MV示例。
在此先感谢您,我真的需要您的帮助。

最佳答案

数小时后,在我设法解决问题之前进行了许多尝试。实际上,解决方案比我最初考虑的要简单得多。

这里是:

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

关于java - 嵌套n:m + JPA属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40975200/

10-09 04:12