无法保存实体可嵌入ID

时间:2018-09-25 11:19:33

标签: java spring spring-boot spring-data-jpa

我有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”:找不到匹配的编辑器或转换策略

2 个答案:

答案 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
}

此外,如果EcranChampEcranChampId没有其他字段,我建议您使用@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
}