Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Composite foreign primary key in 3 tables

Hello,

The example you are giving does not have the field names in the pk class matching the Entities Id field names as required otherwise it has no way of matching them up. 
 Channel which has a Device dev as part of its composite pk, ChannelPK has long device_id.  The type needs to be the same as Dev's pkclass (which is it) and the field name needs to
be 'dev' to match the relationship in Channel.  Same goes for NumericalData and DataPK except that the types there are also wrong.  DataPK needs to have a field
   ChannelPK chan; instead of device_id and channel_index fields.

Please see the examples given in the specification section 2.4.1.2, specifically example 2.

Best Regards,
Chris

On 28/06/2010 5:44 AM, Horat wrote:
Hi,

thanks for your help.

I tried your suggestion for the Primary-Key-classes. The previous error does
not occur anymore, but my example does still not work.

After updating to Eclipselink 2.1.0 I get an Excpetion while parsing the
PersistenceUnit:
Internal Exception: java.lang.ClassCastException:
org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.IdAccessor
cannot be cast to
org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ObjectAccessor

This error persists if I change the IdClass to an Embeddable and embed it in
Class NumericalData.

I have uploaded the actual Entity classes for this example, that correspond
to the scenario described in my first post. Additionally the orm.xml is
attached that contains all mappings.

http://old.nabble.com/file/p29008527/orm.xml orm.xml  Entity-Mappings
http://old.nabble.com/file/p29008527/Device.java Device.java  (Refers to
class A)
http://old.nabble.com/file/p29008527/Channel.java Channel.java  (Refers to
class B)
http://old.nabble.com/file/p29008527/ChannelPK.java ChannelPK.java  (Refers
to class BPK)
http://old.nabble.com/file/p29008527/NumericalData.java NumericalData.java 
(Refers to class C)
http://old.nabble.com/file/p29008527/DataPK.java DataPK.java  (Refers to
class CPK)

It is really frustrating to see Eclipselink not working with these very
simple relations.

Thanks again for help.





christopher delahunt wrote:
  
Hello,

The specification states you should have A's id within the BPK class, 
not A itself.  The same goes for CPK, it should contain int time and BPK 
b as attributes.
Please feel free to file a bug to have the error message improved if 
changing this resolves the problem.

Best Regards,
Chris

On 24/06/2010 3:15 AM, Horat wrote:
    
Hello, 

I have three tables:

Table A:
id (PK)

Table B:
aid (FK from A, PK)
num (PK)

Table C:
aid (FK from A, PK)
bnum (FK from B, PK)
time (PK)


With eclipselink i have annotated the ManyToOne-relation from B to A as
follows:
@Entity
public class A {
 @Id
 public int id;
}

@IdClass (BPK.class)
@Entity
public class B {
 @Id
 @ManyToOne
 @JoinColumn (name="aid" referencedColumnName="id")
 public A a;

 @Id
 public int num;
}

With BPK as the IdClass containing a and num.

This does work as intended!

For table C I annotated the ManyToOne-relation from C to B as follows:

@IdClass (CPK.class)
@Entity
public class C {
 @Id
 @ManyToOne
 @JoinColumns ({ @JoinColumn (name="aid", referencedColumnName="aid"),
@JoinColumn(name="bnum", referencedColumnName="num") })
 public B b;

 @Id
 public int time;
}

With CPK being the IcClass containing B and time.

When I try to run this I get an error:
org.eclipse.persistence.exceptions.ValidationException
Exception Description: The composite primary key attribute [b] of type
[null] on entity class [class C] should be of the same type as defined on
its primary key class [CPK]. That is, it should be of type [B].

I don't get it why the type of b is null. Obviously it is not.

Am I missing out anything?

Thanks for replying.
  
      
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


    
  

Back to the top