使用 JPA 在包装器对象上映射单向 OneToMany 关联

发布于 2024-12-10 21:43:30 字数 2688 浏览 0 评论 0原文

我正在将 Hibernate 配置转换为使用 JPA。当前配置有一个 AlertPref 类(ALERT_PREF 表),其中包含 Long 类型的集合,这些类型是 ALERT_CATEGORY 表中的主键值。有一个 ALERT_PREF_CATEGORY 连接表将两者连接起来。我可以在 JPA 中设置这种关系,方法是将联结表定义为实体类,并在 AlertPref 类中使用 AlertPrefCategory 对象的集合而不是长 ID,但我想尽可能避免这种情况,而是从以下位置设置单向映射: AlertPref 为长 ID。一些遗留代码使用 ID,并且很难更改此代码。

这是 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” 。长的]”

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

I am converting Hibernate configuration over to using JPA. The current configuration has an AlertPref class (ALERT_PREF table) with a collection of Long types that are primary key values from the ALERT_CATEGORY table. There is an ALERT_PREF_CATEGORY junction table that joins the two. I could setup this relationship in JPA by defining the junction table as an Entity class and having a collection of AlertPrefCategory objects instead of Long IDs in the AlertPref class, but I want to avoid this if possible, and instead setup a uni-directional mapping from AlertPref to the Long IDs. Some of the legacy code uses the IDs, and it would be difficult to change this code.

Here's the current class tag in the Hibernate config, which works fine:

<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>

Here is what I tried to use in JPA, but it is throwing the exception "Use of @OneToMany or @ManyToMany targeting an unmapped class: 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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情出卖 2024-12-17 21:43:30

您必须使用 ElementCollection 和 CollectionTable 注释,如 Hibernate 参考文档

You must use the ElementCollection and CollectionTable annotations, as explained in the Hibernate reference documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文