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;
}

And in my migration, users table includes a info_id column of bigint type, and infos table includes a user_id column of bigint type.

However, when i run the application, it gives me 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?

Can anyone provide some insight into what's wrong in my code and how to fix it? Thank you.

OneZero
  • 11,556
  • 15
  • 55
  • 92

1 Answers1

0

Try this,

User Model

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

Info Model

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

You shouldn't use mappedBy along with JoinColumn. See the difference here.

Community
  • 1
  • 1
Sivakumar
  • 1,711
  • 14
  • 18