To put the one-to-one relationship with a Foreign Key column in the child table, duplicate values and null values must not be allowed in the Primary Key column. Map a Foreign Key column in a mapping file, after Configuring the <>
tag with unique=”true” & not-null = true attributes.
Person.java
package com.itoolsinfo; public class Person { private int id; private String address; private Pancard pancard; public Pancard getPancard() { return pancard; } public void setPancard(Pancard pancard) { this.pancard = pancard; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
Here the developer created the class Person and given their variables such as id, address, pancard. Set and Get methods are used to set and get person variables.
Pancard.java
package com.itoolsinfo; public class Pancard { private int pancardNumber; private String name; private Person person; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPancardNumber() { return pancardNumber; } public void setPancardNumber(int pancardNumber) { this.pancardNumber = pancardNumber; } }
Here the developer created the class Pancard and given their variables such as pancardNumber, name. 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. As every one know that this keyword is used to refer the current object.
person.hbm.xml
<?xml version='1.0' encoding='UTF-8'?> <!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” > <id name=”id” column=”personId”> <generator class=”increment”> </generator> </id> <property name=”address”/> <one-to-one name=”pancard” /> </class> </hibernate-mapping>
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.
pancard.hbm.xml
<?xml version='1.0' encoding='UTF-8'?> <!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.Pancard”table=” pancard” > <id name=”pancardNumber” column=”pancardNumber”> <generator class=”foreign”> <param name=”property”>person>/param> </generator> </id> <property name=”name”/> <one-to-one name=”person” cascade=”all”/> </class>
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.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=”person.hbm.xml”/> <mapping resource=”pancard.hbm.xml”/> </session-factory> </hibernate-configuration>
SPLessons.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().buildSessionFactory(); Session session=factory.openSession(); Transaction transaction=session.beginTransaction(); Person person=new Person(); person.setAddress("itoolsinfo"); Pancard pancard=new Pancard(); pancard.setName("Jhon"); person.setPancard(pancard); pancard.setPerson(person); session.save(pancard); session.save(person); transaction.commit(); session.close(); } }
Create the SPLessons class and store the POJO class objects to perform the Database operations. Application acquires session objects from Session Factory. SessionFactory is for the most part arranged as Singleton in application, SessionFactory stores produce SQL statements and other mapping metadata that Hibernate utilizes at runtime.
<>
tag with unique=”true” & not-null = true attributes. Person.java
package com.itoolsinfo; public class Person { private int id; private String address; private Pancard pancard; public Pancard getPancard() { return pancard; } public void setPancard(Pancard pancard) { this.pancard = pancard; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
Here the developer created the class Person with the variables address and pancard. 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.
Pancard.java
package com.itoolsinfo; public class Pancard { private int id; private String name; private Person person; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
Here the developer created the class Pancard with address and person variables. This keyword is used to refer the current object.
person.hbm.xml
<?xml version='1.0' encoding='UTF-8'?> <!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="person1" > <id name="id"> <generator class="increment"/> </id> <property name="address"/> <many-to-one name="pancard" class="com.itoolsinfo.Pancard" cascade="all" unique="true" /> </class> </hibernate-mapping>
pancard.hbm.xml
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”> <!-- Generated by MyEclipse Hibernate Tools. <hibernate-configuration> <session-factory> <property name=”hbm2ddl.auto”>update</property> <property name=”dialect”>org.hibernate.dialect.Oracle9Dialect</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=”connection.driver_class”>oracle.jdbc.driver.OracleDriver</property> <mapping resource=”person.hbm.xml”/> <mapping resource=”pancard.hbm.xml”/> </session-factory> </hibernate-configuration>
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. |
SPLessons.hbm.xml
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(); Transaction transaction=session.beginTransaction(); Person person=new Person(); person.setAddress("itoolsinfo"); Pancard pancard=new Pancard(); pancard.setName("Jhon"); person.setPancard(pancard); pancard.setPerson(person); //session.save(pancard); session.save(person); transaction.commit(); session.close(); } }
Here the class SPLesson has been created to perform database operations.