how to save creation and updation timestamp with hibernate.
for explnation watch the video :
Object time stamping feature of hibernate
=========================================
->this feature keeps track of the object/record is saved/inserted initially
later it also keeps track of when record/object is lastly updated/modified
=>Object versioning feature keeps track how many times the object/record is modified using
Hibernate persistence logic where Object timestampping feature keeps track when exactly the Object/record
is inserted and lastly updated.
useCases for timeStampping
==========================
->Keeping the track of when the password lastly changed
->keeping the track of when stock market share values lastly updated/modified
->Keeping the track of when last tx happend in the bank account
In annotation driven HB programming
====================================
->we can enable timestamp,version features at a time on single entity class
-> in timestamp feature we can take seperate col/property holding the object/record
insertion date and time and another seperate property holding object/record last updation
date and time
@CreationTimeStamp
@UpdationTimeStamp
Directory Structure
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>Bank</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Bank</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.8.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
</project>
Utility Class
HibernateUtil.java
package com.test.util;
import java.util.Properties;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import com.test.entity.Account;
public class HibernateUtil {
static SessionFactory factory = null;
static {
Configuration cfg = new Configuration();
Properties props = new Properties();
props.put(Environment.URL, "jdbc:mysql://localhost:3306/new");
props.put(Environment.USER, "root");
props.put(Environment.PASS, "root");
props.put(Environment.SHOW_SQL, true);
props.put(Environment.HBM2DDL_AUTO, "update");
props.put(Environment.FORMAT_SQL, true);
cfg.setProperties(props);
cfg.addAnnotatedClass(Account.class);
factory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
}
Comments
Post a Comment