Create a Java Project and give
name
Steps:
1.Click
file in the menu-bar / New / Project .
2.Select
Java / Java Project
3. Give name for project
4. Click Finish
Add
Hibernate Jar files
To
build Hibernate Application, we need
1.
Persistent classes, for representing database
entity tables.
2.
Mapping files.
3.
Hibernate configuration settings. This will
show how to connect and manage the
database.
Create the persistent class for entity class
name.
Public class className
{
private int
ID;
private String firstName;
private String lastName;
public int
getID() {
return ID;
}
public void
setID(int id)
{
ID = id;
}
public String
getFirstName()
{
return
firstName;
}
public void
setFirstName(String firstName)
{
this.firstName
= firstName;
}
public String
getLastName()
{
return lastName;
} public void
setLastName(String lastName)
{
this.lastName = lastName;
}
}
Create a mapping for for table
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="Lecturer"
table="TableName">
<id name="ID" type="int">
<generator
class="increment"/> </id>
<property
name="firstName" column="FirstName"
type="string"/>
<property name="lastName"
column="LastName" type="string"/>
</class> </hibernate-mapping>
Create 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.url">jdbc_URL</property>
<property
name="connection.username">jdbc_Username</property>
<property
name="connection.password">jdbc_Password</property>
<property
name="connection.driver_class"> sun.jdbc.odbc.JdbcOdbcDriver
</property> <property name="dialect"> org.hibernate.dialect.SQLServerDialect
</property> <property
name="current_session_context_class">thread</property>
<!-- ___ Defining the Mapping Files ___________
-->
<mapping resource="mappingfilename.xml"
/> </session-factory> </hibernate-configuration>
import org.hibernate.Session;
import
org.hibernate.SessionFactory;
import
org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class SimpleTest
{
public static
void main(String[] args)
{
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
Session
session = sessionFactory.getCurrentSession();
Transaction transx = session.beginTransaction();
classname refclass = new classname();
refclass.setFirstName("srinivas"); refclass.setLastName(“N");
session.save(refclass);
transx.commit(); System.out.println ("The
" + refclass.getFirstName()+ " " + refclass.getLastName()+"
is successfully added to your database"); } }
0 coment�rios:
Post a Comment