-->
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.  [ 5 posts ] 
Author Message
 Post subject: Many-to-Many converted to a many-to-one
PostPosted: Fri Jun 04, 2004 10:31 am 
Beginner
Beginner

Joined: Mon May 03, 2004 1:25 pm
Posts: 31
I have a many-to-many relationship in the db but the domain models it as a many-to-one. Is there anyway to map the relationship as a many-to-one, so that I end up with multiple instances of countysurvey (one for each marketsegment)?

I'm using Hibernate 2.1 against an oracle 9i db.

Thanks,
Tim


Code:
<class name="MarketSegment" table="GDR_PROD.MKT_SEG">
        <jcs-cache usage="read-only"/>
        <id name="id" type="long" column="MKT_SEG_GNMBR">
            <generator class="assigned"/>
        </id>
           
        <set name="countySurveys" table="GDR_PROD.MKT_SEG_SMPL_GEO" lazy="false">
         <key column="MS_MKT_SEG_GNMBR"/>
         <many-to-many column="SG_SMPL_GEO_GNMBR" class="CountySurvey"/>
      </set>
      
   </class>
   
   <class name="CountySurvey" table="GDR_PROD.SMPL_GEO">
        <jcs-cache usage="read-only"/>
        <id name="id" type="int" column="SMPL_GEO_GNMBR">
            <generator class="assigned"/>
        </id>
        <property name="name" column="SMPL_GEO_NAME"/>
      
        <set name="marketSegments" table="GDR_PROD.MKT_SEG_SMPL_GEO" lazy="false">
         <key column="SG_SMPL_GEO_GNMBR"/>
         <many-to-many column="MS_MKT_SEG_GNMBR" class="MarketSegment"/>
      </set>
         
    </class>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 04, 2004 10:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
no you can't map this virtual many to one with hibernate, it must be a many to many.

But what you can do is to declare your marketSegment property (which means many to one in your domain model) and in the :
- setMarketSegments() code marketSegment = (MarketSegment)marketSegments.get(0)
- and in the setMarketSegment code marketSegments.set(0,marketSegment)


marketSegments will be your persistent mapped property
marketSegment will be your business property

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 04, 2004 10:40 am 
Beginner
Beginner

Joined: Mon May 03, 2004 1:25 pm
Posts: 31
I thought of that, but there's more than one segment, so I have to create new countysurvey objects for each one.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 04, 2004 10:43 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
so why don't you just keep many to many semantics?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 04, 2004 10:51 am 
Beginner
Beginner

Joined: Mon May 03, 2004 1:25 pm
Posts: 31
I'm sorry your first post was the solution. Essentially, when the domain asks for the CountySurveys from MarketSegment, I'll populate the setMarketSegment() business method, with the current marketsegment.

Thanks,
Tim


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