Home  >  Hibernate

Example of @Temporal in Hibernate

By Arvind Rai, May 12, 2013
@Temporal is used in hibernate entity with a entity variable. @Temporal defines the TemporalType. TemporalType can be like TemporalType.TIMESTAMP, TemporalType.DATE and TemporalType.TIME. In our example I have used TemporalType.TIMESTAMP.

User.java
package com.concretepage.persistence;
import java.util.Calendar;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="user")  
public class User {
	@Id
	private int id; 
		
	@Column(name="name")  
	private  String name;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="create_date")
	private Calendar createDate;

	public int getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public Calendar getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Calendar createDate) {
		this.createDate = createDate;
	}
}
 


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.User"/>
            
   </session-factory>
</hibernate-configuration>
 


HibernateUtil.java
 package com.concretepage.util;
import java.util.Calendar;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import com.concretepage.persistence.User;

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();
		User user= new User();
		user.setId(1);
		user.setName("Ram");
		
		user.setCreateDate(Calendar.getInstance());
		session.save(user);
		session.getTransaction().commit();
		session.close();
	}
}
 
Output
Example of @Temporal in Hibernate
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2018 concretepage.com | Privacy Policy | Contact Us