-->
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.  [ 10 posts ] 
Author Message
 Post subject: NonUniqueException and ternary association
PostPosted: Tue Feb 03, 2004 5:48 am 
Regular
Regular

Joined: Wed Nov 26, 2003 6:22 am
Posts: 76
Location: Stockholm
Hi!

Class InfoContact has the following mapping.


<hibernate-mapping>
<class
name="ks.rah.cainfo.db.model.InfoContact"
table="CONTACT"
dynamic-update="false"
dynamic-insert="false"
>
.
.
.
<list
name="data"
table="DATA"
lazy="false"
inverse="false"
cascade="save-update"
>

<key
column="CONTACT_FK"
/>

<index
column="INDEX_"
/>

<composite-element
class="ks.rah.cainfo.db.model.FieldValue"
>

<many-to-one
name="field"
class="ks.rah.cainfo.db.model.ContactField"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
column="FIELD_FK"
/>

<many-to-one
name="value"
class="ks.rah.cainfo.db.model.ContactMeta"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
column="VALUE_FK"
/>

</composite-element>

</list>
.
.
</class>

I would want the InfoContact-object to have a List of
FieldValue-object where the 'field' part isn't necessarily unique,
for example:

InfoContact:data
[field1; value1]
[field1; value2]
[field2; value7]
etc.

Problem in short is this:

When I execute the following code (JUnit-test)

public void testCreateNew() throws HibernateException
{
InfoContactDAO infomgr = new InfoContactDAO();
ContactMetaDAO metamgr = new ContactMetaDAO();
ContactFieldDAO fieldmgr = new ContactFieldDAO();
InfoContact contact = new InfoContact();
ContactField gender = fieldmgr.retrieve("K


Top
 Profile  
 
 Post subject: PS
PostPosted: Tue Feb 03, 2004 5:58 am 
Regular
Regular

Joined: Wed Nov 26, 2003 6:22 am
Posts: 76
Location: Stockholm
And I've read

http://www.hibernate.org/117.html#A20

and still don't understand why InfoContact chokes on the ContactMeta-object that I loaded in another session....
I want to associate this ContactMeta object to the FieldValue object that in it's turn should be in a M:1-relation with the InfoContact.
(Why is it so hard to express one


Top
 Profile  
 
 Post subject: PPS
PostPosted: Tue Feb 03, 2004 7:17 am 
Regular
Regular

Joined: Wed Nov 26, 2003 6:22 am
Posts: 76
Location: Stockholm
Another thought...

I'm using the Thread Local-pattern for obtaining a Session object. Could this be the problem??

/F


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2004 12:12 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
can you simplify your code and show that do you smthgmgr ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Repost
PostPosted: Tue Feb 03, 2004 3:03 pm 
Regular
Regular

Joined: Wed Nov 26, 2003 6:22 am
Posts: 76
Location: Stockholm
emmanuel wrote:
can you simplify your code and show that do you smthgmgr ?


Reposted in a hopefully more readable format.

What does 'smthgmgr' mean? I'm from Sweeeden..... ;-)

/F


Top
 Profile  
 
 Post subject: Works
PostPosted: Wed Feb 04, 2004 5:49 am 
Regular
Regular

Joined: Wed Nov 26, 2003 6:22 am
Posts: 76
Location: Stockholm
Well...

changed
Code:
cascade="save-update"


to
Code:
cascade="none"


in the InfoContact mapping. No NonUniqueObjectException anymore.

I'm still concerned about how to handle and think concerning DAO-classes and atomical database calls. Is this the right approach in conjunction with the Thread Local Session pattern? This is rather important to me since I'm developing in Tomcat/J2EE.

/F


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 8:56 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
smthmgr => something manager in your code (*mgr):
[list=]infomgr
metamgr
fieldmgr [/list]

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Somethingmanager
PostPosted: Thu Feb 05, 2004 5:35 am 
Regular
Regular

Joined: Wed Nov 26, 2003 6:22 am
Posts: 76
Location: Stockholm
Hello Emmanuel and thanks for answering.

My manager classes handles database calls, I guess you could calll them DAO-classes. All methods follow the pattern of the method below.

Code:
public class ContactMetaDAO {
..
   public ContactMeta retrieve(String metadisplay) throws HibernateException
   {
      ContactMeta meta = null;
      List result = null;
      Session session = null;
      Transaction tx = null;
      
      try
      {
         session = HibernateSession.currentSession();
         tx = session.beginTransaction();
         result = session.find("from ContactMeta as meta where meta.displayValue = ?",
         metadisplay,
         Hibernate.STRING );
         if(result == null || result.size() != 1 )
         {            
            return meta;
         }
         else
         {
            meta = (ContactMeta) result.get(0);
            //System.out.println(meta);   
         }
         tx.commit();
      }
      catch(HibernateException he)
      {
         if(tx!=null) tx.rollback();
         throw he;
      }
      finally
      {         
         HibernateSession.closeSession();   
      }
      return meta;
   }
..
}


InfoContactDAO:

Code:
public class InfoContactDAO {
   

   public Long create(InfoContact contact) throws HibernateException
   {
      Session session =    null;
      Transaction tx =    null;
      Long id = null;
      
      try
      {
         session = HibernateSession.currentSession();
         tx = session.beginTransaction();
                  session.saveOrUpdate(contact);         
         tx.commit();
      }
      catch(HibernateException he)
      {
         if(tx!=null) tx.rollback();
         throw he;
      }
      finally
      {         
         HibernateSession.closeSession();   
      }
      return id;
   }
}


I'm not sure if this is what you wanted to see...

Sincerely,

/F


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 8:56 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Using the Thread session pattern, I recommend you to manage Tx at a higher level (process level).
Have a look a Spring framework (at least for the concepts), it's based on pretty good patterns.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Questions
PostPosted: Fri Feb 06, 2004 4:17 am 
Regular
Regular

Joined: Wed Nov 26, 2003 6:22 am
Posts: 76
Location: Stockholm
emmanuel wrote:
Using the Thread session pattern, I recommend you to manage Tx at a higher level (process level).
Have a look a Spring framework (at least for the concepts), it's based on pretty good patterns.


Hi, thanks for answering.

OK, let me see if I get you right.
My app is a servletbased one in TOMCAT. The overall idea is this:

UI (JSP, HTML)
communicates with
HttpHandler (invoked through a homemade front end servlet)
uses (through instantiation)
DAO-class
uses
Hibernate
communicates with
DB

Your suggestion is to move the operation (database calls) from the "DAO-class"-layer to "HttpHandler"-layer. Do I get it right?

This raises questions. For example, if I would want to keep the business layer-model(DAO-classes) , how do I best do that? Move the instatiation of the Session (or the Transaction even or both) to the HttpHandler layer, and send the Session (and/or Transaction) created to the DAO-methods as (an) argument(s)?

I feel a lot of portability/extendability is lost if I do a lot of melt-together of the HttpHandler and DAO-class layers. This is a fundamental design issue concerning servlets and Hibernate to which I haven't found a good answer yet.

Sincerely,

/F


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