@GeneratedValue with strategy=GenerationType.TABLE in Hibernate

By Arvind Rai, May 16, 2013
In the case of GenerationType.TABLE the column values is filled by a table. This will be separate table. The syatx to use GenerationType.TABLE can be
 @GeneratedValue(strategy=GenerationType.TABLE) 
 
In this case the the generation table is created by hibernate with name hibernate_sequences and there will be two column sequence_name and sequence_next_hi_value
And second one can be
@GeneratedValue(strategy=GenerationType.TABLE, generator="course")
@TableGenerator(
    name="course",
    table="GENERATOR_TABLE",
    pkColumnName = "key",
    valueColumnName = "next",
    pkColumnValue="course",
    allocationSize=30
)

 
We have defined table named GENERATOR_TABLE. While setting value to the column the current value will also be saved in generator table. Find the example below.

EduCourse.java
package com.concretepage.persistence;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

@Entity
@Table(name = "edu_course")
public class EduCourse {
	@Id
	@GeneratedValue(strategy=GenerationType.TABLE, generator="course")
	@TableGenerator(
		    name="course",
		    table="GENERATOR_TABLE",
		    pkColumnName = "key",
		    valueColumnName = "next",
		    pkColumnValue="course",
		    allocationSize=30
		)
	private int id;
	@Column(name = "course_name")
	private String courseName;
	@Column(name = "duration")
	private int duration;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getCourseName() {
		return courseName;
	}
	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}
	public int getDuration() {
		return duration;
	}
	public void setDuration(int duration) {
		this.duration = duration;
	}
}
 


HibernateUtil.java
package com.concretepage.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import com.concretepage.persistence.EduCourse;

public class HibernateUtil {
	private static final SessionFactory concreteSessionFactory;
	static {
		try {

			concreteSessionFactory = new AnnotationConfiguration().configure()
					.buildSessionFactory();
		} catch (Throwable ex) {
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static Session getSession() throws HibernateException {
		return concreteSessionFactory.openSession();
	}

	public static void main(String... args) {
		Session session = getSession();
		session.beginTransaction();

		EduCourse course = new EduCourse();
		course.setCourseName("B.Sc");
		course.setDuration(3);
		session.save(course);
		
		session.getTransaction().commit();
		session.close();
	}
}
 


hibernate.cfg.xml
 <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.url">
    jdbc:mysql://localhost:3306/hibernate</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"></property>
    <property name="hibernate.connection.pool_size">10</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping class="com.concretepage.persistence.EduCourse"/>
                
   </session-factory>
</hibernate-configuration>
 


edu_course Table
 CREATE TABLE `edu_course` (
	`id` INT(11) NOT NULL AUTO_INCREMENT,
	`course_name` VARCHAR(255) NULL DEFAULT NULL,
	`duration` INT(11) NULL DEFAULT NULL,
	PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;

 
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us