Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
945 views
in Technique[技术] by (71.8m points)

java - JPA - EmbeddedId with @ManytoOne

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...