-->
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.  [ 6 posts ] 
Author Message
 Post subject: Foreign Key Mapping in hbm.xml file
PostPosted: Mon Jan 12, 2009 1:59 am 
Newbie

Joined: Mon Jan 12, 2009 1:46 am
Posts: 2
Hibernate version:3.0



Hi,

This is my first post in this forum, i have 2 tables :-

Category
-----------
Category_ID (PK)
Category_Name
Category_Active


SubCategory
-----------
SubCategory_ID (PK)
Category_ID (FK)
SubCategory_Name
SubCategory_Active

Here in category table everything is one -to- one mapping , but in the subcategory table there is a foreign key reference for category_id through which i can get the category name.

1)How this reference should be depicted in hbm.xml files for both category and subcategory?
2) How the insert,update,delete for these tables will be done. Plaese do share a sample code particularly for the foreign key references?

Please do respond to this.

Thanks,
HibernateCrazy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 4:32 am 
Beginner
Beginner

Joined: Wed Nov 19, 2008 8:25 am
Posts: 46
Location: Saint Petersburg, Russian Federation
Category.java
Code:
package com.hibernate.entity;

public class Category {

   private Long id;
   private String name;
   private String active;

    public Long getId() {
        return id;
    }

    private void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getActive() {
        return active;
    }

    public void setActive(String active) {
        this.active = active;
    }

    @Override
   public String toString() {
      return "id=" + id + "name=" + name + "; active=" + active;
   }
}


SubCategory.java
Code:
package com.hibernate.entity;

public class SubCategory {

    private Long id;
   private String name;
   private String active;
    private Category parent;

    public Long getId() {
        return id;
    }

    private void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getActive() {
        return active;
    }

    public void setActive(String active) {
        this.active = active;
    }

    public Category getParent() {
        return parent;
    }

    public void setParent(Category parent) {
        this.parent = parent;
    }

    @Override
   public String toString() {
      return "id=" + id + "name=" + name + "; active=" + active + "; parent={" + parent + "}";
   }
}


Category.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="com.hibernate.entity.Category" table="Category">
        <id name="id" column="Category_ID">
            <generator class="native"/>
        </id>
        <property name="name" column="Category_Name"/>
        <property name="active" column="Category_Active"/>
    </class>

</hibernate-mapping>


SubCategory.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="com.hibernate.entity.SubCategory" table="SubCategory">
        <id name="id" column="SubCategory_ID">
            <generator class="native"/>
        </id>
        <property name="name" column="SubCategory_Name"/>
        <property name="active" column="SubCategory_Active"/>
        <many-to-one name="parent" class="com.hibernate.entity.Category" cascade="save-update"/>
    </class>

</hibernate-mapping>


hibernate.cfg.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name="connection.url">jdbc:mysql://localhost/test</property>
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="connection.username">root</property>
      <property name="connection.password"></property>
      <property name="connection.pool_size">1</property>
      <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
      <property name="current_session_context_class">thread</property>
      <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
      <property name="use_sql_comments">true</property>
      <property name="hbm2ddl.auto">update</property>
      <mapping resource="com/hibernate/entity/Category.hbm.xml"/>
      <mapping resource="com/hibernate/entity/SubCategory.hbm.xml"/>
   </session-factory>
</hibernate-configuration>


HibernateStart.java
Code:
package com.hibernate;

import java.io.Serializable;
import javax.persistence.*;

