Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] OneToMany unidirectional with composite PK

A unidirectional OneToMany cannot be used when the foreign key is part of the
target object's primary key.  The target Entity must be able to map all of
its Id fields, otherwise it is not an independent Entity.

You can model this using an ElementCollection mapping and make the target an
Embeddable.  JPA does not allow defining an Id in an Embeddable, but
EclipseLink will allow you to set the primary key using a
DescriptorCustomizer or the @PrimaryKey annotation.

Otherwise, keep the relationship as bi-directional, or use a JoinTable and
add a generated Id to the UData.


jandam wrote:
> 
> Please can you help me. I would like to have OneToMany unidirectional with
> composite PK.
> 
> Code:
> 
> public class UEmployee extends DataWithPropertyChange.Abstract implements
> Serializable, Cloneable {
> 
>     @Id
>     @Column(name = "ID")
>     private int id;
> 
>     @OneToMany(mappedBy = "employee", cascade = { CascadeType.MERGE/*,
> CascadeType.REMOVE*/, CascadeType.DETACH, CascadeType.PERSIST,
> CascadeType.REFRESH})
>     @OrderBy("dataType, azimuthDeg")
>     @PrivateOwned
>     private List<UData> dDataCollection;
> }
> 
> // composite PK: dataType, azimuthDeg, employee
> public class UData {
> 
>     @Id
>     @Column(name = "DATA_TYPE", nullable = false)
>     private int dataType;
> 
>     @Id
>     @Column(name = "AZIMUTH", precision = 4, scale = 0)
>     private float azimuthDeg;
> 
>     @Id
>     @JoinColumn(name = "ID", referencedColumnName = "ID")
>     @ManyToOne
>     private UEmployee employee;
> 
>     @Column(name = "DVALUE", precision = 4, scale = 0)
>     private float value;
> }
> 
> 
> I'm using latest release: Eclipselink 2.1.0.v20100614-r7608.
> 
> I can't use @JoinColumn because I get exception that I have to use
> @JoinColumns with all PK columns.
> 
> I would like to remove field 'UData.employee' and replace it with
> unidirectional mapping. Is it possible?
> 
> Thank you very much for help.
> 
> 


-----
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://old.nabble.com/OneToMany-unidirectional-with-composite-PK-tp29447772p29522545.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top