【发布时间】:2021-07-08 19:04:01
【问题描述】:
我有两个具有多对多关系的实体
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;
}
和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;
}
因此我用复合主键创建了连接表:
@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;
}
每次我尝试保存 CardDeckIntersection 元素时,我都会收到此异常:
org.springframework.beans.ConversionNotSupportedException:无法将类型“java.lang.Long”的属性值转换为属性“card”所需的类型“Card”;嵌套异常是 java.lang.IllegalStateException:无法将类型“java.lang.Long”的值转换为属性“card”所需的类型“Card”:找不到匹配的编辑器或转换策略
【问题讨论】:
-
这里只是一个简短的提示。尽管 JPA 不允许您尝试做的事情,但如果您使用它本机,hibernate 会允许这样做
-
部分可以,谢谢