import com.hibernate.entity.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class HibernateStart {


    public static void main(String[] args) {
      processHibernateXml();
    }

    private static void processHibernateXml() {
        Session session;
        Transaction tx;

        session = new Configuration().configure().buildSessionFactory().openSession();

        Category category = new Category();
        category.setName("base-category");
        category.setActive("y");

        SubCategory subCategory = new SubCategory();
        subCategory.setName("sub-category");
        subCategory.setActive("n");
        subCategory.setParent(category);

        // CREATE
        tx = session.beginTransaction();
        Serializable id = session.save(subCategory);
        tx.commit();
        System.out.println("Stored category: " + category);
        System.out.println("Stored subcategory: " + subCategory);

        // READ & UPDATE
        tx = session.beginTransaction();
        subCategory = (SubCategory)session.get(SubCategory.class, id);
        System.out.println("Loaded sub-category: " + subCategory);
        subCategory.setActive("y");
        tx.commit();
        System.out.println("Updated sub-category");

        // DELETE
        tx = session.beginTransaction();
        session.delete(subCategory);
        tx.commit();
        System.out.println("Removed sub-category");

        session.close();
    }

}
[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 9:45 am 
Newbie

Joined: Mon Jan 12, 2009 1:46 am
Posts: 2
denis.zhdanov wrote:
Category.java
Code:
package com.hibernate.entity;

public class Category {

   private Long id;
   private String name;
   private String active;

    public Long getId() {
        return id;
    }

    private void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getActive() {
        return active;
    }

    public void setActive(String active) {
        this.active = active;
    }

    @Override
   public String toString() {
      return "id=" + id + "name=" + name + "; active=" + active;
   }
}


SubCategory.java
Code:
package com.hibernate.entity;

public class SubCategory {

    private Long id;
   private String name;
   private String active;
    private Category parent;

    public Long getId() {
        return id;
    }

    private void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getActive() {
        return active;
    }

    public void setActive(String active) {
        this.active = active;
    }

    public Category getParent() {
        return parent;
    }

    public void setParent(Category parent) {
        this.parent = parent;
    }

    @Override
   public String toString() {
      return "id=" + id + "name=" + name + "; active=" + active + "; parent={" + parent + "}";
   }
}


Category.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="com.hibernate.entity.Category" table="Category">
        <id name="id" column="Category_ID">
            <generator class="native"/>
        </id>
        <property name="name" column="Category_Name"/>
        <property name="active" column="Category_Active"/>
    </class>

</hibernate-mapping>


SubCategory.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="com.hibernate.entity.SubCategory" table="SubCategory">
        <id name="id" column="SubCategory_ID">
            <generator class="native"/>
        </id>
        <property name="name" column="SubCategory_Name"/>
        <property name="active" column="SubCategory_Active"/>
        <many-to-one name="parent" class="com.hibernate.entity.Category" cascade="save-update"/>
    </class>

</hibernate-mapping>


hibernate.cfg.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name="connection.url">jdbc:mysql://localhost/test</property>
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="connection.username">root</property>
      <property name="connection.password"></property>
      <property name="connection.pool_size">1</property>
      <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
      <property name="current_session_context_class">thread</property>
      <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
      <property name="use_sql_comments">true</property>
      <property name="hbm2ddl.auto">update</property>
      <mapping resource="com/hibernate/entity/Category.hbm.xml"/>
      <mapping resource="com/hibernate/entity/SubCategory.hbm.xml"/>
   </session-factory>
</hibernate-configuration>


HibernateStart.java
Code:
package com.hibernate;

import java.io.Serializable;
import javax.persistence.*;

import com.hibernate.entity.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class HibernateStart {


    public static void main(String[] args) {
      processHibernateXml();
    }

    private static void processHibernateXml() {
        Session session;
        Transaction tx;

        session = new Configuration().configure().buildSessionFactory().openSession();

        Category category = new Category();
        category.setName("base-category");
        category.setActive("y");

        SubCategory subCategory = new SubCategory();
        subCategory.setName("sub-category");
        subCategory.setActive("n");
        subCategory.setParent(category);

        // CREATE
        tx = session.beginTransaction();
        Serializable id = session.save(subCategory);
        tx.commit();
        System.out.println("Stored category: " + category);
        System.out.println("Stored subcategory: " + subCategory);

        // READ & UPDATE
        tx = session.beginTransaction();
        subCategory = (SubCategory)session.get(SubCategory.class, id);
        System.out.println("Loaded sub-category: " + subCategory);
        subCategory.setActive("y");
        tx.commit();
        System.out.println("Updated sub-category");

        // DELETE
        tx = session.beginTransaction();
        session.delete(subCategory);
        tx.commit();
        System.out.println("Removed sub-category");

        session.close();
    }

}
[/quote]

Thanks Denis. You are a gem.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 03, 2009 8:56 am 
Newbie

Joined: Fri Apr 03, 2009 8:45 am
Posts: 1
i am testing the given code but i have this:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [test.SubCategory]
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1747)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2149)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:34)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:238)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:158)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:429)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:424)
at test.Test.main(Test.java:32)
Caused by: java.sql.SQLException: General error message from server: "Field 'Category_ID' doesn't have a default value"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2251)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1772)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1619)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1729)
... 14 more




