1

我有一个奇怪的问题。我有一个如下的实体类

@javax.persistence.Entity

@Table(name = "ADMIN") public class Admin extends Entity{

/**
 * 
 */
private static final long serialVersionUID = 2949030196928420876L;

@NaturalId
@Column(name = "USERNAME", nullable = false)
private String username;

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

}

在一种方法中,我正在编写如下代码:

public void sampleMethod(){
//Some code
//Some code
Admin admin = (Admin) session.byNaturalId(Admin.class).using("USERNAME", "ADMIN").getReference();
}

但它给出了一个错误:

An Exception occured during get or save of Admin.: org.hibernate.HibernateException: Entity [com.edfx.tum.persist.entity.Admin] did not define a natural id
at org.hibernate.internal.SessionImpl$BaseNaturalIdLoadAccessImpl.<init>(SessionImpl.java:2451) [hibernate-core-4.1.8.Final.jar:4.1.8.Final]
at org.hibernate.internal.SessionImpl$BaseNaturalIdLoadAccessImpl.<init>(SessionImpl.java:2442) [hibernate-core-4.1.8.Final.jar:4.1.8.Final]
at org.hibernate.internal.SessionImpl$NaturalIdLoadAccessImpl.<init>(SessionImpl.java:2542) [hibernate-core-4.1.8.Final.jar:4.1.8.Final]
at org.hibernate.internal.SessionImpl$NaturalIdLoadAccessImpl.<init>(SessionImpl.java:2546) [hibernate-core-4.1.8.Final.jar:4.1.8.Final]
at org.hibernate.internal.SessionImpl$NaturalIdLoadAccessImpl.<init>(SessionImpl.java:2550) [hibernate-core-4.1.8.Final.jar:4.1.8.Final]
at org.hibernate.internal.SessionImpl$NaturalIdLoadAccessImpl.<init>(SessionImpl.java:2538) [hibernate-core-4.1.8.Final.jar:4.1.8.Final]
at org.hibernate.internal.SessionImpl.byNaturalId(SessionImpl.java:1061) [hibernate-core-4.1.8.Final.jar:4.1.8.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352) [hibernate-core-4.1.8.Final.jar:4.1.8.Final]
at com.sun.proxy.$Proxy50.byNaturalId(Unknown Source)
at com.edfx.tum.dao.basic.BasicDAO.insertAdmins(BasicDAO.java:109) [classes:]
at com.edfx.tum.dao.basic.BasicDAO.<init>(BasicDAO.java:50) [classes:]
at com.edfx.tum.common.listeners.DatabaseLoadListener.contextInitialized(DatabaseLoadListener.java:27) [classes:]
at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_25]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_25]
at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_25]

当我定义了 NaturalId 时,为什么会说 natural id 未定义?请帮忙!!

注释似乎不起作用。

4

1 回答 1

1

这是一个“简单的自然身份”(与复合身份相反)。您是否尝试过特定于简单自然 ID 的方法?IE:

Admin admin = (Admin) session.bySimpleNaturalId(Admin.class)
        .getReference("ADMIN");

对于这个特定的错误不应该有所作为,但更简洁。

您是否尝试过使用更新版本的 Hibernate (4.2.x)?

于 2013-09-21T18:24:21.970 回答