0
我將Hibernate配置轉換爲使用JPA。當前配置具有AlertPref類(ALERT_PREF表),其中包含來自ALERT_CATEGORY表的主鍵值的Long類型集合。有一個ALERT_PREF_CATEGORY聯結表連接這兩個表。我可以在JPA中設置這種關係,方法是將聯結表定義爲實體類,並在AlertPref類中具有AlertPrefCategory對象的集合而不是Long ID,但是如果可能,我想避免這種情況,而是設置單向映射對長ID的AlertPref。一些遺留代碼使用這些ID,並且很難更改此代碼。使用JPA映射包裝器對象上的單向OneToMany關聯
這裏是在Hibernate配置當前類的標籤,其工作正常:
<class name="AlertPref" table="ALERT_PREF">
<id name="alertPrefId" column="ALERT_PREF_ID" type="long">
<generator class="hilo">
<param name="max_lo">100</param>
</generator>
</id>
<property name="userId" column="USER_ID" type="string"
not-null="true" />
<set name="excludedCategoryIds" table="ALERT_PREF_CATEGORY" cascade="all,delete-orphan">
<key column="ALERT_PREF_ID" />
<element column="EXCLUDED_CATEGORY_ID" type="long" />
</set>
</class>
這是我試圖JPA使用,但它是扔的@OneToMany或@ManyToMany瞄準例外「使用未映射類:AlertPref.excludedCategoryIds [java.lang.Long中]」
@Entity
@Table(name = "ALERT_PREF")
public class AlertPref {
@Id
@TableGenerator(name = "table_gen", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.TABLE, generator = "table_gen")
@Column(name = "ALERT_PREF_ID")
private long alertPrefId;
@Column(name = "USER_ID", nullable = false)
private String userId;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(name = "ALERT_PREF_CATEGORY",
joinColumns = @JoinColumn(name = "ALERT_PREF_ID"),
inverseJoinColumns = @JoinColumn(name = "EXCLUDED_CATEGORY_ID"))
private Set<Long> excludedCategoryIds;
/**
* @return Returns the alertPrefId.
*/
public long getAlertPrefId() {
return alertPrefId;
}
/**
* @param alertPrefId
* The alertPrefId to set.
*/
public void setAlertPrefId(long alertPrefId) {
this.alertPrefId = alertPrefId;
}
/**
* @return Returns the userId.
*/
public String getUserId() {
return userId;
}
/**
* @param userId
* The userId to set.
*/
public void setUserId(String userId) {
this.userId = userId;
}
/**
* @return the excludedCategoryIds
*/
public Set<Long> getExcludedCategoryIds() {
return excludedCategoryIds;
}
/**
* @param excludedCategoryIds the excludedCategoryIds to set
*/
public void setExcludedCategoryIds(Set<Long> excludedCategoryIds) {
this.excludedCategoryIds = excludedCategoryIds;
}
}
我假設,如果我想做一個級聯操作,那麼我需要定義一個實體來表示結點表 - 是對的嗎? – acvcu
否。包含基本類型或嵌入組件的集合與持有它的實體具有相同的生命週期。因此,創建包含ID的AlertPref將在連接表中創建行。修改設置將相應地修改行。刪除實體將刪除連接表中的行。 –
我怎麼能注意到級聯和孤兒刪除?這些不是ElementCollection或CollectionTable的選項。如果我刪除AlertPref列,我希望刪除AlertPrefCategory中引用AlertPref的任何列以保持參照完整性。 – acvcu