I’m getting Exception in thread “main” org.hibernate.HibernateException: The database returned no natively generated identity value
Codes:
Student.class
package com.vasnavi.hibernate1stApp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity(name = "Student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "roll_no")
private Long rollNo;
@Column(name="name")
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getRollNo() {
return rollNo;
}
public void setRollNo(Long rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.vasnavi.hibernate1stApp.Student" table="Students">
<id name="id" column="student_id">
<generator class="native"></generator>
</id>
<property name="rollNo" column="student_Roll_No"/>
<property name="name" column="student_Name" />
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/HibernateDB</property>
<property name="connection.username">root</property>
<property name="connection.password">Root1847@</property>
<property name="connection.pool_size">100</property>
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.id.new_generator_mappings">false</property>
<property name="show_sql">true</property>
<property name="hbm2dll.auto">update</property>
<mapping class="com.vasnavi.hibernate1stApp.Student"/>
</session-factory>
</hibernate-configuration>
mainClass
package com.vasnavi.hibernate1stApp;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hibernate First testing" );
Session session=new Configuration().configure().buildSessionFactory().getCurrentSession();
session.beginTransaction();
Student vishnu=new Student();
vishnu.setName("Vishnu Singh");
vishnu.setRollNo(21L);
session.save(vishnu);
session.getTransaction().commit();
}
}
Any one plz… help me