Reputation: 646
I see many examples of one-to-many relationship made by using @Embeddable like this:
@Entity
@Table(name = "profiles")
public class Profile {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@ElementCollection
@CollectionTable(name = "profile_addresses", joinColumns = @JoinColumn(name = "profile_id"))
private List<Address> addresses;
// getters, setters
}
@Embeddable
public class Address {
@Column(name = "zip_code")
private String zipCode;
// other fields
}
It works with such tables:
profiles: addresses:
| id | name | | profile_id | zip_code |
| 1 | Alex | | 1 | 95050 |
| 1 | 95109 |
But how can use in embeddable Address entity its own auto generated id? Foe example, in tables like this:
profiles: addresses:
| id | name | | profile_id | address_id | zip_code |
| 1 | Alex | | 1 | 1 | 95050 |
| 1 | 2 | 95109 |
Upvotes: 1
Views: 431
Reputation: 6732
When you define a primary key for an Embeddable, it's not an Embeddable anymore. An Embeddable is defined by it's parent entity and helds no own identity. That's the reason why Embeddables are only accessable via it's parent entity and cannot be dereferenced anyway.
See https://docs.oracle.com/javaee/6/api/javax/persistence/Embeddable.html
Upvotes: 2