@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?
任何人都可以提供一些洞察我的代码中的错误以及如何解决它的问题吗?谢谢。
答案 0 :(得分:0)
试试这个,
用户模型
@OneToOne
@JoinColumn(name="info_id")
public Info info;
信息模型
@OneToOne
@JoinColumn(name="user_id")
public User user;
您不应该将mappedBy
与JoinColumn
一起使用。查看差异here。