【问题标题】:Spring Boot Hibernate JPA mapping composite primary key gives IllegalArgumentException: This class does not define an IdClassSpring Boot Hibernate JPA 映射复合主键给出 IllegalArgumentException: This class does not defined an IdClass
【发布时间】:2019-12-24 21:40:11
【问题描述】:

我已经为我的实体 UserActivity 配置了复合主键,如下所示,msisdn 和 extReferenceId 上的复合键。两者都在我的实体中用 @Id 注释。 IdClass 是 UserActivityId 并且它也被注释如下。当我启动 Spring Boot 应用程序时,它失败并出现错误:

java.lang.IllegalArgumentException:此类 [class com.compay.ipspaymentstatus.model.UserActivity] 未定义 IdClass

但我已经定义了我的 IdClass 并对其进行了注释。

我尝试过 IdClass 注释,并且在 hibernate xml 映射中也提到了 IdClass,如下所示 似乎没有任何工作,并说 IdClass 未定义。

UserActivity.java

package com.compay.ipspaymentstatus.model;

import java.io.Serializable;
import javax.persistence.*;

@Entity
@IdClass(UserActivityId.class)
@Table(name = "USERACTIVITY")
public class UserActivity implements Serializable {

    private static final long serialVersionUID = -2547001532653675405L;

    @Id
    @Column(name = "MSISDN")
    private String msisdn;
    @Id
    @Column(name = "EXT_REFERENCE_ID")
    private String extReferenceID;

    private String subscriberID;
    private String deviceModelID;

    public String getMsisdn() {
        return msisdn;
    }

    public void setMsisdn(String msisdn) {
        this.msisdn = msisdn;
    }

    public String getExtReferenceID() {
        return extReferenceID;
    }

    public void setExtReferenceID(String extReferenceID) {
        this.extReferenceID = extReferenceID;
    }

    public String getSubscriberID() {
        return subscriberID;
    }

    public void setSubscriberID(String subscriberID) {
        this.subscriberID = subscriberID;
    }

    public String getDeviceModelID() {
        return deviceModelID;
    }

    public void setDeviceModelID(String deviceModelID) {
        this.deviceModelID = deviceModelID;
    }
}

UserActivityId.java


package com.compay.ipspaymentstatus.model;

import java.io.Serializable;
import java.util.Objects;
import javax.persistence.IdClass;

@IdClass(UserActivityId.class)
public class UserActivityId implements Serializable{

        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private String msisdn;
        private String extReferenceID;
        public UserActivityId() {

        }
        public UserActivityId(String msisdn, String extReferenceID) {
            this.msisdn = msisdn;
            this.extReferenceID = extReferenceID;

        }

        public String getMsisdn() {
            return msisdn;
        }

        public void setMsisdn(String msisdn) {
            this.msisdn = msisdn;
        }

        public String getExtReferenceID() {
            return extReferenceID;
        }

        public void setExtReferenceID(String extReferenceID) {
            this.extReferenceID = extReferenceID;
        }

        @Override
        public boolean equals(Object o) {

            if (o == this) {
                return true;
            }
            if (!(o instanceof UserActivity)) {
                return false;
            }
            UserActivity userActivity = (UserActivity) o;
            return Objects.equals(msisdn, userActivity.getMsisdn()) &&
                   Objects.equals(extReferenceID, userActivity.getExtReferenceID());
        }

        @Override
        public int hashCode() {
            return Objects.hash(msisdn, extReferenceID);
        }



}

用户活动.hbm.xml


<hibernate-mapping>
    <class name="com.compay.ipspaymentstatus.model.UserActivity" table="USERACTIVITY">
        <composite-id class="com.compay.ipspaymentstatus.model.UserActivityId">
            <key-property name="msisdn" column="MSISDN" type="string" />
            <key-property name="extReferenceID" column="EXT_REFERENCE_ID" type="string" />
        </composite-id>
        <property name="subscriberID" type="string">
            <column name="SUBSCRIBERID" length="20" not-null="true" />
        </property>
        <property name="deviceModelID" type="string">
            <column name="DEVICEMODELID" length="30" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

启动 springboot 应用程序时收到错误

Caused by: java.lang.IllegalArgumentException: This class [class com.compay.ipspaymentstatus.model.UserActivity] does not define an IdClass
    at org.hibernate.metamodel.internal.AbstractIdentifiableType.getIdClassAttributes(AbstractIdentifiableType.java:183) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation$IdMetadata.<init>(JpaMetamodelEntityInformation.java:259) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:88) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:66) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:188) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:139) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:123) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:64) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:305) ~[spring-data-commons-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:297) ~[spring-data-commons-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport$$Lambda$570/757150717.get(Unknown Source) ~[na:na]
    at org.springframework.data.util.Lazy.getNullable(Lazy.java:211) ~[spring-data-commons-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.util.Lazy.get(Lazy.java:94) ~[spring-data-commons-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:300) ~[spring-data-commons-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:119) ~[spring-data-jpa-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    ... 45 common frames omitted

【问题讨论】:

    标签: hibernate jpa hibernate-mapping composite-id spring-boot-jpa


    【解决方案1】:

    尝试为您的复合 id 创建一个 pojo 类,其中只有参与复合 id 的字段。

    然后在pojo类上使用@Embeddable作为复合id

    UserActivity 类中创建新创建的 pojo 的对象,并将 @EmbeddedId 添加到其中

    也照常应用getter和setter

    更多详情请参考:Composite Id using annotation in hibernate + spring

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 2014-11-10
      • 2011-03-20
      • 2016-09-18
      • 2012-10-07
      • 2016-11-08
      • 2019-04-22
      • 1970-01-01
      • 2021-05-26
      相关资源
      最近更新 更多