0

"Java Persistence 2.0, Final Release" on page 404 has the following example:

Example 3: One-to-one association from an embeddable class to another entity.

@Entity
public class Employee {
  @Id int id;
  @Embedded LocationDetails location;
...
}

@Embeddable
public class LocationDetails {
  int officeNumber;
  @OneToOne ParkingSpot parkingSpot;
...
}

@Entity
 public class ParkingSpot {
  @Id int id;
  String garage;
  @OneToOne(mappedBy="location.parkingSpot") Employee assignedTo;
...
}

I would like to have multiple LocationDetails inside Employee:

@Entity
public class Employee {
  @Id int id;
  @ElementCollection
  @CollectionTable(name="EMP_LOCATION") 
  Map<String, LocationDetails> locations;
...
}

How the entity ParkingSpot has to be changed to point back to embeddable LocationDetails inside the collection table EMP_LOCATION.

Should be

@OneToOne(mappedBy="location.parkingSpot") Employee assignedTo;

replaced be by @ElementCollection ?

Thank You!

romsky
  • 864
  • 1
  • 8
  • 11

1 Answers1

1

I have found the answer in the book: "Keith M., Schincariol M. - Pro JPA 2, 2nd Edition (The Expert's Voice in Java) - 2013" page 271.

A proviso about embeddable types is that if an embedded object is a part of an element collection, then the embedded object in the collection can only include mappings where the foreign key is stored in the source table. It can contain owned relationships, such as one-to-one and many-to-one, but it cannot contain one-to-many or many-to-many relationships where the foreign key is in either the target table or a join table. Similarly, it can’t contain other collection table-based mappings like element collections.

The solution would be to have LocationDetails as an entity, rather then an embedded object.

romsky
  • 864
  • 1
  • 8
  • 11