我正在尝试使用 JPA2 映射以下数据库结构:
ASSET
ID: assetId
...
PARTY
ID: partyId
...
PARTYASSET
ID: partyId
ID: assetId
ID: relationshipType
PARTYASSET 的主键是 PARTY 和 ASSET 的外键和一个附加列的组合:relationshipType。
由于列关系类型,我不能使用@ManyToMany 注释我必须使用@ManyToOne 和@OneToMany,如以下链接所述: http ://en.wikibooks.org/wiki/Java_Persistence/ManyToMany
我尝试了以下指南,但没有帮助: http ://www.kawoolutions.com/Technology/JPA,_Hibernate,_and_Co./JPA_Composite_Key_Variants#JPA_2.0_@IdClass
请你帮助我好吗?
我尝试了以下方法:(继承用于其他目的)
党课
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("PARTY")
public class Party implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long partyId;
@OneToMany(mappedBy = "party")
private Set<PartyAsset> partyAsset;
...
}
资产类别
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("ASSET")
public class Asset implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long assetid;
@OneToMany(mappedBy = "asset")
private Set<PartyAsset> partyAsset;
...
}
使用@IdClass:
派对资产类
@Entity
@IdClass(PartyAssetPK.class)
public class PartyAsset implements Serializable {
private static final long serialVersionUID = 1L;
public static enum RelationshipType {
OWNER, TENANT, SECONDARY_CONTACT, SUPPLIER
}
@Id
@Enumerated(EnumType.STRING)
private RelationshipType relationshipType;
@Id
@ManyToOne
@JoinColumn(name="partyId")
private Party party;
@Id
@ManyToOne
@JoinColumn(name="assetId")
private Asset asset;
...
}
PartyAssetPK 类
public class PartyAssetPK implements Serializable {
private static final long serialVersionUID = 1L;
private Long partyId;
private Long assetId;
private PartyAsset.RelationshipType relationshipType;
}
上面的代码抛出异常:
在实体 PartyAsset 中找不到 @IdClass 的属性:assetId
@IdClass 的替代品
派对资产类
@Entity
@IdClass(PartyAssetPK.class)
public class PartyAsset implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long partyId;
@Id
private Long assetId;
public static enum RelationshipType {
OWNER, TENANT, SECONDARY_CONTACT, SUPPLIER
}
@Id
@Enumerated(EnumType.STRING)
private RelationshipType relationshipType;
@ManyToOne
@JoinColumn(name="partyId")
@MapsId("partyId")
private Party party;
@ManyToOne
@JoinColumn(name="assetId")
@MapsId("assetId")
private Asset asset;
}
这会引发异常:
未找到超类型
使用@EmbeddedId
派对资产类
@Entity
public class PartyAsset implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
public PartyAssetPK id;
public static enum RelationshipType {
OWNER, TENANT, SECONDARY_CONTACT, SUPPLIER
}
@MapsId("relationshipType")
@Enumerated(EnumType.STRING)
private RelationshipType relationshipType;
@ManyToOne
@JoinColumn(name="partyId")
@MapsId("partyId")
private Party party;
@ManyToOne
@JoinColumn(name="assetId")
@MapsId("assetId")
private Asset asset;
}
PartyAssetPK 类
@Embeddable
public class PartyAssetPK implements Serializable {
private static final long serialVersionUID = 1L;
private Long partyId;
private Long assetId;
private PartyAsset.RelationshipType relationshipType;
}
这会引发异常;
原因:org.springframework.beans.factory.BeanCreationException:创建 com.ardan1.propertymanagement.test.config.TestApplicationContext 类中定义的名称为“entityManagerFactoryBean”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.NullPointerException 引起:java.util.Hashtable.put(Unknown Source) at java.util.Properties.setProperty(Unknown Source) at org.hibernate.cfg.annotations.SimpleValueBinder 的 java.lang.NullPointerException .setType(SimpleValueBinder.java:227) 在 org.hibernate.cfg.annotations.PropertyBinder.makePropertyAndValue(PropertyBinder.java:188) 在 org.hibernate.cfg.annotations.PropertyBinder.makePropertyValueAndBind(PropertyBinder.java:203) 在 org. hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:
我有以下库(使用 Maven):
<hibernate.version>4.1.2</hibernate.version>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.0.3.RELEASE</version>
</dependency>