Reputation: 21
I have 2 JPAs. UserJPA and PostJPA. UserJPA has all the user details, like name, address, id. Post JPA has a user_id column. It's a foreign key to User table's id column
when i save the post, i don't want to update the user table, but when i fetch the post table, i want to get the user information
@Table(name = "post_tbl")
public class PostJPA {
@Column(name = "user_id")
private Integer userId;
@Column(name = "content")
@NotNull
private String content;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", referencedColumnName = "id", insertable = false, updatable = false)
UserJPA user = new UserJPA();
}
@Entity(name = "user")
@Table(name = "user_tbl")
public class UserJPA {
@Column(name = "user_name")
private String userName;
@Column(name = "first_name")
@NotNull
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
@NotNull
private String email;
} ```
Why is a save done on PostJPA trying to insert an entry on the User?
Upvotes: 0
Views: 36
Reputation: 32517
Because you are assigning a new, not persisted user to it with
UserJPA user = new UserJPA();
You should assign existing user to the post before saving it
Upvotes: 1