Mapping Collection of String and Enum with Ebean (Play 2.0)
Asked Answered
C

2

10

I have problems mapping a Collection of Strings and Enums in my entities. I have followed different advices, but nothing seem to work. I am using PlayFramework 2.0 and the provided Ebean as ORM.

Here is an illustration class:

package models;

import java.util.*;
import javax.persistence.*;
import play.db.ebean.Model;

@Entity
@Table(name = "foo")
public class Foo extends Model {

    private static final long serialVersionUID = 1L;

    private enum FooBar {
        FOO, BAR;
    }

    @Id
    public Long id;

    @ElementCollection
    @Enumerated(EnumType.STRING)
    @CollectionTable(name = "bar_foobar", 
        joinColumns = @JoinColumn(name = "bar_id", 
            referencedColumnName = "id"))
    @Column(name = "foobar")
    public List<FooBar> fooBars;

    @ElementCollection(targetClass = String.class)
    @CollectionTable(name = "bar_strings", 
        joinColumns = @JoinColumn(name = "bar_id"))
    @Column(name = "string", nullable = false)    
    public List<String> listOfStrings;

    @Basic
    public List<String> listOfStrings2;

    // Attempt to circumvent the issue, but this gives a strange error
    //public String[] arrayOfString;
}

The generated DDL when the application is started looks like this:

create table foo (
id      bigint not null,
constraint pk_foo primary key (id))
;

I would expect to see both the tables bar_foobar and bar_strings being created, if the annotations were correct.

If using the arrayOfString variable, I get a weired error-message upon application launch (which related to a random entity, not necessarily Foo.class

PersistenceException: Error with [models.user.User] It has not been enhanced but it's superClass [class play.db.ebean.Model] is? (You are not allowed to mix enhancement in a single inheritance hierarchy) marker[play.db.ebean.Model] className[models.user.User]

I know I could wrap my Strings and Enums in entities, and use a @ManyToMany relationship, but the thought of it makes me shiver. Is there a bug here in Play 2.0 or Ebean (using v2.7.3)? Are there other ways I could solve my problem?

Crate answered 5/5, 2012 at 1:41 Comment(0)
C
1

Collections mapping is not implemented in Ebean yet. EBEAN-378 All you can do is implementing mapping yourself. The @PrivateOwned annotation can be used on the Foo side to ensure the Strings don't remain in the DB if they are removed from the collection.

Circumstance answered 22/12, 2012 at 9:27 Comment(0)
K
0

This is a known problem in 2.0 (link), but should be fixed in 2.0.1.

Edit: For clarity, with 'this' is was referring to the PersistenceException.

Keen answered 6/5, 2012 at 11:39 Comment(1)
Thank you for the reply. That is regarding the PersistenceException, right? I have actually tried upgrading to 2.0.1, and got the same exception after that. But maybe I need to build the project from scratch with 2.0.1 in order to have this solved? My main question was about the String/Enum mapping, sorry for adding a second question into the post.Crate

© 2022 - 2024 — McMap. All rights reserved.