Click here to Skip to main content
15,944,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Basically, I am trying to establish a relationship between my two tables using spring boots. And the relationship which I had used was the @onetoone and @onetomany relationship. But after building the relationship and creating the table in MySQL whenever I run the programme my foreign key is not updating. The relationship is one user can have many contacts. I have tried unidirectional as well as bidirectional mapping but it is not working.
I want in contact table there will be a separate column for the foreign key. Based on that key I will show all contacts for that particular user.

What I have tried:

This is my contact entity...
@Table(name = "Contact")

public class ContactEntities {

	@GeneratedValue(strategy = GenerationType.AUTO)
	private int c_id;
	private String c_name;
	private String second_c_name;
	private String c_work;
	private String c_emali;
	private String c_phone;
	private String c_image;

	@Column(length = 5000)
	private String c_description;

	@ManyToOne(fetch = FetchType.LAZY)
	 @JoinColumn(name = "contact_id")
	private UserEntities userEntities;

this is my user entity...
@Table(name = "UserEntities")

public class UserEntities {

	@GeneratedValue(strategy = GenerationType.AUTO)
	private int userId;

	@Size(min = 2, max = 20)
	private String userName;

	@Column(unique = true)
	@Email(regexp = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$")
	private String userEmail;

	@NotNull(message = "password should not be blank")
	private String userPass;

	private boolean enable;
	private String role;

	@Column(length = 500)
	private String userAbout;

	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "userEntities")
	// @JoinColumn(name = "user_id")
	private List<ContactEntities> contactList = new ArrayList<>();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900