Address.java
package com.itoolsinfo; public class Address { private String city, village, street; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getVillage() { return village; } public void setVillage(String village) { this.village = village; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } }
Here the developer created the class Address and also given the variables city, village, street. 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.
Employee.java
package com.itoolsinfo; public class Employee { private int id; private String name; private Address address; 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; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
Here the developer created the class Employee and also created the variables id, name, address. The developer used set and get methods to use the 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.
<?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.Employee”table=”employee”> <id name=”id”column=”id”> <generator class=”assigned”> </generator> </id> <property name=”name”length=”14”> <component name=”address” class=”com.itoolsinfo.Address” > <property name=”village” length=”14”/> <property name=”street” length=”14”/> <property name=”city” length=”14”> </property> </component> </class> </hibernate-mapping>
The generator class sub element 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.
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="employee.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.java
package com.itoolsinfo; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class SPLessons { public static void main(String args[]) { Session session=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory().openSession(); Address address1=new Address(); address1.setStreet("BN.Reddy nagar"); address1.setCity("hyderabad"); address1.setVillage("hariharapuram"); Employee employee=new Employee(); employee.setId(1); employee.setName(" Jhon "); employee.setAddress(address1); Transaction transaction=session.beginTransaction(); session.save(employee); transaction.commit(); session.close(); //String str=JOptionPane.showInputDialog("Enter name:user1"); //splessons.setName("str"); // transaction.commit(); // session.close(); // factory.close(); } }
Here created the class SPLessons to perform data base operations on the available code. 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.
Output:
Output will be as follows, where enter the values into the columns.
Data will be selected from the table as following command.
select * from employee;
Tables will be created in the database as follows.