简体   繁体   中英

Not Mapped to a Single Property Exception in JPA Mapping

Am having two models named Style and StyleExp with thier respective PK classes and am using JPA for mapping.

The Style class contains four variables namely pid, Sname, Screen, StyleId. and the class StyleExp has the varibales named StyleId,EId, sno,exp.

In the class Style pid, Sname, Scrn are primary keys and in the class StyleExp StyleId,EId are the primary keys. Am having one to many relation between these two classes.

I have provided the mapping like the following in Style class,

@Entity(name="Style")
@Table(name="Style")
@IdClass(StylePk.class)
public class Style implements Serializable{
@Id
private String pid;
@Id 
private String Sname;
private String styleId;
.....
@OneToMany(fetch=FetchType.Lazy, mappedBy="style")
protected List<StyleExp> styleExp;
}

In the class STyleExp I have provided the Mapping as follows,
@Entity(name="StyleExp")
@Table(name="StyleExp")
public class StyleExp implements Serializable{
@Id
private String styleId;
@Id
private String eId;
@ManyToOne(fetch=FetchType.Lazy)
@JoinColumns({
@JoinColumn(name="styleId", 
referencedColumnName="styleId",insertable=false,updatable=false)
})
protected Style style;
}

But when Am running these code as getting the List of StyleExp from Style class as

Style style = styleDao.getStyle(pid, Sname, Scrn)
List<StyleExp> styleExpList =  style.getStyleExp();

It throws the following error

causedBy : org.hibernate.AnnotationException: referencedColumnName(styleId) of StyleExp.style referencing model.Style not mapped to a single property

So please kindly let me know what mistake am doing? and one more doubt for me is non primary key and primary key OneToMany and ManyToOne mapping is possible in JPa? Because of the non primary key mapped to primary key is the problem in the above situation?

Kindly help me. Thanks in Advance.

I have tried by the following for OneToMany mapping between Style and StyleExp

@Entity(name="Style")
@Table(name="Style")
public class Style implements Serializable{
private String pid;
private String Sname;
@Id 
private String styleId;
.....
@OneToMany(fetch=FetchType.Lazy, cascade=CascadeType.ALL)
@JoinColumns({
@JoinColumn(name="styleId", referencedColumnName="styleId" insertable=false, updatable=false)
})
protected List<StyleExp> styleExp;
}

In the class StyleExp I have provided the Mapping as follows,
@Entity(name="StyleExp")
@Table(name="StyleExp")
public class StyleExp implements Serializable{
@Id
private String styleId;
@Id
private String eId;
}

By using the above code I can map the above two tables

You should only use @Id for primary key in your parent class Style . Other columns that you want to set it as unique, then doing as below:

  // primary key
    @Id
    @Column(name = "pid", unique = true, nullable = false)
    private String pid;
    // unique key
    @Column(name = "styleId", unique = true, nullable = false)
    private String styleId;

Then in your StyleExp class, you can map to the unique column styleId in Style class as below

 // you are referencing to unique column in parent table    
    @ManyToOne
    @JoinColumn(name = "styleId", nullable = false, referencedColumnName = "styleId")
        protected Style style;

The " referencedColumnName " is used when you want to map to a none primary column in parent table.

Anyway, no need to use multiple @Id in a table/model class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM