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.  [ 11 posts ] 
Author Message
 Post subject: Mapping collections
PostPosted: Fri Apr 10, 2009 11:41 am 
Newbie

Joined: Fri Apr 10, 2009 11:26 am
Posts: 5
What is the appropriate mapping strategy for the below scenario.
Following are the DB tables and the java code i want them to map.

Any pointers would be of great help.

Thanks


GROUP
groupid | name
1 | group1
2 | group2

GROUP_OBJ
groupid | objecttype | objectid
1 | OBJ1 | 1
1 | OBJ1 | 2
1 | OBJ2 | 1
1 | OBJ3 | 3
2 | OBJ1 | 1
2 | OBJ2 | 2

Code:
   
Class Group {
    Integer id;
    String Name;
    Collection<GroupObjects> groupObjects;
}

Class GroupObjects {

    String ObjectType;
    Collection<ObjectId> objectids;

}



Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 10, 2009 9:28 pm 
Newbie

Joined: Fri Apr 10, 2009 8:57 pm
Posts: 8
@Entity
class Group {
@OneToMany
List<ObjectID> objectIDs;
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 11, 2009 12:29 am 
Newbie

Joined: Fri Apr 10, 2009 11:26 am
Posts: 5
Thanks for your response.
Can you clarify your solution.

I'm more interested in the mapping for GroupObjects.
Appreciate any comments on the topic

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 11, 2009 2:34 am 
Newbie

Joined: Fri Apr 10, 2009 8:57 pm
Posts: 8
If I'm understanding correctly, the GroupObjects class is meant to be a join between Group and ObjectIDs. Here, the join (either by table or FK) will be managed for you by Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 11, 2009 7:27 am 
Newbie

Joined: Fri Apr 10, 2009 11:26 am
Posts: 5
Yes, GroupObjects is a join between Group and ObjectIds.
Let me explain my problem more clearly.

I want to access (set/get) objectIds based on ObjectType.

group.setObjectIds(ObjectType, Collection<ObjectIds>)

Collection<ObjectIds> group.getObjectIds(ObjectType)

ObjectType is an enum

If i have to adopt your solution then, to get the ObjectIds of a particular ObjectType, i have to iterate through the complete collection.
Similarly if i want to update only Objectids of a particular type then its not straight forward.

that way my ideal requirement will be a kind of map (but the mapping for this appears more complex)

Code:
Class Group {   
      Integer id;   
      String Name;   
      Map<ObjectType, Collection<ObjectIds>> groupObjects;
}   


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 11, 2009 1:00 pm 
Newbie

Joined: Fri Apr 10, 2009 8:57 pm
Posts: 8
Ah, I see. I've mapped Maps in Hibernate, but it was a Map<String, List<Foo>> (simple type to list of user type), so I don't if this applies to you but here goes.

I had to create a class, call it FooList, to encapsulate the List<Foo>, then - in the class with the Map member - declare it as:

@OneToMany(cascade=CascadeType.ALL)
private Map<String, FooList> foos;

I never found a way to directly do Map<String, List<Foo>>. HTH.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 11, 2009 4:29 pm 
Newbie

Joined: Fri Apr 10, 2009 8:57 pm
Posts: 8
Further research supports my initial response:

http://blog.xebia.com/2007/10/05/mappin ... hibernate/


Top
 Profile  
 
 Post subject: Using @OneToMany for a Hashmap + cascade delete
PostPosted: Mon Apr 13, 2009 9:08 am 
Newbie

Joined: Thu Jul 31, 2008 2:15 am
Posts: 7
Location: LONDON
Hi Jason,

Can you plese provide the full annotaitons you used for your java map? So far I have used:

@org.hibernate.annotations.CollectionOfElements
@JoinTable(
name = "zad_category_keywords",
joinColumns = @JoinColumn(name = "Catalog_Id")
)
@org.hibernate.annotations.MapKey(
columns = @Column(name="Shop_Category_Id")
)
@Column(name = "Keywords")
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Map<ShopCategory, String> categoryToKeywordsMap;

But the cascade delete is not working. Apparently it only applies to @OneToMany or @ManyToMany. So I want to adapt your @OneToMany annotations to get this working.

Many thanks
Jeremy


jasonwastaken wrote:
Ah, I see. I've mapped Maps in Hibernate, but it was a Map<String, List<Foo>> (simple type to list of user type), so I don't if this applies to you but here goes.

I had to create a class, call it FooList, to encapsulate the List<Foo>, then - in the class with the Map member - declare it as:

@OneToMany(cascade=CascadeType.ALL)
private Map<String, FooList> foos;

I never found a way to directly do Map<String, List<Foo>>. HTH.


Top
 Profile  
 
