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;

@Entity
@Table(name = "user_type")

public class UserType {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    //@Access(AccessType.FIELD)
    public long id;

    @Column
    //@Access(AccessType.FIELD)
    public String name;

    public UserType() {

    }

    public UserType(String name, long id) {
        super();
        this.name = name;
        this.id = id;
    }



    //@Access(AccessType.PROPERTY)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //@Access(AccessType.PROPERTY)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }



}


pom.xml

<?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.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.haven</groupId>
    <artifactId>Haven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Haven</name>
    <description>Haven-Backend</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.3.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.3.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.10</version>
        <scope>provided</scope>
    </dependency>
    
    <dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>2.3.2</version>
</dependency>


<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.2</version>
</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

session

try {
            StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
            Metadata meta = new MetadataSources(ssr).addAnnotatedClass(UserType.class).getMetadataBuilder().build();

            SessionFactory factory = meta.getSessionFactoryBuilder().build();
            Session session = factory.openSession();
            Transaction t = session.beginTransaction();

            UserType ut = new UserType();

            ut.setId(1L);
            ut.setName("customer");
            System.out.println(ut.getId());

            session.saveOrUpdate(ut);
            t.commit();
            System.out.println("successfully saved");
            factory.close();
            session.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>      
  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/haven</property>
    <property name="connection.username">root</property>
    <property name="connection.password">u37y5a</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
    <property name="hbm2ddl.auto">update</property>  
       

     
  </session-factory>
</hibernate-configuration>

Here I am trying to insert data using session and it is giving me error

org.hibernate.AnnotationException: No identifier specified for entity: com.haven.main.lib.dao.UserType
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:231)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:778)
    at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:250)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:231)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:274)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:84)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:474)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:85)
    at com.haven.main.HavenApplication.main(HavenApplication.java:23)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

On the internet, I have tried many ways. It works with CrudRepository and also with it creating tables, but using sessions doesn't work.

Sometimes it says that properties are not accessible when all properties are exposed, I have tried downgrading hibernate with no luck. I am using hibernate 5

After removing the access type it started giving

org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [public long com.haven.main.lib.dao.UserType.id] by reflection for persistent property [com.haven.main.lib.dao.UserType#id] : com.haven.main.lib.dao.UserType@1d3a4c49
    at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:75)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:224)
    at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4931)
    at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4631)
    at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:226)
    at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:540)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:83)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:678)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:670)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:665)
    at com.haven.main.HavenApplication.main(HavenApplication.java:42)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.IllegalArgumentException: Can not set long field com.haven.main.lib.dao.UserType.id to com.haven.main.lib.dao.UserType
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at java.base/jdk.internal.reflect.UnsafeLongFieldAccessorImpl.getLong(UnsafeLongFieldAccessorImpl.java:60)
    at java.base/java.lang.reflect.Field.getLong(Field.java:630)
    at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:65)
    ... 16 more

Updates

When adding AccessType on getter and adding @Id annotation, new error appears

org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.haven.main.lib.dao.UserType.id
    at org.hibernate.property.access.spi.GetterMethodImpl.get(GetterMethodImpl.java:65)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:224)
    at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4931)
    at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4631)
    at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:226)
    at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:540)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:83)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:678)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:670)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:665)
    at com.haven.main.HavenApplication.main(HavenApplication.java:42)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.hibernate.property.access.spi.GetterMethodImpl.get(GetterMethodImpl.java:42)
    ... 16 more

Any help would be greatly appreciated, forgive my typos and mistakes.

Thanks in advance

ppst99

finally,

After researching tons of posts, blogs, and examples from many developers, I found no specific solution to the problem, as the code samples the developers are using are said to not work with hibernate 5.

It also suggested avoiding xml configuration and opting for an annotation based implementation, I implemented it and it works great with @Transactionalannotations for a lot of requirements and I can configure it according to my needs.

I avoid the session based approach in favor of @Transactionalannotations

Thanks to those who helped me.

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

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

Spring runtime error, no identifier specified for entity:

B.obed: New to Java and Spring. Attempts to create an application were unsuccessful. I have the following entities and controllers in my application. But at runtime I get an error. I've posted some snippets for easier reading. staff.java @Data @Entity public c

Spring runtime error, no identifier specified for entity:

B.obed: New to Java and Spring. Attempts to create an application were unsuccessful. I have the following entities and controllers in my application. But at runtime I get an error. I've posted some snippets for easier reading. staff.java @Data @Entity public c

Spring runtime error, no identifier specified for entity:

B.obed: New to Java and Spring. Attempts to create an application were unsuccessful. I have the following entities and controllers in my application. But at runtime I get an error. I've posted some snippets for easier reading. staff.java @Data @Entity public c