【问题标题】:getIdentifier method of PersistenceUnitUtil not working for Composite keysPersistenceUnitUtil 的 getIdentifier 方法不适用于复合键
【发布时间】:2014-10-04 11:20:24
【问题描述】:

尝试使用 PersistenceUnitUtil 的 getIdentifier 方法检索身份时出现以下错误。是不是我做错了什么。

本地异常堆栈:
异常 [EclipseLink-0] (Eclipse Persistence Services - 2.4.2.v20130514-5956486):org.eclipse.persistence.exceptions.DescriptorException
异常说明:访问主键对象时发生内部错误 [202]。
内部异常:java.lang.NullPointerException
描述符:RelationalDescriptor(com.sample.Person --> [DatabaseTable(PERSON)])

在 org.eclipse.persistence.exceptions.DescriptorException.errorUsingPrimaryKey(DescriptorException.java:1923) 在 org.eclipse.persistence.internal.jpa.CMP3Policy$FieldAccessor.setValue(CMP3Policy.java:686) 在 org.eclipse.persistence.descriptors.CMPPolicy.createPrimaryKeyInstance(CMPPolicy.java:453) 在 org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getIdentifier(EntityManagerFactoryImpl.java:75) 在 org.eclipse.persistence.internal.jpa.EntityManagerFactoryDe​​legate.getIdentifier(EntityManagerFactoryDe​​legate.java:679) 在 com.sample.PersistenceUtil.getEntityIdentifier(PersistenceUtil.java:27) 在 com.sample.PersistenceUtil.getEntityIdentifier(PersistenceUtil.java:18) 在 com.sample.OverrideUtilTest.canconfirmEntityEqualsforCompositeId2(OverrideUtilTest.java:147) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:606) 在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:309) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 引起:java.lang.NullPointerException 在 org.eclipse.persistence.internal.jpa.CMP3Policy$FieldAccessor.setValue(CMP3Policy.java:682) ... 29 更多

我正在尝试的代码示例是这样的:

@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();
    }
}

我正在使用的检索身份的代码是

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

感谢任何帮助

【问题讨论】:

    标签: jpa eclipselink jpa-2.0


    【解决方案1】:

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

    类似情况下的休眠消息会提供更多信息

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

    解决方法:PersonPK添加默认构造函数

    【讨论】:

      猜你喜欢
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 2015-05-03
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多