3
votes

I have EcranChamp entity

@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;

...

And EcranChampId

@Embeddable
public class EcranChampId implements Serializable  {    
private Champ champ;  
private Ecran ecran;
...

Every time i am trying to save an EcranChamp element i have this following error

2018-09-25 12:15:42.889 WARN 14216 --- [nio-8092-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to convert request element: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.Long' to required type 'com.kepler.portailclient.domain.model.Ecran' for property 'ecran'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.Long' to required type 'com.kepler.portailclient.domain.model.Ecran' for property 'ecran': no matching editors or conversion strategy found 2018-09-25 12:15:42.889 WARN 14216 --- [nio-8092-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.Long' to required type 'com.kepler.portailclient.domain.model.Ecran' for property 'ecran'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.Long' to required type 'com.kepler.portailclient.domain.model.Ecran' for property 'ecran': no matching editors or conversion strategy found

2
You specified that the id class for your entity is EcranChampId using the @IdClass annotation. So you must make the @Id field of type EcramChampId. Adding two fields with @Id with types Ecran and Champ does not work. - Jesper
please add a solution to this question and i will try it - Yagami Light

2 Answers

2
votes

Try something like this:

@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 
}    
0
votes

You shoud use @EmbeddedId annotation.

Please, change your EcranChampId class to this:

@Embeddable
public class EcranChampId implements Serializable  {  

    @ManyToOne
    private Champ champ;

    @ManyToOne
    private Ecran ecran;

    //getters and setters
}

And change your EcranChamp class to this:

@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);
    }
}

And use it like this:

public class Champ {

    @OneToMany(mappedBy = "pk.champ")
    private Collection<EcranChamp> ecranChamps;

    //getters and setters
}

Also, if EcranChamp or EcranChampId has no other fields i will recommend you to use @ManyToMany annotation instead EcranChamp class like this:

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
}