Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Help with mapping a inherited relationship as a @OneToOne
Help with mapping a inherited relationship as a @OneToOne [message #533447] Thu, 13 May 2010 22:45 Go to next message
Chris Mathrusse is currently offline Chris Mathrusse
Messages: 24
Registered: July 2009
Junior Member
EclipseLink 2.0
GlassFish 3.0

I have a parent/child relationship going on in the database that I inherited. The relationship is actually using inheritance, although the constraints are not present in the database. Rather than defining this relationship in Java using inheritance, I want to define a has-a relationship instead.

Parent table of Party, which has a PrimaryKey defined as follows:
public class Party {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="PartyID")
	protected Integer id;
....
}


One of the children is Lead and is defined as follows:
public class Lead {
	@Id
	@Column(name="LeadId")
	private Integer id;

    @JoinColumn(name = "LeadId", referencedColumnName = "PartyID", nullable = false, updatable = false)
    @OneToOne(optional = false)
    private Party party;  
}


My problem is that EclipseLink is complaining with the following:
Exception Description: Multiple writable mappings exist for the field [Lead.LeadId].  Only one may be defined as writable, all others must be specified read-only.
Mapping: org.eclipse.persistence.mappings.OneToOneMapping[party]
Descriptor: RelationalDescriptor(com.intraxinc.core.persistence.entity.Lead --> [DatabaseTable(Lead)])


So I know that I have something incorrectly defined but I need help with getting this relationship defined correctly. I know I'm overlooking something, probably very small. I know that the Lead class cannot have a generated Id field as it must be the same as the Party.partyId.

Thanks for the help....

Re: Help with mapping a inherited relationship as a @OneToOne [message #533589 is a reply to message #533447] Fri, 14 May 2010 16:13 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1036
Registered: July 2009
Senior Member
Hello,

The problem is that the "LeadId" database column is still insertable from both the id and party attributes. As you are marking the party mapping as updateable=false, you probably want to also mark it as insertable=false. The draw back to this though is if using a shared cache, you will need to refresh the Lead objects after a flush/commit when they are inserted to have this relationship populated.

If you are able to use the new JPA 2.0 API, the mapsId annotation is made for this. With it, Lead's id attribute becomes controlled through the mapping (effectively making the id as insertable/updateable=false) but will automatically populate it with Party's Id attribute. You will not need to persist a Party and flush it to get the id to set in the Lead object, EclipseLink will know to get it once it is available.

Best Regards,
Chris
Re: Help with mapping a inherited relationship as a @OneToOne [message #533648 is a reply to message #533589] Fri, 14 May 2010 22:11 Go to previous messageGo to next message
Chris Mathrusse is currently offline Chris Mathrusse
Messages: 24
Registered: July 2009
Junior Member
Thanks for the response. This sounds like good news to me. I am using the JPA 2.0 API so I'll give this a try. Just to be clear, what you are proposing is the following:

public class Lead {

	@Id
	@Column(name="LeadId")
	private Integer id;

    @OneToOne(optional = false)
    @MapsId("id")
    private Party party;  
}


Am I understanding this correctly? There is no need to define a composite EmbeddedId correct?

Thanks again...
Re: Help with mapping a inherited relationship as a @OneToOne [message #534274 is a reply to message #533447] Tue, 18 May 2010 14:43 Go to previous messageGo to next message
Karen Butzke is currently offline Karen Butzke
Messages: 220
Registered: July 2009
Senior Member
@Entity
public class Lead {
@Id
private Integer id;

@OneToOne(optional = false)
@JoinColumn(name = "LeadId")
@MapsId("id")
private Party party;
}

Chris,
Here is how you should specify the MapsId. You specify the column information on the relationship and specify nothing on the id mapping. You can also take advantage of the MapsId default if you want and just specify @MapsId instead of MapsId("id")

If you're interested in a UI and design time validation for your JPA project you could use the Dali project . Given your last proposal for the Lead entity, Dali would give you a validation error on the Column annotation: "In attribute 'id', a column is specified, but it is mapped by a relationship. IDs that are mapped by a relationship should not specify a column."

Thanks,
Karen

Re: Help with mapping a inherited relationship as a @OneToOne [message #534902 is a reply to message #534274] Thu, 20 May 2010 15:20 Go to previous messageGo to next message
Chris Mathrusse is currently offline Chris Mathrusse
Messages: 24
Registered: July 2009
Junior Member
Thanks so much for the clarification. I'm guessing the default for @MapsId is to simply use the field defined with @Id. Am I correct?

Isn't the Dali project incorporated into Eclipse Java EE IDE? To be honest, I've been bouncing back and forth between Eclipse and NetBeans. Personally, I love Eclipse as it feels much more feature rich and polished as opposed to NetBeans. However, NetBeans does do some things much better than Eclipse, one of which is Entity generation.
Re: Help with mapping a inherited relationship as a @OneToOne [message #534943 is a reply to message #534902] Thu, 20 May 2010 18:10 Go to previous message
Karen Butzke is currently offline Karen Butzke
Messages: 220
Registered: July 2009
Senior Member
Yes, the default for @MapsId is the field defined with @Id or @EmbeddedId.

Dali is part of the Eclipse Java EE IDE, if you have created a JPA Project in Eclipse you are using Dali. I'm curious if you have used the most recent version of Entity generation. It was redone in Dali 2.2 which was included in the Eclipse 3.5 release, also available in the upcoming 3.6 release. It allows you to customize your relationships, entity names, attribute names, set up generators, etc. It also saves the wizard state if you want to regenerate.

We'd appreciate any feedback over on the Dali forum or in bugzilla, especially since you've worked in multiple IDEs.

Thanks,
Karen

Previous Topic:Performance Issue
Next Topic:valid JPQL creates invalid Derby SQL.
Goto Forum:
  


Current Time: Fri Oct 31 23:53:44 GMT 2014

Powered by FUDForum. Page generated in 0.01667 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software