org.hibernate.AnnotationException: No identifier s

2019-02-02 21:58发布

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:

@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)

Any ideas why?

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

5条回答
小情绪 Triste *
2楼-- · 2019-02-02 22:25

try this

  @Column(name="geonameid",unique=true,nullable=false)
查看更多
狗以群分
3楼-- · 2019-02-02 22:31

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

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

    @Column(name="geonameid")
    private Long geonameid = null;
}
查看更多
孤傲高冷的网名
4楼-- · 2019-02-02 22:33

I had the following

import org.springframework.data.annotation.Id;

Naturally, it should be:

import javax.persistence.Id;

Thanks to @JB Nizet

查看更多
The star\"
5楼-- · 2019-02-02 22:35

I was facing the same issue, later found out i was not putting @Id, same as erikrunia

查看更多
戒情不戒烟
6楼-- · 2019-02-02 22:36

You might have extra fields in the Entity class. Like fields that are not annotated Or Constructors. Please remove those and try. It worked for me.

Happy Coding.

查看更多
登录 后发表回答