My program is developed using Hibernate to demonstrate Collection mapping using maps. Basically An employee has a name, id and an address (represented as a map)
Hibernate version: 3.2.4
Mapping documents
Code:
<?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="hibernate.Employee" table="employee">
<id name="id" column="id" >
</id>
<map name="address" >
<key column ="id" />
<index column = "property_name" type= "string" />
<element column = "property_value" type= "string" />
</map>
<property name = "name" type = "string" />
</class>
</hibernate-mapping>
CodeCode:
package hibernate;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class EmployeeAddress {
public static void main(String[] args) {
Session session = null;
Transaction tx = null;
try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
tx = session.beginTransaction();
Employee employee = new Employee();
employee.setName("bob");
employee.setId(1);
Map address = new HashMap();
address.put("street" , "M.G Road");
address.put("city" , "bangalore");
address.put("pin" , "560018");
employee.setAddress(address);
session.save(employee);
}
catch(NumberFormatException e){
e.printStackTrace();
}catch(HibernateException e){
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}finally{
session.flush();
session.close();
System.exit(0);
}
}
}
Full stack trace of any exception that occurs:No error but this is what got displayed in consolelog4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into employee (name, id) values (?, ?)
Hibernate: insert into address (id, property_name, property_value) values (?, ?, ?)
Hibernate: insert into address (id, property_name, property_value) values (?, ?, ?)
Hibernate: insert into address (id, property_name, property_value) values (?, ?, ?)
Name and version of the database you are using: Oracle 9i
Configuration File
Code:
<?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.connection.url">jdbc:oracle:thin:@localhost:1521:bob</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
The Bean CodeCode:
package hibernate;
import java.util.Map;
// Demonstration of Collection Mapping using Maps
public class Employee {
private int id;
private String name;
private Map address;
public Map getAddress() {
return address;
}
public void setAddress(Map address) {
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Tables that were created in the Backend
SQL> desc employee
Name Null? Type
----------------------------------------- -------- -----------------------
ID NOT NULL NUMBER(10)
NAME VARCHAR2(255 CHAR)
SQL> desc address;
Name Null? Type
----------------------------------------- -------- -----------------------
ID NOT NULL NUMBER(10)
PROPERTY_VALUE VARCHAR2(255 CHAR)
PROPERTY_NAME NOT NULL VARCHAR2(255 CHAR)
NO DATA WAS BEING INSERTED ON RUNNING THE APPLICATION