PersistenceUnitUtil的getIdentifier方法不适用于Composite键

时间:2014-08-11 15:07:46

标签: jpa eclipselink jpa-2.0

尝试使用PersistenceUnitUtil的getIdentifier方法检索身份时,我遇到了以下错误。有什么我做错了。

Local Exception Stack: 
Exception [EclipseLink-0] (Eclipse Persistence Services - 2.4.2.v20130514-5956486): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: An internal error occurred accessing the primary key object [202].
Internal Exception: java.lang.NullPointerException
Descriptor: RelationalDescriptor(com.sample.Person --> [DatabaseTable(PERSON)])
    at org.eclipse.persistence.exceptions.DescriptorException.errorUsingPrimaryKey(DescriptorException.java:1923)
    at org.eclipse.persistence.internal.jpa.CMP3Policy$FieldAccessor.setValue(CMP3Policy.java:686)
    at org.eclipse.persistence.descriptors.CMPPolicy.createPrimaryKeyInstance(CMPPolicy.java:453)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getIdentifier(EntityManagerFactoryImpl.java:75)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getIdentifier(EntityManagerFactoryDelegate.java:679)
    at com.sample.PersistenceUtil.getEntityIdentifier(PersistenceUtil.java:27)
    at com.sample.PersistenceUtil.getEntityIdentifier(PersistenceUtil.java:18)
    at com.sample.OverrideUtilTest.canconfirmEntityEqualsforCompositeId2(OverrideUtilTest.java:147)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NullPointerException
    at org.eclipse.persistence.internal.jpa.CMP3Policy$FieldAccessor.setValue(CMP3Policy.java:682)
    ... 29 more

我正在尝试的代码示例位于

之下
@Entity
@IdClass(PersonPK.class)
@Table(name = "PERSON")
public class Person {
    @Id
    @Column(name = "CODE_C")
    private String code;

    @Column(name = "NAME_C")
    private String name;

    @Id
    @Column(name = "COUNTRY_C")
    private String country;

    public Person() {

    }

    public Person(String code, String country) {

        this.code = code;
        this.country = country;
    }


   public String getCode() {
        return this.code;
    }

     public String getCountry() {
        return this.country;
    }

}




public class PersonPK implements Serializable {
        private static final long serialVersionUID = 1L;

    private final String code;

    private final String country;

        public PersonPK(String city, String country) {
        super();
        this.code = city;
        this.country = country;
    }

    @Override
    public boolean equals(Object object) {
      return super.equals (object);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }
}

我正在使用的retirve身份的代码是

getEntityManagerFactory(persistenceUnitName).getPersistenceUnitUtil()
                .getIdentifier(entity);

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

  

主键类必须是公共的,并且必须具有公共的无参数构造函数。

类似情况下的Hibernate消息提供了更多信息

  

javax.persistence.PersistenceException:org.hibernate.InstantiationException:没有实体的默认构造函数:: PersonPK

解决方案:将默认构造函数添加到PersonPK