Hibernate Configuration
WE can configure hibernate by adding the .jar(s) files to our java application
Mapping is a mechnism for establishing synchronization between object and row of the table.
Mapping can be done using 2 ways:
Syntax: for mapping with XML file:
<? xml version="1.0">
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="POJO class name " table="table name">
< id name ="variable name" column=" primary key column name"
type="java/hibernate type"> <generate class="increment"></generator>
</id>
<property name="variable2 name" column="column name in database"
type="java/hibernate type"/>
<property name="variable2 name" column="column name in database" type="java/hibernate type"/>
</class>
</hibernate-mapping>
<id> element: Each instance is a row in the table. We need unique identifier property to map as primary key in the table to identify each row uniquely. The id element is the declaration of the identifier property.
<generator> element: The nested generator element specifies the identifier generation strategy. Hibernate supports different identifier generation strategy algorithm. These strategies depends on configured database dialect.
<property> element: specifies properties in a class other than id property. The name attribute of the property element tells Hibernate which getter and setter methods to use
configuration file( hibernate.cfg.xml)
configuration file is loaded in to an hibernate application when we run the hibernate application. Configuration file should have 3 kinds of properties.
<session-factory >
<!--connection Properties--->
<property name="connection.driver class">Driver class name
</property>
<property name="connection.url">URL</property>
<property name="connection.user">user</property>
<property name="connection.password">password</property>
<!--END of connection properties-->
<!--Hibernate properties-->
<property name="show_sql">true/false</property>
<property name="dialet">Database dialet class</property>
<property name="hbm2dd1.auto">create/update or whatever</property>
<!--END of Hibernate Properties-->
<!--mapping file names-->
<mapping resource="hbm file1 name.xml"/>
<mapping resource="hbm file2 name.xml"/>
<!--END of Related to the mapping-->
</session-factory>
</hibernate-configuration>
WE can configure hibernate by adding the .jar(s) files to our java application
- Required files of Hibernate Application. (Hibernate requires two XML files)
- mapping file
- configuration file (Hibernate.cfg.xml)
Mapping is a mechnism for establishing synchronization between object and row of the table.
Mapping can be done using 2 ways:
- XML (Mapping XML file)
- Annotations
Syntax: for mapping with XML file:
<? xml version="1.0">
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="POJO class name " table="table name">
< id name ="variable name" column=" primary key column name"
type="java/hibernate type"> <generate class="increment"></generator>
</id>
<property name="variable2 name" column="column name in database"
type="java/hibernate type"/>
<property name="variable2 name" column="column name in database" type="java/hibernate type"/>
</class>
</hibernate-mapping>
- we will include Hibernate DTD for auto complesion of XML mapping elements and attributes in our editor or IDE.
- The DTD file is included in hibernate-core.jar
<id> element: Each instance is a row in the table. We need unique identifier property to map as primary key in the table to identify each row uniquely. The id element is the declaration of the identifier property.
<generator> element: The nested generator element specifies the identifier generation strategy. Hibernate supports different identifier generation strategy algorithm. These strategies depends on configured database dialect.
<property> element: specifies properties in a class other than id property. The name attribute of the property element tells Hibernate which getter and setter methods to use
configuration file( hibernate.cfg.xml)
configuration file is loaded in to an hibernate application when we run the hibernate application. Configuration file should have 3 kinds of properties.
- connection properties
- Hibernate properties
- Mapping file properties
- we may have one or more configuration files based on number of databases we are using for getting date into our application.
- if we want to populate data from two databases like Oracle, MySql then we must create two configuration files. i.e: Number of configuration files in the hibernate application =Number of databases used in application.
<session-factory >
<!--connection Properties--->
<property name="connection.driver class">Driver class name
</property>
<property name="connection.url">URL</property>
<property name="connection.user">user</property>
<property name="connection.password">password</property>
<!--END of connection properties-->
<!--Hibernate properties-->
<property name="show_sql">true/false</property>
<property name="dialet">Database dialet class</property>
<property name="hbm2dd1.auto">create/update or whatever</property>
<!--END of Hibernate Properties-->
<!--mapping file names-->
<mapping resource="hbm file1 name.xml"/>
<mapping resource="hbm file2 name.xml"/>
<!--END of Related to the mapping-->
</session-factory>
</hibernate-configuration>
No comments:
Post a Comment