Сопоставление составного первичного ключа Spring Boot Hibernate JPA дает IllegalArgumentException: этот класс не определяет IdClass

Я настроил составной первичный ключ для своей сущности UserActivity следующим образом с составным ключом на msisdn и extReferenceId. Оба аннотированы @Id в моей сущности. IdClass — это UserActivityId, и он также аннотирован, как показано ниже. Когда я запускаю приложение Spring Boot, оно выходит из строя с ошибкой:

java.lang.IllegalArgumentException: этот класс [класс com.compay.ipspaymentstatus.model.UserActivity] не определяет IdClass

Но я определил свой IdClass и аннотировал его.

Я попробовал аннотацию IdClass, а также упомянул IdClass в сопоставлении xml спящего режима, поскольку ниже ничего не работает и говорит, что 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);
        }



}

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

person Ramakrishna    schedule 19.08.2019    source источник


Ответы (1)


Попробуйте создать класс pojo для вашего составного идентификатора, имеющего только поле, участвующее в составном идентификаторе.

Затем используйте @Embeddable в классе pojo для составного идентификатора.

Создайте объект вновь созданного pojo внутри вашего класса UserActivity и добавьте к нему @EmbeddedId

Также примените к нему геттеры и сеттеры, как обычно

Для получения дополнительной информации см.: Составной идентификатор с использованием аннотации в спящем режиме + весна

person Hrishav Dhawaj Purkayastha    schedule 19.08.2019