Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Primary Key and one-to-may relatuioship
Primary Key and one-to-may relatuioship [message #389796] Tue, 07 July 2009 15:08 Go to next message
Enrico is currently offline EnricoFriend
Messages: 82
Registered: July 2009
Member
Dear all,

can be a property both an @id and the field for @onetomany relatioship.
An example can be more clear:

I have two Entity: A and B. A entity is related to many B entities.

@Entity
@Table(name="APP.CFG")
public class A implements {
int id;;

//jpa relationship
private List<B> Bs;

@Id
public int getId() {
return id;
}

................

@OneToMany(cascade=CascadeType.ALL,
mappedBy="a",fetch=FetchType.LAZY)
public List<B> getBs() {
return Bs;
}

......

}


@IdClass(BPK.class)
@Entity
public class B {
private id;
private A a;


@Id
public int getId() {
return id;
}

@Id
@ManyToOne
@JoinColumn(name="a", nullable=false)
public A getA() {
return a;
}

......
}


So, Entity B has a composite-key (but can an entity be a fied of a
primary key?or can i have only primitive types?) and one of its field
(a) is both @id and also @manytoone.
Is this possible?I get always the following error:

AN INCOMPATIBLE MAPPING HAS BEEN ENCOUNTERED BETWEEN [CLASS A] AND
[CLASS B]. THIS USUALLY OCCURS WHEN THE CARDINALITY OF A MAPPING DOES
NOT CORRESPOND WITH THE CARDINALITY OF ITS BACKPOINTER.

Thanks in advance,
Enrico
Re: Primary Key and one-to-may relatuioship [message #390026 is a reply to message #389796] Wed, 08 July 2009 13:40 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

In JPA 1.0, EclipseLink 1.1 @Id is not allow on ManyToOne relationships.
In JPA 2.0, EclipseLink 2.0, this is allowed.

For JPA 1.0 see,
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_S equencing#Primary_Keys_through_OneToOne_Relationships

Basically you need to define a duplicate basic attribute for the foreign
key and map it as @Id and insertable/updateable=false.

---
James
http://www.nabble.com/EclipseLink---Users-f26658.html


James : Wiki : Book : Blog : Twitter
Re: Primary Key and one-to-may relatuioship [message #390031 is a reply to message #390026] Thu, 09 July 2009 12:13 Go to previous messageGo to next message
Enrico is currently offline EnricoFriend
Messages: 82
Registered: July 2009
Member
James ha scritto:
> In JPA 1.0, EclipseLink 1.1 @Id is not allow on ManyToOne
> relationships. In JPA 2.0, EclipseLink 2.0, this is allowed.
>
> For JPA 1.0 see,
> http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_S equencing#Primary_Keys_through_OneToOne_Relationships
>
>
> Basically you need to define a duplicate basic attribute for the foreign
> key and map it as @Id and insertable/updateable=false.
>
> ---
> James
> http://www.nabble.com/EclipseLink---Users-f26658.html
>

Thanks James for your reply.
I am using maven to build my project.
I am not able to get the eclipselink 2.0 Snapshot from Maven repository
(I know eclipselink 2.0 is yet under development).
I followed the instruction on the eclipselink wiki:
http://wiki.eclipse.org/EclipseLink/Maven

but it seems the repository does not work.
Do you know where I can get a maven repository to import eclipselink 2.0
maven artifact?

BR,
Enrico
Re: Primary Key and one-to-may relatuioship [message #390048 is a reply to message #390031] Mon, 13 July 2009 14:02 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

I don't use Maven myself, but know other people use, it so I assume it
should work. Perhaps someone else can give your more details or update
the wiki with more info.

You can get 2.0 builds from,

http://www.eclipse.org/eclipselink/downloads/nightly.php

or from the CVS reposity.


James : Wiki : Book : Blog : Twitter
Previous Topic:Eclipselink with OSGi
Next Topic:Thrown exceptions are useless
Goto Forum:
  


Current Time: Thu Apr 25 19:11:25 GMT 2024

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

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

Back to the top