atastrophic
atastrophic

Reputation: 3143

How to Change Hibernate Collection Mapping from Set to List

I am using eclipse Tools to generate my Annotated Domain Code Classes. For the One to Many & Many to Many Relationships, the code generated used Set type for collections.

I want to change it to List or ArrayList. What should be my configuration in reveng.xml

Also, what are the standard conversion types between MySQL and Java. I mean like varchar is converted to string, int to int etc.

Can anyone share a pretty much standard reveng.xml file for type conversions...???

Upvotes: 3

Views: 4324

Answers (2)

sp00m
sp00m

Reputation: 48827

You shouldn't use List by default instead of Set. But if you need it punctually, that can help you:

public <T> List<T> fromSetToList(Set<T> set) {
    return new ArrayList<T>(set);
}

Upvotes: 1

Jesse Webb
Jesse Webb

Reputation: 45253

Also, what are the standard conversion types between MySQL and Java. I mean like varchar is converted to string, int to int etc.

For reference on Hibernate mappings, I found the following link helpful for basic scenarios. For more complex mappings, refer to the full hibernate documentation.

Hibernate Mapping Cheat Sheet

As for The List vs. Set, Set should actually be the Collection type you should use. The only difference between List and Set is that List implies order of the elements and Set does not allow duplicates. A simple DB record set does not have a specified order and it does not have duplicates, so a Set is appropriate. A List would be useful only if your query did specify order and/or you wanted some kind of UNION which may produce duplicates.

I don't know how to turn your Sets into Lists but I would encourage you to question if you actually want to do so.

Upvotes: 1

Related Questions