there is one mistake in the SubCategory.hbm.xml

It should be:


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="test.SubCategory" table="SubCategory">
<id name="id" column="SubCategory_ID">
<generator class="native"/>
</id>
<property name="name" column="SubCategory_Name"/>
<property name="active" column="SubCategory_Active"/>
<many-to-one name="parent" class="test.Category" column="Category_ID" cascade="save-update"/>
</class>

</hibernate-mapping>

now it works fine. Thanks for the help. :)


Top
 Profile  
 
 Post subject: Re: Foreign Key Mapping in hbm.xml file
PostPosted: Fri May 17, 2013 9:23 am 
Newbie

Joined: Fri May 17, 2013 9:14 am
Posts: 2
Hi if I am using the same way in my code I am getting the following exception.

Hibernate: /* insert com.save.beans.Master */ insert into MSTR (ID, VRSN, CUR_CD, CUR_STAT, AGT_AUTH_CD, NBR, MOD_ACS_CD, HAZ_DVN_ACS_CD, TOOL_NME, TOOL_ACS_CMNT, ACS_CD, ACS_NME, ACS_CMNT, CRET_ID, CRET_TS, EXP_DT, LST_MAINT_ID, LST_MAINT_TS) values (default, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
[main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: -407, SQLState: 23502
[main] ERROR org.hibernate.util.JDBCExceptionReporter - DB2 SQL Error: SQLCODE=-407, SQLSTATE=23502, SQLERRMC=TBSPACEID=2, TABLEID=527, COLNO=0, DRIVER=3.57.82
Exception in thread "main" org.hibernate.HibernateException: org.hibernate.exception.ConstraintViolationException: could not insert: [com.save.beans.LOAMaster]
at com.save.hibernate.MasterForSaveUpdate.main(MasterForSaveUpdate.java:126)
Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [com.save.beans.Master]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2176)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2656)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:527)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:241)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:292)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:240)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:193)
at org.hibernate.engine.Cascade.cascade(Cascade.java:154)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:454)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:527)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:523)
at com.save.hibernate.MasterForSaveUpdate.main(MasterForSaveUpdate.java:107)
Caused by: com.ibm.db2.jcc.am.fo: DB2 SQL Error: SQLCODE=-407, SQLSTATE=23502, SQLERRMC=TBSPACEID=2, TABLEID=527, COLNO=0, DRIVER=3.57.82
at com.ibm.db2.jcc.am.bd.a(bd.java:672)
at com.ibm.db2.jcc.am.bd.a(bd.java:60)
at com.ibm.db2.jcc.am.bd.a(bd.java:127)
at com.ibm.db2.jcc.am.km.b(km.java:2164)
at com.ibm.db2.jcc.am.km.c(km.java:2147)
at com.ibm.db2.jcc.t4.db.k(db.java:354)
at com.ibm.db2.jcc.t4.db.a(db.java:60)
at com.ibm.db2.jcc.t4.t.a(t.java:50)
at com.ibm.db2.jcc.t4.tb.b(tb.java:218)
at com.ibm.db2.jcc.am.lm.ec(lm.java:2692)
at com.ibm.db2.jcc.am.lm.b(lm.java:3552)
at com.ibm.db2.jcc.am.lm.Wb(lm.java:690)
at com.ibm.db2.jcc.am.lm.executeUpdate(lm.java:673)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
... 30 more

Please help me to solve this issue. I am working for this 2 days. I dont know what to do next.


Top
 Profile  
 
 Post subject: Re: Foreign Key Mapping in hbm.xml file
PostPosted: Fri May 17, 2013 10:08 am 
Newbie

Joined: Fri May 17, 2013 9:14 am
Posts: 2
I forgot to give the class and mapping files:

The client class is :MasterForSaveUpdate.java
--------------------------------------------

package com.save.hibernate;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.save.beans.Details;
import com.save.beans.Master;

