- Create the project directory structure.
- Create the persistence classes.
Person.java
[java]package com.itoolsinfo;
public class Person
{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
[/java]
Here created the class Person with the variables id and name, set and get methods are performed on this variables. Set and Get methods are a pattern of data encapsulation. Instead of accessing class member variables directly, one can define get methods to access these variables, and set methods to modify them.
CardDetails.java
[java]package com.itoolsinfo;
public class CardDetails
{
private int id;
private String cardName;
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCardName() {
return cardName;
}
public void setCardName(String cardName) {
this.cardName = cardName;
}
}
[/java]
Hibernate Many to One, Here created the class CardDetails with the variables id, cardName, person. Set and Get operations have performed on this variables.
- Map the persistence classes in mapping file.
person.hbm.xml
[xml]
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
<hibernate-mapping>
<class name=”com.itoolsinfo.Person” table=”person”>
<generator class=”native”/>
</id>
<property name=”name” />
</class>
</hibernate-mapping>
[/xml]
The generator classs ubelement of id utilized to produce the unique identifier for the objects of persistence class. There are numerous generator classes characterized in the Hibernate Framework. All the generator classes actualizes the org.hibernate.id.IdentifierGenerator interface. Generator class
native uses identity, sequence or hilo depending on the database vendor.
carddetails.hbm.xml
[xml]
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.itoolsinfo.CardDetails" table="carddetails">
<id name="id">
<generator class="native"/>
</id>
<property name="cardName" />
<many-to-one name="person" class="com.itoolsinfo.Person" column="cid" cascade="all"/>
</class>
</hibernate-mapping>
[/xml]
The generator class subelement of id utilized to produce the unique identifier for the objects of persistence class. There are numerous generator classes characterized in the Hibernate Framework. All the generator classes actualizes the
org.hibernate.id.IdentifierGenerator interface.
- Configure the mapping files in Configuration file.
hibernate.cfg.xml
[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.driver_class">oracle.jdbc.driver.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>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<mapping resource="carddetails.hbm.xml"/>
<mapping resource="person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
[/xml]
Properties |
Description |
hibernate.connection.driver_class |
The JDBC driver class. |
hibernate.dialect |
This property makes Hibernate generate the suitable SQL for the picked database. |
hibernate.connection.url |
The JDBC URL to the database instance. |
hibernate.connection.username |
The database username. |
hibernate.connection.password |
The database password. |
hibernate.connection.pool_size |
Limits the number of connections waiting in the Hibernate database connection pool. |
hibernate.connection.autocommit |
Allows autocommit mode to be used for the JDBC connection. |
- Create the SPLessons class and store the POJO class objects to perform the database operations.
SPLessons.java
[java]package com.itoolsinfo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class SPLessons
{
public static void main(String args[])
{
SessionFactory factory=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
Session session=factory.openSession();
Person person=new Person();
person.setName(" john ");
CardDetails carddetails=new CardDetails();
carddetails.setCardName("AadharCard");
carddetails.setPerson(person);
CardDetails carddetails2=new CardDetails();
carddetails2.setCardName("voterId");
carddetails2.setPerson(person);
Transaction transaction=session.beginTransaction();
session.save(carddetails);
session.save(carddetails2);
transaction.commit();
session.close();
}
}
[/java]
Output: See the Output in command prompt.
See the Output in Database table using following command.
[sql]select * from person;[/sql]
[sql]select * from carddetails[/sql]