- Transient: The object which is not in hibernate session yet.
- Persistent: Object is in persistent state which means object is available in hibernate session.
- Detached: Object is in Detached state, after session close object goes into detached state.
HibernateTest.java
package org.yash.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.yash.dto.UserDetails;
public class HibernateTest {
public static void main(String
args[]) {
/* Here object
is in transient state which means object is not in hibernate session yet
*/
UserDetails user
= new UserDetails();
user.setUserName("Test
User");
SessionFactory
sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session =
sessionFactory.openSession();
session.beginTransaction();
session.save(user);
/* Object is
in persistent state which means object is available in hibernate session
*/
user.setUserName("User
Update");
user.setUserName("User Update
Again"); // This record will be update in table
session.getTransaction().commit();
session.close();
/* Object is
in Detached state, after session close object goes into detached state */
user.setUserName("Update
user after session close");
}
}
UserDetails.java
package org.yash.dto;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="USER_DETAILS")
public class UserDetails {
/* @Id says
"userId" is primary key */
@Id @GeneratedValue
(strategy=GenerationType.AUTO)
private int userId;
private String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String
getUserName() {
return userName;
}
public void
setUserName(String userName) {
this.userName = userName;
}
}
hibernate.cfg.xml
<?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>
<property name="connection.driver_class">
org.postgresql.Driver
</property>
<property name="connection.url">
jdbc:postgresql://localhost:5433/hibernatedb
</property>
<property name="connection.username">postgres</property>
<property name="connection.password">admin</property>
<!-- JDBC
connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL
dialect -->
<property name="dialect">
org.hibernate.dialect.PostgreSQLDialect
</property>
<!-- Enable
Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Echo
all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop
and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Names the annotated entity
class -->
<mapping class="org.yash.dto.UserDetails"/>
</session-factory>
</hibernate-configuration>
Happy to found this blog. Good Post!. It was so good to read and useful to improve my knowledge as updated one, keep blogging. Hibernate Training in Electronic City
ReplyDeleteJava Training in Electronic City