A basic hibernate mvc tutorial which connect to Oracle database 12c and do some DDL and DML operation using hibernate framework. I have used Oracle database 12c release 1 on Red Hat Linux 6.4 64 bit and used Eclipse Helios 64 bit and Hibernate Framework 3.6.4. I am uploading the project and sharing the link.
The main contents of the project is
1. hibernate.cfg.xml, 2. UserDetails.java and 3. OracleTest.java.
It is not possible to show the details steps of creating the project for that you need to see the video on youtube.
Download Hibernate 3.6.4-final
Goto project properties, select Java build path, Click on Add library, Select Add user library, Click on New, Give name to the library, click on add jar files. and select these jars.
Now download ojdbc6.jar.zip and extract it to your desired location. Click on Add external jars and select ojdbc.jar. This jar is required for the jdbc driver for oracle connection.
Hibernate.cfg.xml is required to setup the connection to the database, configuration which initiates the connection, build the session, transactional savepoint and commit DDL and DML operation.
Create this three files inside src folder. Put Hibernate.cfg.xml in src root and 2 java files into the package you create.
Sample 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>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@192.168.0.109:1521:orcl</property>
<property name="connection.username">hrd</property>
<property name="connection.password">hrd</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.use_outer_join">false</property>
<!-- <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property> -->
<mapping class="org.subhro.hibernate.UserDetails"/>
</session-factory>
</hibernate-configuration>
UserDetails.java
package org.subhro.hibernate;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class UserDetails {
@Id
private int UserId;
private String UserName;
/**
* @param userId the userId to set
*/
public void setUserId(int userId) {
UserId = userId;
}
/**
* @return the userId
*/
public int getUserId() {
return UserId;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
UserName = userName;
}
/**
* @return the userName
*/
public String getUserName() {
return UserName;
}
}
OracleTest.java
package org.subhro.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class OracleTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
UserDetails usr = new UserDetails();
usr.setUserId(5);
usr.setUserName("Fourth Record");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(usr);
session.getTransaction().commit();
}
}
#java #hibernateframework #oracledatabase #Oracle #Database
The main contents of the project is
1. hibernate.cfg.xml, 2. UserDetails.java and 3. OracleTest.java.
It is not possible to show the details steps of creating the project for that you need to see the video on youtube.
Download Hibernate 3.6.4-final
Goto project properties, select Java build path, Click on Add library, Select Add user library, Click on New, Give name to the library, click on add jar files. and select these jars.
Now download ojdbc6.jar.zip and extract it to your desired location. Click on Add external jars and select ojdbc.jar. This jar is required for the jdbc driver for oracle connection.
Hibernate.cfg.xml is required to setup the connection to the database, configuration which initiates the connection, build the session, transactional savepoint and commit DDL and DML operation.
Create this three files inside src folder. Put Hibernate.cfg.xml in src root and 2 java files into the package you create.
Sample 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>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@192.168.0.109:1521:orcl</property>
<property name="connection.username">hrd</property>
<property name="connection.password">hrd</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.use_outer_join">false</property>
<!-- <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property> -->
<mapping class="org.subhro.hibernate.UserDetails"/>
</session-factory>
</hibernate-configuration>
UserDetails.java
package org.subhro.hibernate;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class UserDetails {
@Id
private int UserId;
private String UserName;
/**
* @param userId the userId to set
*/
public void setUserId(int userId) {
UserId = userId;
}
/**
* @return the userId
*/
public int getUserId() {
return UserId;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
UserName = userName;
}
/**
* @return the userName
*/
public String getUserName() {
return UserName;
}
}
OracleTest.java
package org.subhro.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class OracleTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
UserDetails usr = new UserDetails();
usr.setUserId(5);
usr.setUserName("Fourth Record");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(usr);
session.getTransaction().commit();
}
}
No comments:
Post a Comment