Why Hibernate throws “no identifier specified”?

问题: I'm trying to insert data into an Oracle database for which I have attached the screenshot of it. There is an entity class created inside a model package which contains all...

问题:

I'm trying to insert data into an Oracle database for which I have attached the screenshot of it. There is an entity class created inside a model package which contains all columns and a table. There is an id column which is auto-increment in the database details attached. The main issue is with auto incremented data for id column using @GeneratedValue(strategy = GenerationType.IDENTITY) which gives me no identity specified for this entity details of error attached below. So for this created an object of that entity class and using save method I was trying to save the same but due to error for that identifier, it is not getting saved.

This is my entity class which contains tables and columns

package com.sid.demo.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
//import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.springframework.data.annotation.Id;

@Table(name="user_registration")  //this is a table present in my oracle database
@Entity
public class hindalco_model 
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) //this is where problem exists
    @Column(name="user_id",updatable=false,nullable=false)
    private Long user_id;  //this is column a present in oracle database which is auto incremented

    @Column(name="user_name")
    private String user_name;

    @Column(name="email")
    private String email;

    @Column(name="password")
    private String password;

    public Long getUser_id() {
        return user_id;
    }

    public void setUser_id(Long user_id) {
        this.user_id = user_id;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "hindalco_model [user_id=" + user_id + ", user_name=" + user_name + ", email=" + email + ", password="
                + password + "]";
    }       
}

This is my controller class in which I have created an object of the above entity class

package com.sid.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.sid.demo.model.hindalco_model;
import com.sid.demo.repository.hindalco_repository;

@RestController
@RequestMapping("/hindalco_user")
public class hindalco_controller 
{
    @Autowired
    hindalco_repository hindalco_repo;

    @RequestMapping(value="/user_registration",method = RequestMethod.GET, produces = {"application/json"})
    public hindalco_model user_registration(hindalco_model hindalco)
    {
        hindalco_repo.save(hindalco);
         return hindalco;
    }

}

//this is my repository class through which I am calling save method which takes entity class object as an argument

package com.sid.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import com.sid.demo.model.hindalco_model;

public interface hindalco_repository extends JpaRepository<hindalco_model,Integer>
{

}

//below is my database SQL query which I have executed for auto increment. First I have created a sequence below in commented line and then trigger for the auto incremented

/*create sequence friend_auto start with 1 increment by 1;*/

create or replace trigger user_id
before insert on user_registration
for each row
begin
:new.user_id :=user_id.nextval;
end;

Attached the database screenshot details of all columns enter image description here

Below are the errors that are getting generated while compilation

2019-02-27 18:30:09.076  INFO 7412 --- [           main] org.apache.jasper.servlet.TldScanner     : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2019-02-27 18:30:09.079  INFO 7412 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-02-27 18:30:09.080  INFO 7412 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1603 ms
2019-02-27 18:30:09.293  INFO 7412 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-02-27 18:30:09.295  WARN 7412 --- [           main] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=oracle.jdbc.driver.OracleDriver was not found, trying direct instantiation.
2019-02-27 18:30:09.463  INFO 7412 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-02-27 18:30:09.529  INFO 7412 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2019-02-27 18:30:09.700  INFO 7412 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.4.0.Final}
2019-02-27 18:30:09.701  INFO 7412 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-02-27 18:30:09.850  INFO 7412 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2019-02-27 18:30:10.037  INFO 7412 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.Oracle12cDialect
2019-02-27 18:30:10.254  INFO 7412 --- [           main] org.hibernate.type.BasicTypeRegistry     : HHH000270: Type registration [byte[]] overrides previous : org.hibernate.type.BinaryType@59696551
2019-02-27 18:30:10.254  INFO 7412 --- [           main] org.hibernate.type.BasicTypeRegistry     : HHH000270: Type registration [[B] overrides previous : org.hibernate.type.BinaryType@59696551
2019-02-27 18:30:10.255  INFO 7412 --- [           main] org.hibernate.type.BasicTypeRegistry     : HHH000270: Type registration [Byte[]] overrides previous : org.hibernate.type.WrapperBinaryType@17273273
2019-02-27 18:30:10.255  INFO 7412 --- [           main] org.hibernate.type.BasicTypeRegistry     : HHH000270: Type registration [[Ljava.lang.Byte;] overrides previous : org.hibernate.type.WrapperBinaryType@17273273
2019-02-27 18:30:10.313  WARN 7412 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 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.sid.demo.model.hindalco_model
2019-02-27 18:30:10.313  INFO 7412 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2019-02-27 18:30:10.327  INFO 7412 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2019-02-27 18:30:10.330  INFO 7412 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-02-27 18:30:10.345  INFO 7412 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-02-27 18:30:10.355 ERROR 7412 --- [           main] o.s.boot.SpringApplication               : Application run failed

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.sid.demo.model.hindalco_model
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1762) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at com.sid.demo.SpringBootJpaApplication.main(SpringBootJpaApplication.java:10) [classes/:na]
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.sid.demo.model.hindalco_model
    at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:266) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:211) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:785) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:250) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:231) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:274) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:903) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:934) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1821) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1758) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    ... 16 common frames omitted

The exact error below one

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.sid.demo.model.hindalco_model


回答1:

In hindalco_model you annotated user_id with org.springframework.data.annotation.Id, but it should be javax.persistance.Id.

Change the import at line 9 with

import javax.persistance.Id;

Also be aware that you defined your monel id tye as Long, while in your hindalco_repository you set it as Integer.

Change JpaRepository<hindalco_model,Integer> to JpaRepository<hindalco_model, Long>

  • 发表于 2019-03-01 00:48
  • 阅读 ( 2832 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除