Hi,
I am having problems with the collection mapping in hibernate. The documentation does not explain enough for me. They only go through and explain the map and then do a few bits and pieces on the other ones throughout that. So anyways, it has made it difficult for me. I have spent the past 5 hours beating my head against this so I'm back with some questions.
1.) Where can I find a list of the required fields for the mappings so I can use it as reference.
Example:
A list mapping requires a list-index field, etc.
This will be handy when I choose what I want to do.
2.) What is a bag? I assume this is just some sort of generic mapping that does not order things and you can use it in conjunction with any collection implementation you use in your code. IE: You can have a list and then map it to a bag, OR you could have a set and map it to a bag.
3.) This is my specific problem. I have tried many different things so I will not provide a lot of detailed information and ask for an explaination of a simple example:
I have a class, lets say nyClass. In that class I have an ArrayList of strings called myList:
ArrayList<String> myList;
I have the setters and the getters for it.
I want to have TWO tables. I want to have a table for myClass and then I want to have a table for the contents of myList.
Like this:
If myClass had this:
myList = "a", "b", "c"
name = "myClass"
The table for myClass would look like so:
Code:
ID Name
1 myClass
The table for myList would look like:
Code:
myClassID Name
1 a
2 b
3 c
In my mapping file what do I use in order to save this to my DB?
Here are several things I have tried:
Code:
<list name="myList"
table="listTable">
<key column="listID"/>
<list-index column="sortOrder"/>
<element column="name" type="string"/>
</list>
This gives the following error:
Exception in thread "main" org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of events.Event.setMyList
I have also tried this:
Code:
<array name="myList" table="listTable">
<key column="myClass_id"/>
<list-index column="listID"/>
<element column="name" type="string"/>
</array>
This gives the following error:
Exception in thread "main" java.lang.IllegalArgumentException: Argument is not an array
I believe 'array' is for arraylist and 'primitive-array' is for a 'normal' array: int myint[10];
So, basically, I am not entirely sure what to do and any help with my questions would be great!