java.lang.RuntimeException:org.hibernate.AnnotationException:没有为实体指定标识符

时间:2016-01-05 05:42:44

标签: java hibernate intellij-idea

我看过几个关于这个主题的帖子,其中每个人都建议添加 @Id 注释我已经从头开始做了除了IntelliJ ide帮助我生成 Entity 类自动,所以基本上我没有参与编写这些注释。但是,当我尝试从 hibernate 控制台运行查询时:

     from InfoEntity where id = 1

它给了我例外:

java.lang.RuntimeException: org.hibernate.AnnotationException: No identifier specified for entity: sample.InfoEntity
    at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:277)
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:224)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:665)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3512)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3466)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1756)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
    at com.intellij.hibernate.console4.remote.HibernateFacadeImpl$1.buildSessionFactory(HibernateFacadeImpl.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$256(TCPTransport.java:683)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:276)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:253)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:162)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:217)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:171)
    at com.sun.proxy.$Proxy158.buildSessionFactory(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.execution.rmi.RemoteUtil.invokeRemote(RemoteUtil.java:124)
    at com.intellij.execution.rmi.RemoteUtil.access$100(RemoteUtil.java:36)
    at com.intellij.execution.rmi.RemoteUtil$2$1$1.compute(RemoteUtil.java:105)
    at com.intellij.execution.rmi.RemoteUtil.executeWithClassLoader(RemoteUtil.java:181)
    at com.intellij.execution.rmi.RemoteUtil$2$1.invoke(RemoteUtil.java:102)
    at com.sun.proxy.$Proxy158.buildSessionFactory(Unknown Source)
    at com.intellij.hibernate.engine.HibernateEngine.ensureInitialized(HibernateEngine.java:121)
    at com.intellij.hibernate.engine.HibernateEngine.createQuery(HibernateEngine.java:140)
    at com.intellij.jpa.engine.JpaEngineBase.executeQueryInner(JpaEngineBase.java:166)
    at com.intellij.jpa.engine.JpaEngineBase.access$000(JpaEngineBase.java:56)
    at com.intellij.jpa.engine.JpaEngineBase$1.compute(JpaEngineBase.java:128)
    at com.intellij.jpa.engine.JpaEngineBase$1.compute(JpaEngineBase.java:123)
    at com.intellij.database.console.AbstractEngine$4.compute(AbstractEngine.java:179)
    at com.intellij.database.console.AbstractEngine$4.compute(AbstractEngine.java:174)
    at com.intellij.database.console.AbstractEngine$3.run(AbstractEngine.java:159)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

实体类

import javax.persistence.*;

/**
 * Created by fayme on 1/5/2016.
 */
@Entity
@Table(name = "Info", schema = "dbo", catalog = "temp")
public class InfoEntity {

    @Id
    @Column(name = "id")
    private int id;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }



    @Basic
    @Column(name = "name")
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }




    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        InfoEntity that = (InfoEntity) o;

        if (id != that.id) return false;
        if (name != null ? !name.equals(that.name) : that.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }

hibernate.cfg.xml中

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=temp</property>
    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
      <mapping class="sample.InfoEntity"/>
    <!--<mapping resource="sample/InfoMapping.xml"/>-->
    <!-- DB schema will be updated if needed -->
    <!-- <property name="hbm2ddl.auto">update</property> -->
  </session-factory>
</hibernate-configuration>

在这种情况下,我做错了什么!!!

1 个答案:

答案 0 :(得分:0)

hibernate.cfg.xml 在创建Entity类时自动更新,稍后我手动添加了属性connaction.usernameconnection.password,其用户名为“Sa”及其密码现在它的工作。