-->
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.  [ 3 posts ] 
Author Message
 Post subject: many-to-many mapping
PostPosted: Mon Jun 14, 2004 5:38 pm 
Newbie

Joined: Mon Jun 14, 2004 5:16 pm
Posts: 16
Location: New York
Hi,
I'm quite new to Hibernate and am trying to modify my existing web apps' persistence layer to use it..

I have two database tables:

Code:
Portal (Portal_id, name, url, etc.)
Portal_Zipcode_Map (portal_id, zipcode)


There is no separate zipcode table, nor a zipcode_id field in Portal_zipcode_map....

Portal.java contains a

Code:
List postCodes


along with other properties.

PortalZipcodeMap.java has only:
Code:
Integer portalId
String zipcode



Both classes have their default constructors...

In Portal.hbm.xml I have defined the 'bag' as follows:
Code:
<bag
            name="postCodes"
            table="portal_zipcode_map"
            lazy="false"
            inverse="true"
            cascade="none"
        >
     <key column="portal_id"/>
     <many-to-many  class=".PortalPostcodeMap"
                  column="postcode"
                  outer-join="false"/>

</bag>


And this works...But I'm not confident that this is the best approach..

Do I have to represent the portal_postcode_map as a java class in order to populate the
Code:
postCodes
list of the
Code:
Portal
object?
Can't I simply load the postcodes as simple String values?

Thanks in advance.
Cagan




[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 14, 2004 5:53 pm 
Beginner
Beginner

Joined: Mon Sep 29, 2003 10:32 pm
Posts: 35
Location: Toronto, Ontario
Yes you can. See
http://www.hibernate.org/hib_docs/refer ... ns-mapping


Top
 Profile  
 
 Post subject: How to filter on a scalar collection element in select?
PostPosted: Wed Nov 10, 2004 12:22 pm 
Newbie

Joined: Mon Jun 14, 2004 5:16 pm
Posts: 16
Location: New York
Hibernate Version: 2.1
Database: MySQL Standard 4.0.12

I have a Portal class that contains a set of Postcodes (String objects).

I created the following mapping in the Portal object for the postCodes set:
Code:
       
<set
            name="postCodes"
            table="tbl_portal_postcode_map"
            lazy="true"
            inverse="false"
            cascade="none"
            sort="unsorted"
        >

              <key
                  column="portal_id"
              >
              </key>

              <element
                  column="postcode"
                  type="string"
                  not-null="false"
                  unique="false"
              />

        </set>


tbl_portal_postcode_map table contains two columns:
    portal_id
    postcode

I'm trying to retrieve list Portal objects whose Postcode values match a certain value with the following Criteria:

Code:
Criteria criteria = session.createCriteria(Portal.class);
Criteria pCriteria = criteria.createCriteria("postCodes");
pCriteria.add(Expression.ilike("postCodes.postcode", postcode + "%"));
criteria.addOrder(Order.asc("name"));
           
  List results = criteria.list();


But I get the following exception:
Code:
WARNING: Unable to retrieve portals by postcode: collection was not an association: com.emirca.touch.module.share.model.portal.Portal.postCodes


How can I solve this?

Thanks in advance,
Cagan


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