当前位置: 首页 > 知识库问答 >
问题:

未能将类型“java.lang.long”的属性值转换为所需类型

徐博雅
2023-03-14
@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;

}

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

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

    @Id
    @ManyToOne
    private Card card;

    @Id
    @ManyToOne
    private Deck deck;

}

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

org.springframework.beans.ConversionNotSupportedException:未能将类型“java.lang.long”的属性值转换为属性“card”所需的类型“card”;嵌套异常是java.lang.IllegalStateException:无法将类型“java.lang.Long”的值转换为属性“Card”所需的类型“Card”:找不到匹配的编辑器或转换策略

共有1个答案

太叔豪
2023-03-14

您可以尝试使用像这样的多对多关系式。

将此添加到您的卡片类中。

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

把这个添加到你的甲板类中。

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "deck")
    private Set<Card> card = new HashSet<>();
 类似资料: