How to fix org.hibernate.AnnotationException: No identifier specified for entity


Akif Assad
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.javacourse.project.hibernateAndJPA.Entities.City
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:609) ~[spring-beans-5.3.3.jar:5.3.3]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531) ~[spring-beans-5.3.3.jar:5.3.3]
    package com.javacourse.project.hibernateAndJPA.Entities;
    
    import javax.persistence.*;
    
    @Entity
    @Table(name="city")
    public class City {
        @Column(name="Id")
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private int id;
        @Column(name="name")
        private String name;
        @Column(name="countryCode")
        private String countryCode;
        @Column(name="district")
        private String district;
        @Column(name="population")
        private int population;
        public City(int id, String name, String countryCode, String district, int population) {
            super();
            this.id = id;
            this.name = name;
            this.countryCode = countryCode;
            this.district = district;
            this.population = population;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getCountryCode() {
            return countryCode;
        }
        public void setCountryCode(String countryCode) {
            this.countryCode = countryCode;
        }
        public String getDistrict() {
            return district;
        }
        public void setDistrict(String district) {
            this.district = district;
        }
        public int getPopulation() {
            return population;
        }
        public void setPopulation(int population) {
            this.population = population;
        }
        
    }

Application scale:

    spring.datasource.url=jdbc:derby://localhost:1527/sample
    spring.datasource.username=user
    spring.datasource.password=1234
    spring.datasource.initialize=false
    spring.datasource.driver-class-name=org.apache.derby.jdbc.ClientDriver

pom.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.4.2</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.javacourse.project</groupId>
        <artifactId>hibernateAndJPA</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>hibernateAndJPA</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>11</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
            <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.derby/derbyclient -->
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.15.2.0</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
    </dependency>
    
            
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
Simon Martinelli

@Id annotation is missing

@Id 
@Column(name="Id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

Every entity needs an ID, the primary key

Related


No identifier specified for entity: XYZ (AnnotationException)

ppst99 entity import javax.persistence.Access; import javax.persistence.AccessType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entit

No identifier specified for entity: XYZ (AnnotationException)

ppst99 entity import javax.persistence.Access; import javax.persistence.AccessType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entit

hibernate: no identifier specified for entity

Patrick H I am trying to build a small project in Hibernate and use mysql docker database for demonstration. Everything worked fine until I added a new class called Flight and a corresponding FlightController. The log says: 2018-12-02 14:42:47,281 DEBUG [main]

Identifier not specified for entity in Java Hibernate

Pratic Josh I am using hibernate and I have tables mapped with beans. If I don't assign any particular column value as @id, it throws a "no identifier specified for entity" error, but it's not a primary key in my datatable. I want to add multiple records with

Identifier not specified for entity in Java Hibernate

Pratik Joshi I am using hibernate and I have the table mapped with beans. If I don't assign any particular column value as @id, it throws a "no identifier specified for entity" error, but it's not a primary key in my datatable. I want to add multiple records w

Identifier not specified for entity in Java Hibernate

Pratic Josh I am using hibernate and I have tables mapped with beans. If I don't assign any particular column value as @id, it throws a "no identifier specified for entity" error, but it's not a primary key in my datatable. I want to add multiple records with