Wednesday, October 11, 2017

Hibernate Declarative Approach

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>
        <!-- Database connection properties - Driver, URL, user, password -->
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@172.20.1.47:1521:XE</property>
        <property name="hibernate.connection.username">DLAUDIT</property>
        <property name="hibernate.connection.password">DLAUDIT</property>
        <!-- Connection Pool Size -->
        <property name="hibernate.connection.pool_size">10</property>
       
        <!-- org.hibernate.HibernateException: No CurrentSessionContext configured! -->
        <property name="hibernate.current_session_context_class">thread</property>
       
        <!-- Outputs the SQL queries, should be disabled in Production -->
        <property name="hibernate.show_sql">true</property>
        <property name="format_sql">true</property>
       
        <!-- Dialect is required to let Hibernate know the Database Type, MySQL, Oracle etc
            Hibernate 4 automatically figure out Dialect from Database Connection Metadata -->
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>

        <!-- mapping file, we can use Bean annotations too -->
        <mapping resource="resorces/Account.hbm.xml" />
    </session-factory>
</hibernate-configuration>


Account.hbm.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.hiberante.api.declartive.Account" table = "ACCOUNT">
  <id name="id" column="ID" type="java.lang.Integer"></id>
  <property name="gui" column="GUI" type="java.lang.String"/>
  <property name="firstName" column="FIRST_NAME" type="java.lang.String"/>
  <property name="lastName" column="LAST_NAME" type="java.lang.String"/>
  <property name="age" column="AGE" type="java.lang.Integer"/>
  </class>

 </hibernate-mapping>