-->
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.  [ 1 post ] 
Author Message
 Post subject: Mapping Collection of Enum types that implement interface
PostPosted: Thu Apr 28, 2011 6:44 pm 
Beginner
Beginner

Joined: Wed Nov 21, 2007 10:24 am
Posts: 25
I've got a tricky thing I'm trying to map with Enums for storing static data.

It starts off with enum classes declared as such:
Code:
interface MyInterface {
String getName();
}

public enum A implements MyInterface {
FIRST_A("First A");
private final String name;
private A(String name) {
this.name = name;
}
public String getName() {
return name;
}

public String toString() {
return name;
}
}

public enum B implements MyInterface {
FIRST_B("First B");
private final String name;
private B(String name) {
this.name = name;
}
public String getName() {
return name;
}

public String toString() {
return name;
}
}


Now I want to use these as:
Code:
List<MyInterface> elements


The problem comes in mapping the collection. I can persist these just fine normally if I use a CompositeUserType I've written for writing the class of the enum being persisted as well as the String value that would come from persisting an @Enumerated with the EnumType.STRING. In those cases I can declare:

Code:
@Type(type="MyCustomEnumType.class")
@Columns(columns = {
         @Column(name = "enumValue"), @Column(name = "enumClass")
})
private Enum<?> enumValue;


In that case I can persist them just fine, but I'm uncertain if using @Type on the collection will make all items inside that collection persist using that custom type. Should I use @ElementCollection as well on it since it's such that it's embeddable? Or do I use the user type class I've written as the targetClass of the collection? I'm a bit confused on how it works for something like that. I want to keep it generic enough to persist A or B possibly in the collection (although it will end up being only one type per entity persisted, but could be either).

I'd prefer not to have to set this up as an Entity class due to it being static data that won't change, but may have a new version in the future which would end up being another enum.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.