Hibernate simple program:
Required jars:
hibernate-distribution-3.6.10.Final\hibernate3.jar
hibernate-distribution-3.6.10.Final\lib\required\antlr-2.7.6.jar
hibernate-distribution-3.6.10.Final\lib\required\commons-collections-3.1.jar
hibernate-distribution-3.6.10.Final\lib\required\dom4j-1.6.1.jar
hibernate-distribution-3.6.10.Final\lib\required\javassist-3.12.0.GA.jar
hibernate-distribution-3.6.10.Final\lib\required\jta-1.1.jar
hibernate-distribution-3.6.10.Final\lib\required\slf4j-api-1.6.1.jar
hibernate-distribution-3.6.10.Final\lib\jpa\hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-distribution-3.6.10.Final\lib\bytecode\javassist\javassist-3.12.0.GA.jar
ojdbc14.jar
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>
UserDetails.java
package
com.hibernate.model;
import
javax.persistence.Column;
import
javax.persistence.Entity;
import
javax.persistence.Id;
@Entity(name="USER_DETAILS")
public
class
UserDetails {
@Id
@Column(name="USER_ID")
private
Integer userId;
public
Integer getUserId() {
return
userId;
}
public
void
setUserId(Integer userId) {
this.userId
= userId;
}
@Column(name="USER_NAME")
public
String getUserName() {
return
userName + " from getter";
}
public
void
setUserName(String userName) {
this.userName
= userName;
}
private
String userName;
}
UserAccess.java
package
com.hibernate;
import
java.util.Scanner;
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();
Scanner scan = new
Scanner(System.in);
System.out.println("Enter
the id");
int
userId = scan.nextInt();
System.out.println("Enter
the name");
String userName = scan.next();
user.setUserId(userId);
user.setUserName(userName);
SessionFactory factory = new
Configuration().configure()
.buildSessionFactory();
Session session =
factory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
session.close();
factory.close();
}
}
sample
output:
Enter
the id
1
Enter
the name
karthik
Hibernate: insert into USER_DETAILS (userName, USER_ID) values (?, ?)
No comments:
Post a Comment