To set up a simple hibernate project using XML for the configurations you need 3 files, hibernate.cfg.xml, a POJO for each entity, and a EntityName.hbm.xml for each entity. Here is an example of each using MySQL:
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="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/DBSchemaName
</property>
<property name="hibernate.connection.username">
testUserName
</property>
<property name="hibernate.connection.password">
testPassword
</property>
<!-- List of XML mapping files -->
<mapping resource="HibernatePractice/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
DBSchemaName, testUserName, and testPassword would all be replaced. Make sure to use the full resource name if it is in a package.
Employee.java
package HibernatePractice;
public class Employee {
private int id;
private String firstName;
private String middleName;
private String lastName;
public Employee(){
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getMiddleName(){
return middleName;
}
public void setMiddleName(String middleName){
this.middleName = middleName;
}
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
}
Employee.hbm.xml
<hibernate-mapping>
<class name="HibernatePractice.Employee" table="employee">
<meta attribute="class-description">
This class contains employee information.
</meta>
<id name="id" type="int" column="empolyee_id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="middleName" column="middle_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
</class>
</hibernate-mapping>
Again, if the class is in a package use the full class name packageName.className.
After you have these three files you are ready to use hibernate in your project.