[2.0] Ebean OneToOne Simple example not working

940 views
Skip to first unread message

André

unread,
May 24, 2012, 1:07:29 PM5/24/12
to play-framework
Hi!

I want to model the following:

I have a Machine and a MachineView (they have a relationship
OneToOne)..

in the table I want something like
Machine (id_machine, machineName)
id_machine is pk
MachineView (id_machine_view, description)
id_machine_view is pk; id_machine_view is foreign key referencing
(Machine.id_machine)

I was following the example described in the example 2 in: (and in
others sites as well)
http://docs.oracle.com/javaee/6/api/javax/persistence/OneToOne.html

so I coded in Machine.class:
@Entity
public class Machine extends Model{
@Id
public Long idMachine;

@Constraints.Required
public String machineName;

@OneToOne
public MachineView view;

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

public static List<Machine> all() {
return find.all();
}
}

and the MachineView.class:
@Entity
public class MachineView extends Model {
@Id
public Long idMachineView;

public String description;
}

This code create the tables similar as I want (it created another
column in machine referencing the pk column in machineView). I want to
access the machineView data in Java as machine.view.

Is this code wrong?

I have filled the database manually to test, but when I try to
retrieve the MachineView data the object returns always null.

code:
List<Machine> machines = Machine.all();
JsonNode result = Json.toJson(machines.get(0).view);
return ok(result);

(I am sure that there is one machine in the db as well a machineview
and the primary key, foreign key relationship are ok in db)

Giving up this approach I tried to use embeddable approach where
MachineView is embeddable class. It works fine. Until I decide to
create a new attribute (image) in MachineView with has a relationship
with another table.

in MachineView.class i added:
@ManyToOne
public Image image;

in Image.class:
@Entity
public class Image extends Model {
@Id
public Long idImage;

public String description;
}

But after create this relationship in the embeddable class the error
was
"RuntimeException: Error reading annotations for models.MachineView"

Any suggestions of how Can I do these kind of relationships in Play
2?


Thanks in advance.

André

Marco P.

unread,
May 24, 2012, 4:05:10 PM5/24/12
to play-fr...@googlegroups.com
About your first example, I think is all correct but you have to fetch the details table. A good help comes from Ebean manual.
Hope this can help. Marco.

André

unread,
May 25, 2012, 8:47:49 AM5/25/12
to play-framework
Hi,

What do you mean with "fetch the details table"?
I thought that this would be automatic since I have data in the
database and the correct associations between the classes.

At least, with the OneToMany/ManyToOne bidirectional association was
working. I have a room, and in this room I have many computers (a list
of computers)... When I access a room (with "Room.all()"), I am able
to access any computer of the list.. as well when I have a computer I
can access the room with the simple code "machine.room" without any
special code for this.

btw.. The Ebean user guide didn't help me, it talks a lot about querys
and so on.. I guess what I want is simplier..

Thanks

André.

fielding

unread,
Jan 7, 2013, 7:15:43 PM1/7/13
to play-fr...@googlegroups.com
i ran into the same issue.
For one to one relationship, there are two ways to fetch reference objects.

One is fetching machineView explicitly.
Machine.find.fetch("machineView").find List()

The other is adding getter and setter to machine object manually.

Both work fine, but I am not clear how it works internally. FetchType seems not working for ebean. Based on play! suggestion, all fields keep public and no need to add getter and setter manually. Does it mean we need specify all the join explicitly in the query?

Any answer should be appreciated.

Reply all
Reply to author
Forward
0 new messages