0
我需要在mysql表中一次保存兩個實體。以下是兩個實體 一個是UserEntity,另一個是UserDetailsEntity。我想在UserDetails表中插入UserId列作爲外鍵。但UserId不會插入到UserDetails表中。幫助我在這個Plz。外鍵關係:單向註釋休眠
UserEntity.java
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "UserId")
private int id;
@Column(name = "UserLogin", length = 15, unique = true)
private long phNumber;
@Column(name = "Password", length = 100)
private String password;
@Column(name = "UserType", length = 20)
private String userType;
@Column(name = "PromoCode", length = 20)
private String promoCode;
@Column(name = "FirstName", length = 30)
private String firstName;
@Column(name = "LastName", length = 30)
private String lastName;
@Column(name = "RoleId")
private int roleId;
@Column(name = "isActive")
private int isActive;
@Column(name = "isTestUser")
private int isTestUser;
@Column(name = "FcmToken", length = 100)
private String fcmToken;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "UserId", unique = true,insertable = true, updatable = true)
//@ForeignKey(name = "UserId")
private UserDetailsEntity userDetailsEntity;
// getter和setter
UserDetailsEntity.java
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "UserDetailsId")
private int id;
@Column(name="UserId", unique=true, nullable=false)
@GeneratedValue(generator="gen")
@GenericGenerator(name="gen", strategy="foreign", parameters={@Parameter(name="property", value="uid")})
private int userId;
@Column(name ="AadhaarCardNumber" ,length = 12)
private long aadhaarNumber;
@Column(name="", length = 50)
private String emailId;
@Column(name="Address1" , length = 30)
private String address1;
@Column(name="Address2", length = 30)
private String address2;
@Column(name="CityId", length = 10)
private int cityId;
@Column(name="StateId", length = 10)
private int stateId;
@Column(name="CountryId", length = 10)
private int countryId;
@Column(name="Latitude", length = 60)
private String lat;
@Column(name="Longitude", length = 60)
private String lng;
@Column(name="MobileNumber1", length = 15)
private long mobileNumber1;
@Column(name="MobileNumber2", length = 15)
private long mobileNumber2;
@Column(name="DOB", length = 15)
private Date dob;
@OneToOne
private UserEntity userEntity;
// getter和setter方法在這裏
我自己找到了解決方案。讓我更新。 –