Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JPA在包装器对象上映射单向OneToMany关联_Jpa - Fatal编程技术网

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

使用JPA在包装器对象上映射单向OneToMany关联,jpa,Jpa,我正在将Hibernate配置转换为使用JPA。当前配置有一个AlertPref类(ALERT_PREF表),其中包含一组长类型,这些长类型是ALERT_类别表中的主键值。有一个ALERT_PREF_类别联接表将两者连接起来。我可以在JPA中通过将连接表定义为实体类并在AlertPref类中拥有AlertPrefCategory对象的集合而不是长ID来设置这种关系,但是如果可能的话,我希望避免这种情况,而是设置从AlertPref到长ID的单向映射。一些遗留代码使用ID,因此很难更改此代码 下面

我正在将Hibernate配置转换为使用JPA。当前配置有一个AlertPref类(ALERT_PREF表),其中包含一组长类型,这些长类型是ALERT_类别表中的主键值。有一个ALERT_PREF_类别联接表将两者连接起来。我可以在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>

100
以下是我试图在JPA中使用的内容,但它抛出了一个异常“使用@OneToMany或@ManyToMany针对未映射的类:AlertPref.excludedCategoryIds[java.lang.Long]”

@实体
@表(name=“ALERT\u PREF”)
公共类AlertPref{
@身份证
@TableGenerator(name=“table_gen”,allocationSize=1)
@GeneratedValue(策略=GenerationType.TABLE,generator=“TABLE_gen”)
@列(name=“ALERT\u PREF\u ID”)
私有长前缀;
@列(name=“USER\u ID”,null=false)
私有字符串用户标识;
@OneToMany(cascade=CascadeType.ALL,orphan=true)
@JoinTable(name=“警报优先类别”,
joinColumns=@JoinColumn(name=“ALERT\u PREF\u ID”),
inverseJoinColumns=@JoinColumn(name=“EXCLUDED\u CATEGORY\u ID”))
专用集排除类别ID;
/**
*@return返回带前缀的警报。
*/
公共长getAlertPrefId(){
返回警告前缀;
}
/**
*@param alertPrefId
*警报前缀为set。
*/
public void setAlertPrefId(长alertPrefId){
this.alertPrefId=alertPrefId;
}
/**
*@return返回用户标识。
*/
公共字符串getUserId(){
返回用户标识;
}
/**
*@param userId
*要设置的用户ID。
*/
public void setUserId(字符串userId){
this.userId=userId;
}
/**
*@返回ExcludedCategoryId
*/
公共集getExcludedCategoryIds(){
退货不包括类别ID;
}
/**
*@param excludedCategoryId要设置的excludedCategoryId
*/
公共无效集合ExcludedCategoryId(集合ExcludedCategoryId){
this.excludedCategoryIds=excludedCategoryIds;
}
}

必须使用ElementCollection和CollectionTable注释,如中所述。

我假设如果要执行级联操作,然后我需要定义一个实体来表示连接表-正确吗?不正确。包含基本类型或嵌入式组件的集合与包含它的实体具有相同的生命周期。因此,创建包含ID的AlertPref将在联接表中创建行。修改集合将相应地修改行。删除实体将删除联接表中的行。如何标记级联和孤立删除?这些不是ElementCollection或CollectionTable上的选项。如果我删除AlertPref列,我希望删除AlertPref类别中引用AlertPref的所有列,以保持引用完整性。我不了解您的意思。AlertPrefCategory不引用AlertPref。联接表引用了AlertPref。正如我所说,删除AlertPref将删除联接表中引用此AlertPref的所有行。我想我理解。。。那么,级联和删除设置的意义是什么?这不需要在JPA中指定吗?我的想法是,当删除ALERT_PREF中的行时,必须专门为要删除的联接表(ALERT_PREF_类别)中的行设置此选项。
@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;
    }
}