Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Wednesday, January 6, 2010

Simple hbm.xml file example

In hibernate environment there would be one class_name.hbm.xml file for each entity/object. This file will be used to map an object with the corresponding table in database.
For example - Person.hbm.xml would be used to map the table 'PERSON' in database which stores the Person objects.

Below is a simple example to write hbm.xml file

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="path.to.the.class.package.Person" table="PERSON">
<id name="lastName" type="string" unsaved-value="null" >
<column name="L_NAME" not-null="true"/>
<generator class="uuid.hex"/>
</id>

<property name="firstName" column="F_NAME" type="string"/>
<property name="age" column="AGE" type="integer"/>
<property name="city" column="CITY" type="string"/>
<property name="pinCode" column="PIN" type="integer"/>

</class>
</hibernate-mapping>

To make hibernate to use this file or to load this file in environment you need to specify the property for this in hibernate.cfg.xml as below

<mapping resource="path/to/the/hbmXml/file/Person.hbm.xml">

In next section we will see what is the next thing to learn to start the Hibernate development.

Tuesday, January 5, 2010

Simple hibernate.cfg.xml file example

Here I'm giving an example to right a simple hibernate.cfg.xml file.



<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<!-- jdbc connection properties -->
<property name="hibernate.connection.datasource">myDataSourceName</property>
<property name="connection.driver_class">properDriverForDatabase</property>
<property name="connection.url">databaseURL</property>
<!-- for example : jdbc:oracle:thin:@myHost:port:orcl-->
<property name="connection.username">user</property>
<property name="connection.password">password</property>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
<property name="transaction.factory_class">net.sf.hibernate.transaction.JDBCTransactionFactory</property>

<mapping resource="path/to/the/hbmXml/file/fileName.hbm.xml">

</session-factory>
</hibernate-configuration>

We will see in coming posts about the mandatory properties and optional properties and what they stands for.

SpringBoot: Features: SpringApplication

Below are a few SpringBoot features corresponding to SpringApplication StartUp Logging ·          To add additional logging during startup...