-->
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: Hibernate and Collections
PostPosted: Mon Dec 12, 2005 2:44 pm 
Newbie

Joined: Thu Dec 01, 2005 10:24 am
Posts: 7
Hi,

I have this classes (and others similar examples), I want to use hibernate to persist them, but I don't find an example about how can I do it. There are two things in this classes:
1- I'm using hashtables, can I use hibernate to persist them?
2- I have a Collection in Users and Groups classes (in this case a hashtable, but if I can't use a hash I can change it for another collection) that has objects of User or Group type, and in Group I have a collection of Users type. Can I do the persistence with hibernate off the collecion Groups with the sentence save(...) and does it mean than I persist the collection of groups and users ?

public class User {
String name;
...
}

public class Users {
Hashtable users; // Collection of User
int c;
...
}

public class Group {
Usuarios users;
String mail;
...
}

public class Groups {
Hashtable group; // Collection of Group
int c;
...
}

Thanks,

Luciana


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 5:27 pm 
Newbie

Joined: Mon Dec 12, 2005 11:37 am
Posts: 14
No sure what you are trying exacltly. You can use Hashmaps in hibernate if that is the question.

or e.g. to map the Users and User class


<class name="Users">
<map name="users" table="User" fetch="join" lazy="false">
<key column="USER_ID"/>
<map-key column="username" type="string"/>
<element column="some-other column" type="string"/>
</map>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 10:34 am 
Newbie

Joined: Thu Dec 01, 2005 10:24 am
Posts: 7
Hi,

Thank you very much for answer my question.
Sorry about my english, that can be the reason for don't understanding what I want.

The first question was if I can use hashtable in hibernate. So, if I can use a hashmap that is similar it's ok!!

The second question was if I can use hibernate with the structures of classes I have, and if I do the persistence of Groups with hibernate that implies that I persist all the users too.

Thank you very much again.

Luciana


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 10:52 am 
Newbie

Joined: Mon Dec 12, 2005 11:37 am
Posts: 14
Well - let me ask you this - what exactly are you trying to store.

From your posts, it seems to me that basically what you are trying to do is to store "nested" groups i.e. one group can contain other groups. What is not clear to me is why you need a list of users, containing other users?

If the above is correct, I think what you need is something like:

class User {
private String userName;
private String userId;
....
}

class Group {
private List<User> users;
private LIst<Group> subGroups;
....
}

If this is the case, this is emminently doable in H.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 11:51 am 
Newbie

Joined: Thu Dec 01, 2005 10:24 am
Posts: 7
No, it is not that case... :( I think it is a little more complicated.

I want to store a collection of users and a collection of groups, that has users inside. I don't have a list of users containing users, and I'dont have nested groups.

I have the class User, that represents one User. The collection off users is the class Users, that has an object Hashtable that contains objects of User type.

public class User {
String name;
...
}

public class Users {
Hashtable users; // Collection of User
int c;
...
}

I have a class Group, that represents one Group and contains a collection of Users. The collection off groups is the class Groups, that has an object Hashtable that contains objects of Group type.

public class Group {
Users users;
String mail;
...
}

public class Groups {
Hashtable group; // Collection of Group
int c;
...
}

What I want to know is if I store the class Groups I'm storing all the users too... and how is the mapping between tables Groups and the class Groups, that only has a hashtable.

How I tell hibernate that when I want to store the class Groups it has to store all the objects of Group type, and all the users they have.

I hope I make it easier to understand... =)

Luciana


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 12:14 pm 
Newbie

Joined: Mon Dec 12, 2005 11:37 am
Posts: 14
Well firstly if you do the mappings correctly, H will store the Groups and all associated group and then all teh associated users etc etc.

One of the thinsg I still dont understand is why do you have a hashtable? isnt it just a List? and if it a hashtable, what is the key?

So basically you would need 4 tables? GROUPS -> GROUP -> USERS ->USER? if that correct?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 19, 2005 11:57 am 
Newbie

Joined: Thu Dec 01, 2005 10:24 am
Posts: 7
Hi,

I have three tables: Groups (with all de groups of the application), Users (with all de users of the application) and the table that do the "mapping" between the users and groups, so I know which users are in every group.

Hibernate makes me have a table for every class?

I always use hashtable because it is easier and more quickly too get an object.
The key in the hashtable of Users is the name of the user (string), and the key in the hashtable of Groups is the name of the group (string).

If I change this classes:

public class User {
String name;
...
}

public class Users {
Hashtable users; // Collection of User
int c;
...
}

public class Group {
Usuarios users;
String mail;
...
}

public class Groups {
Hashtable group; // Collection of Group
int c;
...
}


To:

public class User {
String name;
...
}


public class Group {
Hashtable users; /* or any collection */
String mail;
...
}

Then, I create a user but don't asign it to a group, then hibernate is not going to persist it and I lose it?

Thanks,

Luciana


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 20, 2005 10:43 am 
Newbie

Joined: Mon Dec 12, 2005 11:37 am
Posts: 14
With the new structure you can still save teh user without assigning it to a group.

So, you would do something like this:

User u = new User();

session.save(user);

// this should save teh user - no assignments

// now

Group g = session.load(Group.class, groupId)

g.getUsers().add(u);

session.save(g);

// saves the assignment


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 21, 2005 10:33 am 
Newbie

Joined: Thu Dec 01, 2005 10:24 am
Posts: 7
Then if I want to save all the groups in my system, and the users inside them I can do :

Groups g = new Groups();

/* I add some objects of type Group to my collection */

session.save(g)
/* and then I save all the collection */

or I have to iterate in my collection and save one by one?

Thanks for all your help.

Luciana


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 21, 2005 10:35 am 
Newbie

Joined: Mon Dec 12, 2005 11:37 am
Posts: 14
It should save the Collection as well. Give it a try - maybe with some dummy classes and see what happens. Then you can always ask the specific questions about what type of mappings etc you need.


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.