Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Advice request for storing a List of Enum values

BasicCollection with a Converter should work for this.  If you can make the
values the enum string instead of ids, you could use the EnumTypeConverter. 
Otherwise, you can use an ObjectTypeConverter mapping the enum type to the
ids value (you will need to hard-code the ids, although you could load the
values from the db in your customizer).  You will need to define the
ObjectTypeConverter using a DescriptorCustomizer most likely.



Martijn Morriën wrote:
> 
> Hi, I am looking for implementation tips for storing a List of Enum
> values.
> 
> I have been reading the
> http://dev.eclipse.org/mhonarc/lists/eclipselink-users/msg00915.html
> thread, it gives me some options but I'm not sure what path to take. I
> will try to explain my model first and then I list a number of
> concerns I have.
> 
> I want to have a model like:
> 
> User --* Role *-- Action
> 
> 
> public class User {
>     private List<Role > roles;
> 
>     public List<Role > getRoles() {
>         return roles;
>     }
> 
>     public void setRoles(List<Role > value) {
>         this.roles= value;
>     }
> }
> 
> public class Action {
>     private List<Role > roles;
> 
>     public List<Role > getRoles() {
>         return roles;
>     }
> 
>     public void setRoles(List<Role > value) {
>         this.roles= value;
>     }
> }
> 
> public enum Role {
>     admin,
>     user
> }
> 
> I want the database to look like:
> 
> Table User
> - Column id
> 
> Table Action
> - Column id
> 
> Table User_Role
> - Column userId
> - Column roleId
> 
> Table Action_Role
> - Column actionId
> - Column roleId
> 
> Table Role
> - Column id (Enum string value, primary key)
> 
> 
> - The Role table will be manually filled with 2 rows: 'admin' and
> 'user' when the database is created.
> - The User_Role and Action_Role will have foreign key constrains to
> the relevant tables.
> - With this approach I ensure database integrity.
> 
> Should I use a BasicCollection (like the advice given here:
> http://dev.eclipse.org/mhonarc/lists/eclipselink-users/msg00917.html)
> the way to go? If so, is there somewhere an example which uses Enums?
> Or do I need to fall back on a Class mapping, which is portable to
> other JPA implementations, but requires me to convert class values to
> java enums?
> 
> Thank you,
> 
> Martijn
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/Advice-request-for-storing-a-List-of-Enum-values-tp20769478p20774484.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top