-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: No Error | Creating the Tables but not inserting the Data
PostPosted: Fri Jul 06, 2007 8:38 am 
Newbie

Joined: Mon May 28, 2007 6:24 am
Posts: 11
Location: Bangalore
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>




Code
Code:

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 console

log4j: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 Code

Code:

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

_________________
http://www.hibernate-tutorial.com
http://www.spring-hibernate.com
http://www.spring-tutorial.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 06, 2007 8:44 am 
Newbie

Joined: Mon May 28, 2007 6:24 am
Posts: 11
Location: Bangalore
SOLVED

I had fogotton to commit the transaction .. :-)[/b]

_________________
http://www.hibernate-tutorial.com
http://www.spring-hibernate.com
http://www.spring-tutorial.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.