OneToOne relation is not fetched from DB correctly

719 views
Skip to first unread message

Thomas Felix

unread,
Feb 10, 2012, 9:42:10 AM2/10/12
to Ebean ORM
Hi everyone,

I am quite new to Hibernate/JPA/Ebean stuff but I kinda like how it
works and saves me from databases. I am building a web app with the
play!framework which uses ebean orm. Since 3 days now I am struggling
and maybe I am doing something utterly wrong but I have no clues what
it could be. And docs where not very helpful either, so maybe the pros
in here are my last hope:

I have a following model (sorry for the public properties, thats the
way play! uses the model to avoid some getter and setter but I am not
100% convinced by it...):

public class Player extends Model {
@Id
@GeneratedValue
public Long id;

@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
public Clan clan;
@OneToMany(cascade = CascadeType.PERSIST)
public List<Nickname> nicknames = new ArrayList<Nickname>();

public static Finder<Long, Player> find = new
Finder<Long,Player>(Long.class, Player.class);
}

and Clan is a simple:

@Entity
public class Clan extends Model {
@Id @GeneratedValue
public Long id;

public String name;

public Clan(String _name) {
name = _name;
}
}

The problem is if I create a new player, a new clan instance and save
it into the database, all ids/fk are set correctly (I checked using
and browsing a mysql db). I can even fetch the Clan from the database
when it stands alone. But if I do a Player p = Player.find.byId(1) the
p.clan is NOT null, it has a instantiated Object of type Clan, but the
name (an other fields if I put them there) are empty. What am I doing
wrong? The funny thing is, if I do it like this then p.nicknames is
populated with the correct nickname I have set and persisted in the
db. The problem seems to lie just in the @OneToOne annotation. Is it
wrong maybe? :(

best regards and many many thanks in advance,
Thomas Felix

Daryl Stultz

unread,
Feb 10, 2012, 10:24:37 AM2/10/12
to Ebean ORM

On Feb 10, 9:42 am, Thomas Felix <thomas.feli...@googlemail.com>
wrote:
> public class Player extends Model {

What does your Model class look like? Does it have a default
constructor?

>         public List<Nickname> nicknames = new ArrayList<Nickname>();

I think the above will give you surprises. You should not initialize
collections otherwise Ebean can't track membership changes.

/Daryl

Daryl Stultz

unread,
Feb 10, 2012, 10:26:06 AM2/10/12
to Ebean ORM

On Feb 10, 9:42 am, Thomas Felix <thomas.feli...@googlemail.com>
wrote:
> I have a following model (sorry for the public properties, thats the
> way play!

You'll need to use enhancement rather than subclassing to get this to
work, I imagine.

/Daryl

Thomas Felix

unread,
Feb 10, 2012, 3:46:40 PM2/10/12
to Ebean ORM
Thanks for your reply. I have forgotten to mention this, play! uses
the Model for providing some convienence methods. Currently there are
no java
doc of the new play 2.0 version I am using but its not that big:
https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/java/play/db/ebean/Model.java
And yes it declares no constructor and therefor has a default one.
Maybe the
next thing I try would be to go without subclassing and using the
plain
Player class...

> I think the above will give you surprises. You should not initialize
> collections otherwise Ebean can't track membership changes.

Thank you for pointing this out, I will change it. =)

But my annotations look good so far?

Greetings,
Thomas

Daryl Stultz

unread,
Feb 10, 2012, 4:15:54 PM2/10/12
to Ebean ORM

On Feb 10, 3:46 pm, Thomas Felix <thomas.feli...@googlemail.com>
wrote:
> But my annotations look good so far?

You don't need the fetch declarations, Ebean doesn't work like JPA in
this regard. You can use autofetch and have Ebean handle the
optimization and/or optimize each query quite easily.

/Daryl

Thomas Felix

unread,
Feb 10, 2012, 5:26:47 PM2/10/12
to Ebean ORM
Ok very interesting I have found the problem and maybe for your
interest: The corresponding
Model values are empty when accessed via the public member:

DOESN'T WORK:

Player p = new Player();
p.clan = new Clan("MyTest");
Ebean.save(p); //to this point it works perfectly. Saving into DB is
fine.
...
Player p2 = Ebean.find(Player.class, 1); //Assuming ID 1 was generated
with the save operation
System.out.println(p2.clan.name); //Is empty. (actually it somehow
crashes Play! framework and my browser looses connection to the
server, but p2.clan is not null its just an empty Clan object as far
as I could find out)

WORKS:

Player p2 = Ebean.find(Player.class, 1);
System.out.println(p2.getClan().getName());

When the member are private and correctly wrapped in getter-setter
calls the String is correctly read from the database. (if I subclass
my models from play.db.ebean.Model or not
makes no difference by the way, it stays or falls with using accessor
methods.

I am glad it works and I anyway like the usage of accessors more then
plain public members. But out of curiosity is this behaviour intended?

Sorry if I annoyed someone with my newbie like questions and many
thanks for the nice answers!

Thomas

Daryl Stultz

unread,
Feb 10, 2012, 9:04:08 PM2/10/12
to Ebean ORM


On Feb 10, 5:26 pm, Thomas Felix <thomas.feli...@googlemail.com>
wrote:
> System.out.println(p2.clan.name); //Is empty. (actually it somehow
> crashes Play! framework and my browser looses connection to the
> server, but p2.clan is not null its just an empty Clan object as far
> as I could find out)

I'm not sure we've settled the issue of enhancement yet. Are you
enhancing your entities?

/Daryl

Thomas Felix

unread,
Feb 13, 2012, 6:44:18 PM2/13/12
to Ebean ORM
I checked and it seems the playframework provides an enhancer which
inserts the get-setter
automagic and some extra stuff. But sadly doing this stuff is fairly
new to me. Can you perhaps
recommend a good book to start with JPA/Hibernate/Ebean stuff?

Josh Kamau

unread,
Feb 13, 2012, 11:41:27 PM2/13/12
to eb...@googlegroups.com
This is a good book.

Apress Pro JPA.2 Mastering the Java Persistence API

Regards.
Josh

Daryl Stultz

unread,
Feb 14, 2012, 7:06:37 AM2/14/12
to Ebean ORM


On Feb 13, 6:44 pm, Thomas Felix <thomas.feli...@googlemail.com>
wrote:
> new to me. Can you perhaps
> recommend a good book to start with JPA/Hibernate/Ebean stuff?

While Ebean uses the core JPA annotations for mapping, it differs
greatly from JPA. A good understanding of JPA is good, but I don't
think a JPA2 book would be money well spent. I bought a JPA2 book a
couple of years ago and it's what drove me to seek something better
(Ebean).

This is the place to start for Ebean:
http://www.avaje.org/doc/ebean-userguide.pdf

/Daryl
Reply all
Reply to author
Forward
0 new messages