简体   繁体   中英

Spring Boot Hibernate JPA mapping composite primary key gives IllegalArgumentException: This class does not define an IdClass

I have configured composite primary key for my entity UserActivity as follows with composite key on msisdn & extReferenceId. Both are annotated with @Id in my entity. The IdClass is UserActivityId and it is annotated too as below. When I start the Spring Boot app, it is failing with error:

java.lang.IllegalArgumentException: This class [class com.compay.ipspaymentstatus.model.UserActivity] does not define an IdClass

But I have defined my IdClass and annotated it.

I have tried IdClass annotation and also mentioned IdClass in hibernate xml mapping as below nothing seemed to work and says IdClass not defined.

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>

Error received while starting the springboot application

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

Try to create a pojo class for your composite id having only the field participating in the composite id.

Then use @Embeddable on the pojo class for composite id

Create an object of the newly created pojo inside your UserActivity class and add @EmbeddedId to it

Also apply getters and setters to it as usual

For more details refer: Composite Id using annotation in hibernate + spring

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM