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é