I had two tables. One of the ıs owner and other one is Bill. In the both table ı had email column. So ı want to use email as a ForeignKey. but owner ıd ıs became foreign key automaticly. how can ı do that?
This one is bill side
@Id
private Long id;
private static final Integer LIMIT=200;
private String firstName;
private String lastName;
private String email;
private Integer amount;
private String productName;
@ManyToOne
@JoinColumn(name = "email")
private PurchasingSpecialist owner;
And this one is owner side
@Id
private Long Id;
private String firstName;
private String lastName;
private String email;
@OneToMany(
cascade = CascadeType.ALL
)
@JoinColumn(name = "email",referencedColumnName = "email")
private List<Bill> bills;
but still owner_id column creating automaticly
CodePudding user response:
First You have to do is remove Long id
attribute from PurchasingSpecialist Entity. And add @Id
to email column. Then It will look like this. Then the JPA will create the email column as the primary key for the entity.
@Id
@Column(length = 200)
private String email;
private String firstName;
private String lastName;
@OneToMany(
cascade = CascadeType.ALL
)
@JoinColumn(name = "email",referencedColumnName = "email")
private List<Bill> bills;
The next step is adding the email
primary key to the Bill Entity.
When Using @JoinColumn(name = "email",referencedColumnName = "email")
It will create a "email" column in Bill Entity. Therefore, private String email;
in bill entity should be removed. Your Bill entity will look like this.
@Id
private Long id;
private static final Integer LIMIT=200;
private String firstName;
private String lastName;
private Integer amount;
private String productName;
@ManyToOne
@JoinColumn(name = "email")
private PurchasingSpecialist owner;
The ER will look like this. I think this is the result you expected.