2

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

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

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

我已经尝试过 IdClass 注释,并且还在休眠 xml 映射中提到了 IdClass,如下所示似乎没有任何效果,并说 IdClass 未定义。

用户活动.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
4

1 回答 1

0

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

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

在你的类中创建一个新创建的 pojo 的对象UserActivity并添加@EmbeddedId到它

也像往常一样对其应用 getter 和 setter

有关更多详细信息,请参阅:Composite Id using annotation in hibernate + spring

于 2019-08-19T16:36:19.747 回答