Home » Eclipse Projects » EclipseLink » Multi-column, one-to-one, JPA 2.0 @EmbeddedId using @MapsId fails with read-only on @JoinColum
| | |
Re: Multi-column, one-to-one, JPA 2.0 @EmbeddedId using @MapsId fails with read-only on @JoinColum [message #671313 is a reply to message #671130] |
Tue, 17 May 2011 13:16 |
Chris Delahunt Messages: 1389 Registered: July 2009 |
Senior Member |
|
|
Hello Karsten,
I'm not sure I understood your distinction on multi-column nested ID classes. MapsId means the relationship maps the targeted ID column; so the relationship controls the field/targeted mapping. So mapping in your embeddable is then set to read-only - since its value and the field are controlled/set through the relationship. If you mark the join column(s) as read-only, you then will not have a writable mapping for the field.
The above is true regardless of the type of pk used in the relationship target.
Put it another way, if you have both set to be writable, and put in different values, what would you expect to consistently happen? I hope you wouldn't expect the relationship value to change, so it means that the relationship value must always trump the value in the embeddable - so the embeddable mapping is essentially read-only.
Best Regards,
Chris
|
|
|
Re: Multi-column, one-to-one, JPA 2.0 @EmbeddedId using @MapsId fails with read-only on @JoinColum [message #671919 is a reply to message #671313] |
Thu, 19 May 2011 11:10 |
Karsten Wutzke Messages: 124 Registered: July 2009 |
Senior Member |
|
|
Please have a look at the following example:
Mappings:
Roster.java: (single-column auto-ID PK)
@Entity
@Table(name = "Rosters")
@Name(value = "roster")
public class Roster implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
...
}
Group.java: (multi-column PK)
@Entity
@Table(name = "Groups")
@Name(value = "group")
public class Group implements Serializable
{
@EmbeddedId
private GroupId embeddedId;
...
}
GroupId.java: (Group ID class)
@Embeddable
public class GroupId implements Serializable
{
@Column(name = "round_id")
private Integer roundId;
@Column(name = "ordinal_nbr")
private Integer ordinalNbr = 1;
...
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
Participation.java: (multi-column PK join table!)
@Entity
@Table(name = "Participations")
@Name(value = "participation")
public class Participation implements Serializable
{
@EmbeddedId
private ParticipationId embeddedId;
@Column(name = "was_withdrawn")
private Boolean wasWithdrawn;
@ManyToOne
@JoinColumn(name = "roster_id", referencedColumnName = "id")
private Roster roster;
@ManyToOne
@JoinColumns(value = {@JoinColumn(name = "round_id", referencedColumnName = "round_id"), @JoinColumn(name = "group_ordinal_nbr", referencedColumnName = "ordinal_nbr")})
private Group group;
...
}
ParticipationId.java: (Participation ID class)
@Embeddable
public class ParticipationId implements Serializable
{
// Roster reference: single-column
@Column(name = "roster_id", insertable = false, updatable = false)
private Integer rosterId;
// Group reference: multi-column
@Column(name = "round_id", insertable = false, updatable = false)
private Integer roundId;
@Column(name = "group_ordinal_nbr", insertable = false, updatable = false)
private Integer groupOrdinalNbr;
...
}
When you look at the mappings in Participation and ParticipationId you can choose to either include the read-only (insertable = false, updatable = false) in the @Column or the @JoinColumn annotations. This is what I mean by option. You have to make a decision here.
However, for JPA 2.0 @EmbeddedId derived identities the situation is different, because JPA 2.0 allows nesting of ID classes:
@Entity
@Table(name = "Participations")
@Name(value = "participation")
public class Participation implements Serializable
{
@EmbeddedId
private ParticipationId embeddedId;
@Column(name = "was_withdrawn")
private Boolean wasWithdrawn;
@MapsId(value = "rosterId")
@ManyToOne
@JoinColumn(name = "roster_id", referencedColumnName = "id")
private Roster roster;
@MapsId(value = "groupId")
@ManyToOne
@JoinColumns(value = {@JoinColumn(name = "round_id", referencedColumnName = "round_id"), @JoinColumn(name = "group_ordinal_nbr", referencedColumnName = "ordinal_nbr")})
private Group group;
...
}
@Embeddable
public class ParticipationId implements Serializable
{
// Roster reference: single-column
@Column(name = "roster_id", insertable = false, updatable = false)
private Integer rosterId;
// Group reference: multi-column, BUT NO @JoinColumn choice!
@Embedded
private GroupId groupId;
...
}
I could still decide whether to put the read-only on the single-column relationship (@JoinColumn) or the @EmbeddedId PK field (@Column), but it's technically "impossible" for multi-column JPA 2.0 @EmbeddedId derived identities because of the nested ID class. This choice is essentially "replaced" by the @MapsId JPA or JPA provider policy.
That's all that there is to it. So, the @MapsId actually only implements the policy you mentioned for JPA 2.0 derived identities' multi-column relationships???
Karsten
|
|
|
Goto Forum:
Current Time: Thu Mar 28 17:03:17 GMT 2024
Powered by FUDForum. Page generated in 0.02576 seconds
|