public class MasterForSaveUpdate {

public static void main(String[] args)
{
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction tx;
try {
tx = session.beginTransaction();

Master master = new Master();
master.setCRET_ID("Hi");
//
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
long currentDate = System.currentTimeMillis();
master.setCRET_TS(new Timestamp(currentDate));
master.setEXP_DT(new Date(currentDate));
master.setAGT_AUTH_CD('x');
master.setCUR_CD('c');
master.setCUR_STAT("Inval");
master.setNBR(new Integer(6));
master.setVRSN(new Integer(100));
master.setMAINT_ID("HiMai");
master.setMAINT_TS(new Timestamp(currentDate));
master.setACS_CD('d');
master.setACS_CMNT("cmnt");
master.setACS_NME("Indra");
master.setHAZ_DVN_ACS_CD('h');
master.setMOD_ACS_CD('w');
master.setTOOL_ACS_CMNT("webtool");
master.setTOOL_NME("webtoolname");


Details details = new Details();
details.setCo_633("COORG");
details.setMaint_id("MaintID");
details.setMaster(master);
details.setMaint_ts(new Timestamp(currentDate));
session.saveOrUpdate(details);
tx.commit();
System.out.println("Stored Master: " + master);
System.out.println("Stored Details: " + details);

session.flush();

}
// catch (ConstraintViolationException ce){
// System.out.println("Constraint Name : $$$$$$$$$$$$$$$$$$$$$$$: "+ce.getConstraintName());
// }
catch (HibernateException e) {

throw new HibernateException(e);
}
finally {
session.close();
//factory.close();
}
}

}

And My bean classes:
-------------------------

/**
*
*/
package com.save.beans;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

/**
* @author indramohan_G
*
*/
public class Master implements Serializable{

int ID;

int VRSN;
char CUR_CD;
String CUR_STAT;

char AGT_AUTH_CD;
int NBR;
char MOD_ACS_CD;
char HAZ_DVN_ACS_CD;
String TOOL_NME;
String TOOL_ACS_CMNT;
char ACS_CD;
String ACS_NME;
String ACS_CMNT;
String CRET_ID;
Timestamp CRET_TS;
Date EXP_DT;
String MAINT_ID;
Timestamp MAINT_TS;
Details details;
public Master() {

}

public Details getDetails() {
return details;
}

public void setDetails(LOA3PLDetails details) {
this.details = details;
}

public int getID() {
return ID;
}

public void setID(int id) {
ID = id;
}

public int getVRSN() {
return VRSN;
}

public void setVRSN(int vrsn) {
VRSN = vrsn;
}

public char getCUR_CD() {
return CUR_CD;
}

public void setCUR_CD(char cur_cd) {
CUR_CD = cur_cd;
}

public String getCUR_STAT() {
return CUR_STAT;
}

public void setCUR_STAT(String cur_stat) {
CUR_STAT = cur_stat;
}

public char getAGT_AUTH_CD() {
return AGT_AUTH_CD;
}

public void setAGT_AUTH_CD(char agt_auth_cd) {
AGT_AUTH_CD = agt_auth_cd;
}

public int getNBR() {
return NBR;
}

public void setNBR(int nbr) {
NBR = nbr;
}

public char getMOD_ACS_CD() {
return MOD_ACS_CD;
}

public void setMOD_ACS_CD(char mod_acs_cd) {
MOD_ACS_CD = mod_acs_cd;
}

public char getHAZ_DVN_ACS_CD() {
return HAZ_DVN_ACS_CD;
}

public void setHAZ_DVN_ACS_CD(char haz_dvn_acs_cd) {
HAZ_DVN_ACS_CD = haz_dvn_acs_cd;
}

public String getTOOL_NME() {
return TOOL_NME;
}

public void setTOOL_NME(String tool_nme) {
TOOL_NME = tool_nme;
}

public String getTOOL_ACS_CMNT() {
return TOOL_ACS_CMNT;
}

public void setTOOL_ACS_CMNT(String tool_acs_cmnt) {
TOOL_ACS_CMNT = tool_acs_cmnt;
}

public char getACS_CD() {
return ACS_CD;
}

public void setACS_CD(char acs_cd) {
ACS_CD = acs_cd;
}

public String getACS_NME() {
return ACS_NME;
}

public void setACS_NME(String acs_nme) {
ACS_NME = acs_nme;
}

public String getACS_CMNT() {
return ACS_CMNT;
}

public void setACS_CMNT(String acs_cmnt) {
ACS_CMNT = acs_cmnt;
}

public String getCRET_ID() {
return CRET_ID;
}

public void setCRET_ID(String cret_id) {
CRET_ID = cret_id;
}

public Timestamp getCRET_TS() {
return CRET_TS;
}

public void setCRET_TS(Timestamp cret_ts) {
CRET_TS = cret_ts;
}

public Date getEXP_DT() {
return EXP_DT;
}

public void setEXP_DT(Date exp_dt) {
EXP_DT = exp_dt;
}

public String getMAINT_ID() {
return MAINT_ID;
}

public void setMAINT_ID(String maint_id) {
MAINT_ID = maint_id;
}

public Timestamp getMAINT_TS() {
return MAINT_TS;
}

public void setMAINT_TS(Timestamp maint_ts) {
MAINT_TS = maint_ts;
}
}


