【问题标题】:Cannot find column [id] for OneToOne?找不到 OneToOne 的列 [id]?
【发布时间】:2015-01-28 03:22:08
【问题描述】:
@Entity
@Table(name = "users")
public class User extends Model
{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id")
  public Long id;

  @OneToOne(mappedBy= "user", cascade = {CascadeType.ALL})
  @JoinColumn(name="info_id",referencedColumnName = "id")
  public Info info;
}

@Entity
@Table(name="infos")
public class Info extends Model
{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id")
  public Long id;

  @OneToOne(mappedBy="info",cascade = {CascadeType.ALL})
  @JoinColumn(name="user_id", referencedColumnName = "id")
  public User user;
}

在我的迁移中,users 表包含一个 bigint 类型的 info_id 列,infos 表包含一个 bigint 类型的 user_id 列。

但是,当我运行应用程序时,它给了我PersistenceException: Error with the Join on [models.Info.user]. Could not find the matching foreign key for [id] in table[users]? Perhaps using a @JoinColumn with the name/referencedColumnName attributes swapped?

谁能提供一些关于我的代码有什么问题以及如何修复它的见解?谢谢。

【问题讨论】:

  • 你可以为这两个表发布你的架构吗?
  • 删除两个实体中的 mappedBy
  • 否不要删除两个实体中的 mappedBy;从一侧取下它。它用于将关系标记为双向,并且只放在非所有者方面。

标签: jpa model-view-controller playframework playframework-2.0


【解决方案1】:

试试这个,

用户模型

  @OneToOne
  @JoinColumn(name="info_id")
  public Info info;

信息模型

  @OneToOne
  @JoinColumn(name="user_id")
  public User user;

您不应将mappedByJoinColumn 一起使用。看看区别here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 2015-07-10
    • 2013-05-24
    • 2019-04-01
    • 2023-01-11
    • 2016-03-13
    • 2018-02-25
    相关资源
    最近更新 更多