Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA - EmbeddedId with @ManytoOne

Tags:

java

maven

jpa

I have a problem with my code (obviously) and after many searches on Internet, I don't find an answer to my problem, so I ask my question here. I have this :

@Entity
public class Resident
{
    /** Attributes */
    @EmbeddedId
    private IdResident idResident;
     ...

@Embeddable
public class IdResident {
    @Column(name="NOM")
    private String nom;
    @ManyToOne
    @JoinColumn(name="CODE")
    private Port port;
  ...

@Entity
public class Port
{
    /** Attributes */
    @Id
    @Column(name="CODE")
    private String code;
    @Column(name="NOM")
    private String nom;
    ...

And I'm using Maven, I've write this in my persistence.xml :

<class>beans.Port</class>
<class>beans.Resident</class>   

But when i run the program, no matter what i've write, I have this :

Exception Description: The mapping [port] from the embedded ID class 
[class beans.IdResident] is an invalid mapping for this class. An embeddable class that
 is used with an embedded ID specification (attribute [idResident] from the source 
[class beans.Resident]) can only contain basic mappings. Either remove the non
 basic mapping or change the embedded ID specification on the source to be embedded.

I don't see where is my mistake, I think it's because of the IdResident class wich has an Entity object in it, but I don't know how to fiw it

like image 558
Carvallegro Avatar asked Mar 14 '12 15:03

Carvallegro


1 Answers

Error message you get explains it quite well, Embeddable that is used as an embedded id can contain only basic mappings, not relationships. In JPA 2.0 specification this is told with following words:

Relationship mappings defined within an embedded id class are not supported.

Just define attributes that are part of composite id in embeddable that is used as embedded id, and map relationships in entity itself (or in another embeddable and include mappings with @Embedded).

like image 174
Mikko Maunu Avatar answered Sep 22 '22 18:09

Mikko Maunu