package com.save.beans;

import java.sql.Timestamp;

public class Details {

private int id;
private int pl_id;
private String co_633;
private String maint_id;
private Timestamp maint_ts;
private Master master;


public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPl_id() {
return pl_id;
}
public void setPl_id(int pl_id) {
this.plid = pl_id;
}
public String getCo_633() {
return co_633;
}
public void setCo_633(String co_633) {
this.co_633 = co_633;
}
public String getMaint_id() {
return maint_id;
}
public void setMaint_id(String maint_id) {
this.maint_id = maint_id;
}
public Timestamp getMaint_ts() {
return maint_ts;
}
public void setMaint_ts(Timestamp maint_ts) {
this.maint_ts = maint_ts;
}
public Master getMaster() {
return master;
}
public void setMaster(Master master) {
this.master = master;
}

}

Mapping files:Master.hbm.xml
------------------------------------------------------

<?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="com.save.beans.Master" table="MSTR">
<meta attribute="class-description">
This class contains Details about the
</meta>

<id name="ID" column="ID" type="integer">
<generator class="native"/>
</id>
<property name="VRSN" column="VRSN" type="integer"/>
<property name="CUR_CD" column="CUR_CD" type="char"/>
<property name="CUR_STAT" column="CUR_STAT" type="string"/>
<property name="AGT_AUTH_CD" column="AGT_AUTH_CD" type="char"/>
<property name="NBR" column="NBR" type="integer"/>
<property name="MOD_ACS_CD" column="MOD_ACS_CD" type="char"/>
<property name="HAZ_DVN_ACS_CD" column="HAZ_DVN_ACS_CD" type="char"/>
<property name="TOOL_NME" column="TOOL_NME" type="string"/>
<property name="TOOL_ACS_CMNT" column="TOOL_ACS_CMNT" type="string"/>
<property name="ACS_CD" column="ACS_CD" type="char"/>
<property name="USER_ACS_NME" column="ACS_NME" type="string"/>
<property name="ACS_CMNT" column="ACS_CMNT" type="string"/>
<property name="CRET_ID" column="CRET_ID" type="string"/>
<property name="CRET_TS" column="CRET_TS" type="timestamp"/>
<property name="EXP_DT" column="EXP_DT" type="date"/>
<property name="MAINT_ID" column="MAINT_ID" type="string"/>
<property name="MAINT_TS" column="MAINT_TS" type="timestamp"/>

</class>
</hibernate-mapping>

Details.hbm.xml file:
-----------------------
<?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="com.save.beans.Details" table="PL_DTL">
<meta attribute="class-description">
This class contains Details about the.
</meta>
<id name="pl_id" column="PL_ID" type="int">
<generator class="native"/>
</id>
<property name="co_633" column="CO_633" type="string"/>
<property name="maint_id" column="MAINT_ID" type="string"/>
<property name="maint_ts" column="MAINT_TS" type="timestamp"/>
<many-to-one name="master" class="com.save.beans.Master" column="ID" cascade="save-update"/>

</class>
</hibernate-mapping>

Config xml file:
---------------

<?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>

<!-- Database connection settings -->
<property name="connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
<property name="connection.url">jdbc:db2://171.4.10.55:3700/DB</property>
<property name="connection.username">bupbup</property>
<property name="connection.password">ert5ert5</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.DB2Dialect</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="use_sql_comments">true</property>
<property name="hbm2ddl.auto">update</property>




<mapping resource="Master.hbm.xml"/>
<mapping resource="Details.hbm.xml"/>


</session-factory>

</hibernate-configuration>


Please give me the solution. I am not able fix this for last two days.

Thanks in Advance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.