Mapping Xml
file:-
An Object/relational mappings are usually defined in an XML document. This
mapping file instructs Hibernate — how to map the defined class or classes to
the database tables?
Syntax Of
Mapping xml:-
<hibernate-mapping>
<class name="POJO class name"
table="table name in database">
<id name="variable name"
column="column name in database"
type="java/hibernate type" />
<property name="variable1 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>
Configuration:-
Configuration is the file loaded into an hibernate
application when working with hibernate, this configuration file
contains 3 types of information..
- Connection
Properties
- Hibernate
Properties
- Mapping
file name(s)
We must create one configuration file for each database we are going to use,
suppose if we want to connect with 2 databases, like Oracle, MySql, then we must create 2
configuration files.
No. of databases we are using = That many
number of configuration files
Syntax
Of Configuration xml:
<hibernate-configuration>
<session-factory>
<!-- Related to the connection START
-->
<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>
<!-- Related to the connection END -->
<!-- Related to hibernate properties START
-->
<property name="show_sql">true/false</property>
<property name="dialet">Database
dialet class</property>
<property name="hbm2ddl.auto">create/update
or what ever</property>
<!-- Related to hibernate properties
END-->
<!-- Related to mapping START-->
<mapping resource="hbm file 1 name .xml" / >
<mapping resource="hbm file 2 name .xml" / >
<!-- Related to the mapping END -->
</session-factory>
</hibernate-configuration>
Let
see one example to get more clarity about mapping and configuration file in
hibernate..
- write pojo class whose objects
will persist in the table..
public class Employee {
private int id;
private String
firstName;
private String lastName;
private int salary;
public Employee() {}
public Employee(String fname, String lname, int salary) {
this.firstName
= fname;
this.lastName
= lname;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId( int id ) {
this.id = id;
}
public String
getFirstName() {
return
firstName;
}
public void
setFirstName( String
first_name ) {
this.firstName
= first_name;
}
public String
getLastName() {
return
lastName;
}
public void
setLastName( String
last_name ) {
this.lastName
= last_name;
}
public int
getSalary() {
return salary;
}
public void
setSalary( int salary ) {
this.salary = salary;
}
}
- create a table in pojo class data
will be persist.
create table EMPLOYEE (
id INT NOT NULL auto_increment,
first_name VARCHAR(20) default NULL,
last_name
VARCHAR(20) default NULL,
salary
INT default NULL,
PRIMARY KEY (id)
);
- Write a maping file of your pojo
class.
<?xml
version = "1.0" encoding = "utf-8"?>
<!DOCTYPE
hibernate-mapping PUBLIC
"-//Hibernate/Hibernate
Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "Employee" table = "EMPLOYEE">
<meta attribute = "class-description">
This class contains the employee
detail.
</meta>
<id name = "id" type = "int" column = "id">
<generator class="native"/>
</id>
<property name = "firstName" column = "first_name" type = "string"/>
<property name = "lastName" column = "last_name" type = "string"/>
<property name = "salary" column = "salary" type = "int"/>
</class>
</hibernate-mapping>
- Write a configuration file for the
pojo class
<?xml
version = "1.0" encoding = "utf-8"?>
<!DOCTYPE
hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/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>
<!-- Assume test is the
database name -->
<property name = "hibernate.connection.url">
jdbc:mysql://localhost/test
</property>
<property name = "hibernate.connection.username">
root
</property>
<property name = "hibernate.connection.password">
root123
</property>
<!-- List of XML mapping
files -->
<mapping resource = "Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
0 comments:
Post a Comment