SpLessons.java
package com.itoolsinfo; public class SpLessons { private int empId; private String empName; private String city; public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
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.
<!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.iquickinfo.Splessons" table="splessons"> <id name="empId" column="eid"> <generator class="assigned"/> </id> <property name="empName" column="ename"/> <property name="city" column="city"/> </class> </hibernate-mapping>
The generator classsubelement 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.
<?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">root</property> <property name="connection.password">root</property> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="hibernate.hbm2ddl.auto">create</property> <property name="show_sql">true</property> <mapping resource="splessons.hbm.xml"/> </session-factory> </hibernate-configuration>
mysql.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">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/MySQL</property> <property name="connection.username">system</property> <property name="connection.password">system</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">create</property> <property name="show_sql">true</property> <mapping resource="splessons.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. |
Employess.java
package com.iquickinfo; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class Employees { public static void main(String[] args) { Configuration configuration1=new Configuration(); configuration1.configure("oracle.cfg.xml"); Configuration configuration2=new Configuration(); configuration2.configure("mysql.cfg.xml"); SessionFactory factory1=configuration1.buildSessionFactory(); SessionFactory factory2=configuration2.buildSessionFactory(); Session oraclesession=factory1.openSession(); Session mysqlsession=factory2.openSession(); Splessons splessons=new Splessons(); splessons.setEmpId(414); splessons.setEmpName(" John "); splessons.setCity("Hyderabad"); Transaction transaction1=oraclesession.beginTransaction(); oraclesession.save(splessons); transaction1.commit(); Transaction transaction2=mysqlsession.beginTransaction(); mysqlsession.save(splessons); transaction2.commit(); oraclesession.close(); mysqlsession.close(); factory1.close(); factory2.close(); } }
SessionFactory is an interface, which is accessible in “org.hibernate” package.Session factory is long live multithreaded object. Typically one session manufacturing plant ought to be made for one database. When the developer have different databases in your application, developer ought to make numerous SessionFactory object.