 Post subject: Re: Using @OneToMany for a Hashmap + cascade delete
PostPosted: Tue Apr 14, 2009 11:23 am 
Newbie

Joined: Fri Apr 10, 2009 8:57 pm
Posts: 8
I used

@OneToMany(cascade=CascadeType.ALL)

jcolton wrote:
Hi Jason,

Can you plese provide the full annotaitons you used for your java map? So far I have used:

@org.hibernate.annotations.CollectionOfElements
@JoinTable(
name = "zad_category_keywords",
joinColumns = @JoinColumn(name = "Catalog_Id")
)
@org.hibernate.annotations.MapKey(
columns = @Column(name="Shop_Category_Id")
)
@Column(name = "Keywords")
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Map<ShopCategory, String> categoryToKeywordsMap;

But the cascade delete is not working. Apparently it only applies to @OneToMany or @ManyToMany. So I want to adapt your @OneToMany annotations to get this working.

Many thanks
Jeremy


jasonwastaken wrote:
Ah, I see. I've mapped Maps in Hibernate, but it was a Map<String, List<Foo>> (simple type to list of user type), so I don't if this applies to you but here goes.

I had to create a class, call it FooList, to encapsulate the List<Foo>, then - in the class with the Map member - declare it as:

@OneToMany(cascade=CascadeType.ALL)
private Map<String, FooList> foos;

I never found a way to directly do Map<String, List<Foo>>. HTH.


Top
 Profile  
 
 Post subject: Re: Using @OneToMany for a Hashmap + cascade delete
PostPosted: Tue Apr 14, 2009 11:28 am 
Newbie

Joined: Thu Jul 31, 2008 2:15 am
Posts: 7
Location: LONDON
Hi,

Thanks for your fast reply.

You just used this single annotation? How did you specify the column names, join table etc.? Could you provide the code snippet which specifies the map too?

Many thanks
Jeremy

jasonwastaken wrote:
I used

@OneToMany(cascade=CascadeType.ALL)

jcolton wrote:
Hi Jason,

Can you plese provide the full annotaitons you used for your java map? So far I have used:

@org.hibernate.annotations.CollectionOfElements
@JoinTable(
name = "zad_category_keywords",
joinColumns = @JoinColumn(name = "Catalog_Id")
)
@org.hibernate.annotations.MapKey(
columns = @Column(name="Shop_Category_Id")
)
@Column(name = "Keywords")
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Map<ShopCategory, String> categoryToKeywordsMap;

But the cascade delete is not working. Apparently it only applies to @OneToMany or @ManyToMany. So I want to adapt your @OneToMany annotations to get this working.

Many thanks
Jeremy


jasonwastaken wrote:
Ah, I see. I've mapped Maps in Hibernate, but it was a Map<String, List<Foo>> (simple type to list of user type), so I don't if this applies to you but here goes.

I had to create a class, call it FooList, to encapsulate the List<Foo>, then - in the class with the Map member - declare it as:

@OneToMany(cascade=CascadeType.ALL)
private Map<String, FooList> foos;

I never found a way to directly do Map<String, List<Foo>>. HTH.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 15, 2009 8:39 am 
Newbie

Joined: Fri Apr 10, 2009 11:26 am
Posts: 5
Hi,

I tried the Map of Sets (using MultiMap) solution that you pointed to.
See below for the mapping. In spite of cascade option set the AclObject is not getting saved and i get the following errors
“object references an unsaved transient instance - save the transient instance before flushing”

Code:
<class>
...
<map name='acl' table='GROUP_OBJECTS' cascade='save-update,delete' collection-type='MultiMapType'>
<key column='GROUPID' />
<map-key column='OBJECTTYPEID' type='objectType'/>
<one-to-many class='AclObject' />
</map>
</class>


<class name='AclObject' table='GROUP_OBJECTS' lazy='false'>
<id name='id' column='ID' type='java.lang.Integer'>
<generator class='increment'/>
</id>
<property name='groupId' column='USERGROUPID' type='java.lang.Integer' not-null='true'/>
<property name='objectType' column='OBJECTTYPEID' type='objectType' not-null='true'/>
<property name='objectId' column='OBJECTID' type='java.lang.String' not-null='true'/>
</class>


Any pointers would be of great help
Also, if you could elaborate on your approach (Map<String, FooList>).

Thanks


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