-->
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: Collections question, stuck on something that should be easy
PostPosted: Thu Sep 15, 2005 1:41 pm 
Newbie

Joined: Wed Sep 14, 2005 6:29 pm
Posts: 4
Location: USA
If each A can have several Bs, I have no problem defining a one-to-many map that gives me the map of Bs, in A's hbm.xml. It works great.

However, I can't figure out, if I _only_ have a table of Bs, how to define a map in B's hbm.xml, of all the B's. That is, I want a way to fetch a map of B's, typically _all_ B's, without having to define a dummy A that relates to a foreign key in all of the B's.

In the simplest case, if I have a table T, such that:

col1 col2
+----+-----+
| foo | bar |
+----+-----+
| baz | qux |
+----+-----+

How can I get a map such that:

Key (String) -> Object (String)

foo bar
baz qux

_without_ having _another_ table which has a one to many relationship with this table?

Is this too simple for Hibernate?

I have thrashed on it for a while without figuring out how to do it...

I really just want a PersistentMap interface to a single table...

It is easy if I create a table called "dummy" with 1 row and 1 value of "1", and then add a foreign key to each row in T referring to "1" in "dummy".

In that case, I can define a <map> on "dummy" mapping all keys to values of table T for a given "dummy".

But it doesn't seem right that I would need the "dummy" table...

Am I missing something obvious?

-Tom


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 15, 2005 2:08 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
First, let me say that you should not post twice the same question to the same forum in 5 hour interval.

I am not sure if I fully understand your question. If I get this right, you want to map an Entity to itself through an association.

ie: Collection relatedEntityA = EntityA.getRelatedEntityAs()

If this is the case and if col1 represents your primary key and col2 your foreign key, then you can do it this way.
Code:

set name="relatedEntityAs" table="EntityATable">
            <key column="col2"/>
            <one-to-many class="EntityA"/>
</set>



if col1 is not your primary key, then it becomes more complex. I would need to read the doc. but it can be done.


But, If what you want is a way to get all records from the EntityA table from an instance of A : like

A.getAllAs(),

then I do not think you should do it through mapping. It should be offered through a DAO.

I hope this will help. Regards,

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 15, 2005 3:33 pm 
Newbie

Joined: Wed Sep 14, 2005 6:29 pm
Posts: 4
Location: USA
What I want is to end up with a PersistentMap object.

Referring to my example, if I have a "dummy" table, I can get an object of type org.hibernate.collection.PersistentMap, which is backed by table T, mapping from col1 (key) to col2 (value).

I know I can get a list, or use a dao for each objet, or get a set.

What I want is something that implements java.util.Map - as does org.hibernate.collection.PersistentMap - as I can get from the <map> tag in my .hbm.xml - of a table.

Note, neither col1 or col2 are my primary key - nor need they be if I'm using an <element> definition of a <map> on the "dummy" table - but I could (in this case) change col1 to be the primary key (rather than a sequence oid), if I had to.

The point of my question is that _with a dummy table_, I get a nice PeristentMap representation, which allows lazy loading and transparent persistence, but _without a dummy table_, I can't figure out a way to get that.

Sorry for the duplicate posting, I thought my original post had a misleading subject and a poorly written body, mea culpa.

A set or list isn't what I need- I'm not going to iterate through all of them, I'm handing it to processes that were written to expect Map or Properties objects, which is what is so nice about the org.hibernate.collection.PersistentMap approach...

Again, I am probably just ignorant of an easy way to do this without the superfluous table...

-Tom


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.