I have 2 tables: user and profile. User can exists without profile, but profile entity must have unique user id.
+-----------------------------------+
| user |
+-----------------------------------+
| id | nickname | password |
+----+-------------+----------------+
| 1 | admin | pass |
+----+-------------+----------------+
+-----------------------------------+
| profile |
+-----------------------------------+
| id | user_id | status |
+----+-------------+----------------+
| 1 | 1 | java forever |
+----+-------------+----------------+
How i can connect them in @Entity class? This code does not work:
@Entity
@Table(name="profiles")
public class Profile {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne
@JoinColumn(name = "user")
@JsonIgnore
private User user;
}
@Entity
@Table(name = "users")
public class User {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "nickname", length = 50, unique = true)
@NotNull
private String nickname;
@OneToOne(mappedBy = "profile")
Profile profile;
}
Optionally i can put 'profile_id' column to user table (and it can be null), but i'm trying to use this type of DB tables. What is wrong?
try add optional=false
@OneToOne(optional=false)
@JoinColumn(name = "user")
@JsonIgnore
private User user
when: optional=false
is specify by NOT NULL
So if you are sure, one profile belongs only to one user your classes could look like this:
@Table(name="profiles")
public class Profile {
(.....)
@Column(name="id", nullable=false, length=10)
@Id
@GeneratedValue(generator="PROFILE_ID_GENERATOR")
@org.hibernate.annotations.GenericGenerator(name="PROFILE_ID_GENERATOR", strategy="native")
private Integer id;
@Column(name="status", nullable=true, length=20)
private String status;
@PrimaryKeyJoinColumn
@OneToOne(optional=false, targetEntity=User.class, fetch=FetchType.LAZY)
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK})
@JoinColumns({ @JoinColumn(name="userid", referencedColumnName="id", nullable=false) })
private User user;
@Column(name="userid", nullable=false, insertable=false, updatable=false)
@Id
@GeneratedValue(generator="PROFILE_USERID_GENERATOR")
@org.hibernate.annotations.GenericGenerator(name="PROFILE_USERID_GENERATOR", strategy="foreign", parameters=@org.hibernate.annotations.Parameter(name="property", value="user"))
private int userId;
(....)
}
public class User {
(.....)
@Column(name="id", nullable=false, length=10)
@Id
@GeneratedValue(generator="USER_ID_GENERATOR")
@org.hibernate.annotations.GenericGenerator(name="USER_ID_GENERATOR", strategy="native")
private int id;
@Column(name="nickname", nullable=true, length=25)
private String nickname;
@Column(name="password", nullable=true, length=25)
private String password;
@OneToOne(mappedBy="user", targetEntity=Profile.class, fetch=FetchType.LAZY)
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK})
private Profile profile;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.