//In this example we will see about the generators using annotations
UserDetails.java
UserAccess.java
NOTE:
@GeneratedValue(strategy=GenerationType.AUTO) is better to use which supports all the databases
UserDetails.java
package
com.hibernate.model;
import
javax.persistence.Entity;
import
javax.persistence.GeneratedValue;
import
javax.persistence.GenerationType;
import
javax.persistence.Id;
import
javax.persistence.Table;
@Entity
@Table(name
= "userdet")
public
class
UserDetails {
@Id
//1.
@GeneratedValue(strategy=GenerationType.AUTO)
//
The above annotation prints the id value from 1,2........ if it is
AUTO
//
2.@GeneratedValue(strategy=GenerationType.IDENTITY)
/*
*
Oracle doesn't support Identity it will print exception as
*
org.hibernate.MappingException: org.hibernate.dialect.OracleDialect
does
*
not support identity key generation
*/
/*3.@GeneratedValue(strategy=GenerationType.SEQUENCE)
The
above annotations takes the value where we stopped previously and it
prints*/
/*@GeneratedValue(strategy=GenerationType.TABLE)
The
id value starting from 32768,32769,.......*/
private
Integer userId;
public
Integer getUserId() {
return
userId;
}
public
void
setUserId(Integer userId) {
this.userId
= userId;
}
public
String getUserName() {
return
userName;
}
public
void
setUserName(String userName) {
this.userName
= userName;
}
private
String userName;
}
UserAccess.java
package
com.hibernate;
import
org.hibernate.Session;
import
org.hibernate.SessionFactory;
import
org.hibernate.cfg.Configuration;
import
com.hibernate.model.UserDetails;
public
class
UserAccess {
public
static
void
main(String[] args) {
//
TODO
Auto-generated method stub
UserDetails user = new
UserDetails();
user.setUserName("karthik
reddy");
UserDetails user2 = new
UserDetails();
user2.setUserName("karthik");
SessionFactory factory = new
Configuration().configure()
.buildSessionFactory();
Session session =
factory.openSession();
session.beginTransaction();
session.save(user);
session.save(user2);
session.getTransaction().commit();
session.close();
factory.close();
}
}
hibernate.cfg.xml
<?xml
version='1.0'
encoding='utf-8'?>
<!DOCTYPE
hibernate-configuration PUBLIC
"-//Hibernate/Hibernate
Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection
settings -->
<property
name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property
name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property
name="connection.username">system</property>
<property
name="connection.password">system</property>
<!-- JDBC connection pool
(use the built-in) -->
<property
name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property
name="dialect">org.hibernate.dialect.OracleDialect</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">update</property>
<mapping
class="com.hibernate.model.UserDetails"
/>
</session-factory>
</hibernate-configuration>
NOTE:
@GeneratedValue(strategy=GenerationType.AUTO) is better to use which supports all the databases
No comments:
Post a Comment