Sunday 15 September 2013

java - Hibernate + MySQL + JPA -


1) I have 2 tables, users and user_profile OneToOne with relationship,

User table:

id, user_name, first_name, last_name - is the primary key with id,

user profile:

is the primary key with id, name, user_id - id and user_id is the foreign key with the user table

2) The following are two JPA unit objects

User:

  @Entity @Table (Name = "user") public class user {@Id @ generatedValue (strategy = GenerationType.AUTO) @Column (name = "id") Private Long ID; @column (name = "user_name", exclusive = true, empty = false) the name of the private string; @column (name = "first_name") private string first name; @column (name = "last_name") personal string lastName; @OneToOne (cascade = {CascadeType.ALL}) @JoinColumn (name = "user_id") public UserProfile userProfile; // Setting / Receiving Methods}   

User Profile: @Entity @Table (name = "user_profile") Public class UserProfile {@Id @ column (name = "ID") @ Generated Values ​​(strategy = generation type. INNTITY) Private Long ID; @column (name = "name") private string name; @column (name = "user_id") private long user id; @OneToOne (mapped = "userProfile") private user user; // Set / Received Methods}

When I am trying to insert in both tables, then the value of the user_id column with the USER table I have not seen any value in the user_id column of the USER_PROFILE don.

I'm new to this MySQL, how can some one highlight how to fix it?

"post-text" itemprop = "text">

Your mapping is backwards @Joint column should be on user, not user profile

No comments:

Post a Comment