猿问

使用@JoinTable 的休眠一对多映射问题

我有两个表:用户和团队

用户

  • 用户身份

  • 4位密码/密码

团队

  • Team_id

  • 队名

约束 1 个用户只能属于一个团队,而 1 个团队可以有多个用户。我已经对数据库进行了规范化,名为 user_team 的第三个 table()Join Table) 将如下所示:

User_Team[加入表]

  • User_id(这是来自用户表的外键)

  • Team_Id(这是来自团队表的外键)

在使用 @JoinColumn 属性和 @OneToMany 注释在 Hibernate 中实现这一点时,它会抛出以下错误:

来自 com.project.hibernate.User 的外键引用 com.project.hibernate.Team 的列数错误。应该是 2

线程“主”org.hibernate.AnnotationException 中的异常:从 com.project.hibernate.User 引用 com.project.hibernate.Team 的外键具有错误的列数。在 org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1610) 在 org.hibernate.cfg.annotations 的 org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:657) 应该是 2。 CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1335) at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:800) at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:725) org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54) 在 org.hibernate.boot.internal.InFlightMetadataCollectorImpl。


杨__羊羊
浏览 159回答 1
1回答

慕斯709654

您有一个包含两个字段的组合键:@Id@Column(name="team_id")private String team_id;@Id@Column(name="team_name")private String team_name;而您的连接表每个表只有一个键:@OneToMany(cascade=CascadeType.ALL)@JoinTable(name="user_team",&nbsp; &nbsp; &nbsp; &nbsp; joinColumns=@JoinColumn(name="team_id"),&nbsp; &nbsp; &nbsp; &nbsp; inverseJoinColumns=@JoinColumn(name="user_id"))private List<User> user;您需要使用所有建立关系的键,例如(当然表也需要有正确的列):@OneToMany(cascade=CascadeType.ALL)@JoinTable(name="user_team",&nbsp; &nbsp; &nbsp; &nbsp; joinColumns=@JoinColumn({@JoinColumn(name="team_id"), @JoinColumn(name="team_name")}),&nbsp; &nbsp; &nbsp; &nbsp; inverseJoinColumns=@JoinColumn(name="user_id"))private List<User> user;
随时随地看视频慕课网APP

相关分类

Java
我要回答