[
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
|
Ok, I've got the BasicCollection with en Enum working with my desired
table model.
For the record:
============= SQL ==========
create table USER1 (
id serial not null,
primary key (id)
);
create table USER_ROLE (
userId serial not null,
role varchar not null,
primary key(userId, role)
);
create table ROLE (
role varchar not null,
primary key(role)
);
alter table USER_ROLE add foreign key (userId) references USER1 (id);
alter table USER_ROLE add foreign key (role) references ROLE (role);
insert into user1 values (1);
insert into role values ('admin');
insert into role values ('user');
insert into user_role (userId, role) values (1, 'admin');
insert into user_role (userId, role) values (1, 'user');
============= Classes ============
public enum Role {
admin,
user
}
@Entity
@Table(name = "user1")
@ObjectTypeConverter (
name = "roleEnumFromStringConversion",
objectType = Role.class,
dataType = String.class,
conversionValues = {
@ConversionValue(objectValue = "admin", dataValue = "admin"),
@ConversionValue(objectValue = "user", dataValue = "user")
}
)
public class User implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private long id;
private List<Role> roles;
@Id
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@BasicCollection (
fetch=FetchType.EAGER,
valueColumn=@Column(name="ROLE")
)
@CollectionTable (
name="USER_ROLE",
primaryKeyJoinColumns={
@PrimaryKeyJoinColumn(
name="USERID",
referencedColumnName="ID")
}
)
@Convert("roleEnumFromStringConversion")
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
public String toString() {
return "" + id;
}
}
public class Test {
@SuppressWarnings("unchecked")
public static void main(String args[]) throws Throwable {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("bodega");
EntityManager em = emf.createEntityManager();
// Get all users
String queryString = "SELECT u FROM User u";
Query query = em.createQuery(queryString);
List<User> users = query.getResultList();
// persist a new User
EntityTransaction tx = em.getTransaction();
tx.begin();
ArrayList<Role> roles = new ArrayList<Role>();
roles.add(Role.admin);
User newUser = new User();
newUser.setId(users.size() + 1);
newUser.setRoles(roles);
em.persist(newUser);
tx.commit();
// Get all users
queryString = "SELECT u FROM User u";
query = em.createQuery(queryString);
users = query.getResultList();
for (User user : users) {
System.out.println("#user: " + user);
for (Role role : user.getRoles()) {
System.out.println("#role: " + role);
}
}
em.close();
}
}
========== Persistence.xml ============
Nothing special:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="bodega">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>testjpa.User</class>
<properties>
<property name="eclipselink.target-database" value="PostgreSQL"/>
<property name="eclipselink.jdbc.driver"
value="org.postgresql.Driver"/>
<property name="eclipselink.jdbc.url"
value="jdbc:postgresql://localhost:5432/postgres"/>
<property name="eclipselink.jdbc.user" value="postgres"/>
<property name="eclipselink.jdbc.password" value="postgres"/>
</properties>
</persistence-unit>
</persistence>
Martijn
On Mon, Dec 1, 2008 at 4:56 PM, James Sutherland <jamesssss@xxxxxxxxx> wrote:
>
> 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.
>
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>