-->
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: composite key problem
PostPosted: Wed Oct 08, 2003 8:47 am 
Newbie

Joined: Fri Sep 26, 2003 10:46 am
Posts: 6
Hi All
I have a table with 2 composite keys(which makes primary key).one is of string type(dty_Doc_Type) and the other int type(dty_Year)..
Basing on the above, when i am trying to insert fields hibernate is throwing
net.sf.hibernate.MappingException: No persister for: com.webify.ccpa.db.hibernate.CompositeKey

- I have a main class as TblMst_Document_Type
-I have also created a separate class CompositeKey for placing the 2 columns..

-Mapping file for TblMst_Document_Type :

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.webify.ccpa.db.hibernate.TblMst_Document_Type" table="tblmst_document_type">

<composite-id class="com.webify.ccpa.db.hibernate.CompositeKey" name="id">
<key-property name="dty_Doc_Type" column="dty_Doc_Type"/>
<key-property name="dty_Year" column="dty_Year"/>
</composite-id>

<property name="dty_CreateBy"/>
<property name="dty_CreateDate"/>
<property name="dty_Doc_desc"/>
<property name="dty_Initial_Doc_Number"/>
<property name="dty_Last_Doc_Number"/>
<property name="dty_ModifiedBy"/>
<property name="dty_ModifiedDate"/>
<property name="dty_deleteflag"/>

</class>

</hibernate-mapping>

<!-- parsed in 30ms -->



Service class will have a function like:

Code:
public void insertParameterValues(TblMst_Document_Type doc) {
    Transaction tx = null;
    CompositeKey ck = new CompositeKey();
    try {
        session = sessionFactory.openSession();
        tx = session.beginTransaction();   

        if(doc == null)
                doc = new TblMst_Document_Type();

       ck.setDty_Doc_Type("D0001");
         ck.setDty_Year(2003);

        doc.setdty_Doc_desc("XYZ");
        doc.setdty_ModifiedBy("KReddy");


        session.save(ck);

        session.save(doc);

        session.flush();

        session.connection().commit();

        tx.commit();

        System.out.println("Inserted");

    } catch (HibernateException hibex) {

    try {

if (tx != null)

tx.rollback();

} catch (Exception iex) {

}

System.err.println(hibex);

} catch (Exception ex) {

ex.printStackTrace();

} finally {

try {

if (session != null)

session.close();

} catch (Exception ex) {

}

}

}


Can u pl guide me where i am wrong?

TblMst_Document_Type Code:
Code:
private String dty_Doc_Type;
private int dty_Year;
private String dty_Doc_desc;
private String dty_Last_Doc_Number;
private String dty_Initial_Doc_Number;
private String dty_deleteflag;
private String dty_CreateBy;
private Date dty_CreateDate;
private String dty_ModifiedBy;
private Date dty_ModifiedDate;
private CompositeKey id;

// generate getter and setter
// aslo overriden hascode and equals method


ComposteKey Code:
Code:
public class CompositeKey implements Serializable {
   private String dty_Doc_Type;
   private int dty_Year;
   /**
    * getDty_Doc_Type
    * String
    */
   public String getDty_Doc_Type() {
      return dty_Doc_Type;
   }

   /**
    * getDty_Year
    * int
    */
   public int getDty_Year() {
      return dty_Year;
   }

   /**
    * setDty_Doc_Type
    * void
    */
   public void setDty_Doc_Type(String string) {
      dty_Doc_Type = string;
   }

   /**
    * setDty_Year
    * void
    */
   public void setDty_Year(int i) {
      dty_Year = i;
   }

   public boolean equals(Object obj) {
      if (obj instanceof TblMst_Document_Type) {
         if (dty_Doc_Type == ((TblMst_Document_Type) obj).getdty_Doc_Type()
            && dty_Year == ((TblMst_Document_Type) obj).getdty_Year())
            return true;
      }
      return false;
   }

   /**
    * @see java.lang.Object#hashCode()
    */
   public int hashCode() {
      return new Integer(dty_Year).hashCode()+dty_Doc_Type.hashCode();
   }
}


Please guide me where i am going wrong??

Thanks and Regards

Krishna


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2003 9:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The composite key is added to the class, setId(ck) and it is not saved by itself so delete the save(ck).


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.