0

I have two entities with many to many relation

Card

@Entity
@Data
@Table(name = "card")
public class Card {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, name = "name")
    private String name;

    @Column(nullable = false, name = "type")
    private String type;

}

and Deck

@Entity
@Data
@Table(name = "deck")
public class Deck {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    private User owner;

    @Column(nullable = false, name = "name")
    private String name;

    @Column(nullable = false, name = "access_level")
    private String accessLevel;

    @OneToMany(mappedBy = "deck", cascade = CascadeType.ALL)
    List<CardDeckIntersection> cardDeckIntersections;

}

thus i crated join table with composite primary key:

@Data
@Entity
@Table(name = "card_deck_intersection")
@IdClass(CardDeckIntersectionId.class)
public class CardDeckIntersection {

    @Id
    @ManyToOne
    private Card card;

    @Id
    @ManyToOne
    private Deck deck;

}

and composite key class

@Data
public class CardDeckIntersectionId implements Serializable {
    private Card card;
    private Deck deck;
}

Every time i am trying to save an CardDeckIntersection element i get this exception:

org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.Long' to required type 'Card' for property 'card'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.Long' to required type 'Card' for property 'card': no matching editors or conversion strategy found

1 Answers1

0

You can try using many to many relations like this.

Add this to your card class.

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
    @JoinTable(name = "card_deck",
            joinColumns = {@JoinColumn(name = "card_id")},
            inverseJoinColumns = {@JoinColumn(name = "deck_id")})
    private Set<Deck> deck = new HashSet<>();

Add this to your deck class.

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "deck")
    private Set<Card> card = new HashSet<>();
kameshsr
  • 327
  • 3
  • 13