Issue with a List<Long> using JPA

1,311 views
Skip to first unread message

Claw

unread,
Jul 11, 2011, 6:01:40 AM7/11/11
to play-framework
I am working on a project using the Play!Framework, and I an
encoutering some issues with JPA. It seems that it doesn't save my
List, or doesn't manage to retrieve it from the DB.

Here is my User class :

@Entity
public class User extends Model {
public String login;
public String password;
public String name;
public String email;
@ElementCollection
public List<Long> eventStreamIds;
@Transient
UserEventBuffer eventBuffer;

public User(String login, String password, String name, String
email, ArrayList<Long> eventStreamIds) {
this.login = login;
this.password = password;
this.name = name;
this.email = email;
this.eventStreamIds = eventStreamIds;
UserEventBuffer eventBuffer = new UserEventBuffer();
}
}

I have another class, a ModelManager, which constructor is as
following :

public ModelManager() {
User u = new User("user1", "pwd", "Alex", "te...@test.com");
EventStreamMC eb = new EventStreamMC("http://www.wservice.com/
stream1");
streams.add(eb);
eb.save();
u.eventStreamIds.add(eb.id);
Logger.info("before : " + u.eventStreamIds.size());
u.save();
User u2 = User.find("byLoginAndPassword", "user1", "pwd").first();
Logger.info("after : " + u2.eventStreamIds.size());
}

(My EventStreamMC also extends Model and has an @Entity tag, so its
Long id is automatically generated) When i run this code, here is the
result :

before = 1
after = 0

So the List is empty after the call to the find() method.

How can I make it work ?

Ronald Haring

unread,
Jul 12, 2011, 4:24:10 AM7/12/11
to play-framework
@ElementCollection should refer to embeddable objects so not objects
that have a lifecycle of their own. If you want to use entities that
you store yourself you should use a @OneToMany

Regards
Ronald
>     User u = new User("user1", "pwd", "Alex", "t...@test.com");

Claw

unread,
Jul 13, 2011, 10:31:58 AM7/13/11
to play-framework
I have already tried to use the @OneToMany relation, but here is what
I obtain while using it :

"A JPA error occurred (Unable to build EntityManagerFactory): Use of
@OneToMany or @ManyToMany targeting an unmapped class:
models.User.eventStreamIds[java.lang.Long]"

canavar

unread,
Jul 13, 2011, 3:57:09 PM7/13/11
to play-fr...@googlegroups.com

Other side of the relation must also a be an entity and you know Long is not an entity. Create a new model for storing the ids.

Robert Magnus

unread,
Jul 14, 2011, 3:15:29 AM7/14/11
to play-fr...@googlegroups.com
Hi

Am 13.07.2011 21:57, schrieb canavar:
> Other side of the relation must also a be an entity and you know Long is
> not an entity. Create a new model for storing the ids.

In JPA version 2 you could have a relation with primitiv types.

See
http://agoncal.wordpress.com/2009/11/01/mapping-and-querying-a-list-of-primitive-types-with-jpa-2-0/

reagrds,
Robert

--
Robert Magnus
akquinet tech@spree GmbH
Bülowstraße 66, D-10783 Berlin
Tel. +49 (0)30 235 520 - 44 Fax. +49 (0)30 217 520 - 12

Geschäftsführung: Martin Weber, Dr. Torsten Fink
Amtsgericht Berlin-Charlottenburg HRB 86780 B
USt.-Id. Nr.: DE 225 964 680

canavar

unread,
Jul 14, 2011, 3:53:09 AM7/14/11
to play-fr...@googlegroups.com

Vow! I didn't even know that. So what claw did was right. 

14 Tem 2011 10:15 tarihinde "Robert Magnus" <Robert...@akquinet.de> yazdı:

Ronald Haring

unread,
Jul 14, 2011, 4:50:42 AM7/14/11
to play-framework
As far as I know, you can have a ElementCollection using whatever, but
then you can not handle the lifecyle of those elements yourself.
You just set the list and save it's owner entity however claw is
saving entities using the save on the child object. Then you should
use @OneToMany(mappedBy = "user", cascade=CascadeType.ALL)
and add a user with @ManyToOne on the eventStreamId

But this really is more an issue for a jpa list instead of the play
list

Regards
Ronald

On Jul 14, 9:15 am, Robert Magnus <Robert.Mag...@akquinet.de> wrote:
> Hi
>
> Am 13.07.2011 21:57, schrieb canavar:
>
> > Other side of the relation must also a be an entity and you know Long is
> > not an entity. Create a new model for storing the ids.
>
> In JPA version 2 you could have a relation with primitiv types.
>
> Seehttp://agoncal.wordpress.com/2009/11/01/mapping-and-querying-a-list-o...

Claw

unread,
Jul 15, 2011, 4:18:50 PM7/15/11
to play-framework
Apparently my first version of the User class was right, now it works.
I don't really know why, but it works.

My problem now, is to find how to update my user (already saved) into
the database, after having added or removed some items of my
eventStreamIds list.

I have tried this.save() which gave me a
"org.hibernate.PersistentObjectException: detached entity passed to
persist" error.
I have tried this.merge(), I have no error but it doesn't seem to
update anything in the database :/
Reply all
Reply to author
Forward
0 new messages