我有EcranChamp
个实体
@Entity
@IdClass(EcranChampId.class)
public class EcranChamp {
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ecran")
Ecran ecran;
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "champ")
Champ champ;
...
还有EcranChampId
@Embeddable
public class EcranChampId implements Serializable {
private Champ champ;
private Ecran ecran;
...
每次尝试保存EcranChamp
元素时,都会出现以下错误
2018-09-25 12:15:42.889 WARN 14216 --- [nio-8092-exec-8] .wsmsDefaultHandlerExceptionResolver:未能转换请求元素:org.springframework.beans.ConversionNotSupportedException:未能转换属性值属性'ecran'的类型'java.lang.Long'到必需的类型'com.kepler.portailclient.domain.model.Ecran';嵌套异常为java.lang.IllegalStateException:无法将类型“ java.lang.Long”的值转换为属性“ ecran”的必需类型“ com.kepler.portailclient.domain.model.Ecran”:找不到匹配的编辑器或转换策略 2018-09-25 12:15:42.889 WARN 14216 --- [nio-8092-exec-8] .wsmsDefaultHandlerExceptionResolver:由处理程序执行引起的已解决异常:org.springframework.beans.ConversionNotSupportedException:无法转换类型的属性值“ java.lang.Long”为属性“ ecran”的必需类型“ com.kepler.portailclient.domain.model.Ecran”;嵌套异常为java.lang.IllegalStateException:无法将类型“ java.lang.Long”的值转换为属性“ ecran”的必需类型“ com.kepler.portailclient.domain.model.Ecran”:找不到匹配的编辑器或转换策略
答案 0 :(得分:1)
尝试这样的事情:
@Entity
@Table(name = "<entity name>")
public class EcranChamp {
@EmbeddedId
@AttributeOverrides({ @AttributeOverride(name = "id_ecran", column = @Column(name =
"<column name>", nullable = false)),
@AttributeOverride(name = "id_champ", column = @Column(name = "<column name>", nullable = false)) })
EcranChampId id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_ecran")
Ecran ecran;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_champ")
Champ champ;
//getters & setters
}
@Embeddable
public class EcranChampId implements Serializable {
@Column(name = "id_champ", nullable = false)
private Long id_champ;
@Column(name = "id_ecran", nullable = false)
private Long id_ecran;
//getters & setters
}
答案 1 :(得分:0)
您应该使用@EmbeddedId
注释。
请将您的EcranChampId
类更改为此:
@Embeddable
public class EcranChampId implements Serializable {
@ManyToOne
private Champ champ;
@ManyToOne
private Ecran ecran;
//getters and setters
}
然后将您的EcranChamp
类更改为此:
@Entity
@Table(name = "champ_has_ecran_table_name")
@AssociationOverrides({
@AssociationOverride(name = "pk.champ", joinColumns = @JoinColumn(name = "champ_id"))
@AssociationOverride(name = "pk.ecran", joinColumns = @JoinColumn(name = "ecran_id"))
})
public class EcranChamp {
@EmbeddedId
private EcranChampId pk;
public EcranChamp() {
pk = new EcranChampId();
}
public EcranChampId getPk() {
return pk;
}
public void setPk(EcranChampId pk) {
this.pk = pk;
}
@Transient
public Champ getChamp() {
return pk.getChamp();
}
public void setChamp(Champ champ) {
pk.setChamp(champ);
}
@Transient
public Ecran getEcran() {
return pk.getEcran();
}
public void setChamp(Ecran ecran) {
pk.setEcran(ecran);
}
}
并像这样使用它:
public class Champ {
@OneToMany(mappedBy = "pk.champ")
private Collection<EcranChamp> ecranChamps;
//getters and setters
}
此外,如果EcranChamp
或EcranChampId
没有其他字段,我建议您使用@ManyToMany
注释代替EcranChamp
类,如下所示:
public class Champ {
@ManyToMany
@JoinTable(
name = "champ_has_ecran_table_name",
joinColumns = @JoinColumn(name = "champ_id", referencedColumnName = "id", nullable = false),
inverseJoinColumns = @JoinColumn(name = "ecran_id", referencedColumnName = "id", nullable = false)
)
private Collection<Ecran> ecrans;
//getters and setters
}