Friday, May 5, 2017

Caused by: java.lang.IllegalArgumentException: This class [class com.myzc98.server.domain.JsonRecordDomain] does not define an IdClass

Below is my error message:

Caused by: java.lang.IllegalArgumentException: This class [class com.myzc98.server.domain.JsonRecordDomain] does not define an IdClass
 at org.hibernate.jpa.internal.metamodel.AbstractIdentifiableType.getIdClassAttributes(AbstractIdentifiableType.java:183) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
 at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation$IdMetadata.(JpaMetamodelEntityInformation.java:253) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
 at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.(JpaMetamodelEntityInformation.java:84) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
 at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:68) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
 at org.springframework.data.jpa.repository.support.SimpleJpaRepository.(SimpleJpaRepository.java:110) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
 at com.myzc98.server.mapper.MyMapperImpl.(MyMapperImpl.java:15) ~[classes/:na]
 at com.myzc98.server.mapper.MyMapperFactoryBean$CustomRepositoryFactory.getTargetRepository(MyMapperFactoryBean.java:38) ~[classes/:na]
 at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:199) ~[spring-data-commons-1.13.1.RELEASE.jar:na]
 at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.1.RELEASE.jar:na]
 at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.1.RELEASE.jar:na]
 at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.1.RELEASE.jar:na]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
 ... 30 common frames omitted

Why:
Because i used Composite Primary Key in JsonRecordDomain.

How to fixed it, add @IdClass composite annotation:

@Entity
@Table(name = "myzc_json_record")
@IdClass(String2LongId.class)
public class JsonRecordDomain implements java.io.Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 主键类型
     */
    @Id
    @Column(name = "key_type")
    private String keyType;

    /**
     * 主键类型 ID
     */
    @Id
    @Column(name = "key_id")
    private Long keyId;

    // ...
}

public class String2LongId implements Serializable {
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 
 @Column(nullable = false)
 private String keyType;

 @Column(nullable = true)
 private Long keyId;

 public String2LongId() {
 }

 public String2LongId(String keyType, Long keyId) {
  this.keyType = keyType;
  this.keyId = keyId;
 }

 public String getKeyType() {
  return keyType;
 }

 public void setKeyType(String keyType) {
  this.keyType = keyType;
 }

 public Long getKeyId() {
  return keyId;
 }

 public void setKeyId(Long keyId) {
  this.keyId = keyId;
 }

 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + ((keyType == null) ? 0 : keyType.hashCode());
  result = prime * result + ((keyId == null) ? 0 : keyId.hashCode());
  return result;
 }

 @Override
 public boolean equals(Object obj) {
  do{
   if (this == obj)
    return true;
   
   if (obj == null)
    break;
   
   if (!String2LongId.class.equals(obj.getClass()))
    break;
   
   String2LongId other = (String2LongId) obj;
   if (this.keyType == null) {
    if (other.keyType != null)
     break;
   }else if(!this.keyType.equals(other.keyType)){
    break;
   }
   
   if (this.keyId == null) {
    if (other.keyId != null)
     break;
   }else if(!this.keyId.equals(other.keyId)){
    break;
   }
   
   return true;
  }while(false);
  
  return false;
 }

 @Override
 public String toString() {
  return "String2LongId [keyType=" + keyType + ", keyId=" + keyId + "]";
 }
}

No comments: