Hibernate example for Java with oracle.
Jars required:
-
hibernate-annotations-3.4.0.GA/lib/slf4j-api.jar
-
hibernate-annotations-3.4.0.GA/lib/dom4j.jar
-
hibernate-annotations-3.4.0.GA/lib/hibernate-commons-annotations.jar
-
hibernate-annotations-3.4.0.GA/lib/hibernate-core.jar
-
hibernate-annotations-3.4.0.GA/lib/test/slf4j-log4j12.jar
-
hibernate-annotations-3.4.0.GA/lib/test/antlr.jar
-
hibernate-annotations-3.4.0.GA/lib/test/asm.jar
-
hibernate-annotations-3.4.0.GA/lib/test/asm-attrs.jar
-
hibernate-annotations-3.4.0.GA/lib/test/commons-collections.jar
-
hibernate-annotations-3.4.0.GA/lib/test/javassist.jar
-
hibernate-annotations-3.4.0.GA/lib/test/jta.jar
-
hibernate-annotations-3.4.0.GA/lib/test/junit.jar
-
hibernate-annotations-3.4.0.GA/lib/test/log4j.jar
-
app/oracle/product/10.2.0/server/jdbc/lib/ojdbc14.jar
-
app/oracle/product/10.2.0/server/jdbc/lib/ojdbc14_g.jar
1-13 we have downloaded from
hibernate.org, 14 and 15 are there in oracle home
directory, Imentioned the path so add all the above
in class path
Note: Please download both
hibernate core and annotations. from
www.hibernate.org
Firrst Example: There is no order
of sequence I followed these number are just count
for the number of things we need todo.
1) Hibernate.cfg.xml file
<? 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="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property
name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property
name="hibernate.connection.username">hr</property>
<property
name="hibernate.connection.password">hr</property>
<property
name="hibernate.connection.pool_size">11</property>
<property
name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property
name="show_sql">true</property>
<property
name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files
-->
<mapping
resource="message.xml"/>
</ session-factory>
</ hibernate-configuration>
2) Bean configuration file "message.xml"
<?xml
version="1.0"?>
<!DOCTYPE
hibernate-mapping
PUBLIC
"-//Hibernate/Hibernate Mapping DTD
3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class
name="org.hibernate.Message"
table="Messages">
<id
name="siNo"
type="int"
column="SINO"
>
<generator
class="assigned"/>
</id>
<property
name="message">
<column
name="MESSAGE"/>
</property>
</class>
</ hibernate-mapping>
3) Bean file:
package
org.hibernate;
public
class
Message {
private
int
siNo;
private
String Message;
public
String getMessage() {
return
Message;
}
public
void
setMessage(String message) {
Message
= message;
}
public
int
getSiNo() {
return
siNo;
}
public
void
setSiNo(int
siNo) {
this.siNo
= siNo;
}
}
4) Run the commit using a java file
package
org.hibernate;
import
org.hibernate.Session;
import
org.hibernate.SessionFactory;
import
org.hibernate.cfg.Configuration;
// Darur Venugopal
public
class
HibDemo {
public
static
void
main(String[] args) {
Session session =
null;
try
{
// This step will
read hibernate.cfg.xml
// and prepare
hibernate for useSessionFactory sf = new
Configuration()
SessionFactory sessionFactory =
new
Configuration().configure().buildSessionFactory();
System. out.println("111111111111111");
session = sessionFactory.openSession();
System. out.println("ABCD");
// Create new
instance of Contact and set
Message message =
new
Message();
//message.setSiNo(2);
message.setMessage( "HELLO
WORLD");
session.save(message);
System. out.println("Done");
}
catch
(Exception e) {
System. out.println(e.getMessage());
}
finally
{
// Actual contact
insertion will happen at this step
session.flush();
session.close();
// Above two lines are must
}
}
}
Hibernate's first example for Java with oracle.
|