找不到OneToOne的列[id]?

时间:2015-01-28 03:22:09

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

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

任何人都可以提供一些洞察我的代码中的错误以及如何解决它的问题吗?谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个,

用户模型

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

信息模型

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

您不应该将mappedByJoinColumn一起使用。查看差异here