spring org.hibernate.AnnotationException:没有为实体指定标识符 - 即使它是

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15320408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:48:24  来源:igfitidea点击:

org.hibernate.AnnotationException: No identifier specified for entity - even when it was

springhibernatespring-dataspring-data-jpa

提问by checklist

I have the following configuration:

我有以下配置:

<bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="jpaDataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="packagesToScan">
        <list>
            <value>com.example.domain</value>
            <value>com.example.repositories</value>
        </list>
    </property>
</bean>

I have my Geoname class in com.example.domain:

我在 com.example.domain 中有我的 Geoname 类:

@Entity
@Table(name="geonames")
public class Geoname implements Serializable {

    @Id
    @Column(name="geonameid")
    private Long geonameid = null;
}

yet, when running, I get the following exception:

然而,在运行时,我收到以下异常:

Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.example.domain.Geoname 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:664) at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3449) at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3403) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1330) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1730)

引起:org.hibernate.AnnotationException:没有为实体指定标识符:com.example.domain.Geoname 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:664) at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3449) at org.hibernate.cfg.Configuration $MetadataSourceQueue.processMetadata(Configuration.java:3403) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1330) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1730)

Any ideas why?

任何想法为什么?

side note: I am combining both mongodb and hibernate/ mysql on this project.

旁注:我在这个项目中结合了 mongodb 和 hibernate/mysql。

回答by checklist

I had the following

我有以下

import org.springframework.data.annotation.Id;

Naturally, it should be:

自然应该是:

import javax.persistence.Id;

Thanks to @JB Nizet

感谢@JB Nizet

回答by Kunal Hazard

I faced the same error.I solved it and figured out i didn't put @Id annotations in id field in my Entity class.

我遇到了同样的错误。我解决了它并发现我没有在我的实体类的 id 字段中放置 @Id 注释。

@Entity
@Table(name="geonames")
public class Geoname implements Serializable {

    @Column(name="geonameid")
    private Long geonameid = null;
}

回答by PSR

try this

尝试这个

  @Column(name="geonameid",unique=true,nullable=false)