Auto Increment Column Name in Hibernate

Model Class


/**
 *
 */
package com.learnjavabyanand;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * @author ananddw
 *
 */
@Entity
@Table(name="student_details")
public class Student {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int studentrollnumber;

@Column(name="student_name")
private String studentName;

public int getStudentrollnumber() {
return studentrollnumber;
}

public void setStudentrollnumber(int studentrollnumber) {
this.studentrollnumber = studentrollnumber;
}

public String getStudentName() {
return studentName;
}

public void setStudentName(String studentName) {
this.studentName = studentName;
}




}


Client Class: 

package com.learnjavabyanand;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
/**
 * @author ananddw
 *
 */
public class StudentClient {

public static void main(String[] args) {
// TODO Auto-generated method stub

Student student= new Student();
student.setStudentName("Anand Dwivedi");
Student student2= new Student();
student2.setStudentName("Ajeet Singh");
Student student3= new Student();
student3.setStudentName("Antrish Mishra");
try{
SessionFactory factory=new AnnotationConfiguration().configure().buildSessionFactory();
Session session=factory.openSession();
session.beginTransaction();
session.save(student);
session.save(student2);
session.save(student3);
session.getTransaction().commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
}

}


hibernate Configuration file 


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <session-factory>
  <!--  Postgres DB Details  -->
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1:5432/hibernate</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password">password</property>
      <!--  Postgres DB Details End  -->
    
    <!-- Postgres Dialect has information of list of  queries avil in Postgres DB -->
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <!-- Postgres Dialect End -->
    
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
    <!--  Echo show Query  -->
    <property name="show_sql">true</property>
     <!--  Echo show Query End  -->
    
    <property name="format_sql">true</property>
    
    <!--  Create every time table and insert Data -->
    <property name="hibernate.hbm2ddl.auto">create</property>
    <!--  Create every time table and insert Data End -->
    
    <!--  Define Package for mapping class -->
    
    <mapping class="com.learnjavabyanand.Student"/>
    <!--  Define Package for mapping class End -->
   
  </session-factory>
</hibernate-configuration>




Snapshot : 



PS: Add all the required Jars 

Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Java Design pattern

Map class field to map