外键关系:单向注解hibernate
Foreign key Relation : UniDirectional annotation hibernate
我需要在 mysql table 中同时保存两个实体。下面是两个实体
一个是 UserEntity,另一个是 UserDetailsEntity。我想在 UserDetails table 中插入 UserId 列作为外键。但是 UserId 没有被插入到 UserDetails Table 中。请在这方面帮助我。
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;
// getters 和 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 和二传手在这里
我自己找到了解决方案。让我更新。只需在 parents table(这里是 Users)中提及注解 OneToOne。子 table 即 UserDetails 的 ID 将存储在父 table 中。 @OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@JoinColumn(name = "UserDetailsId", nullable = false)
private UserDetailsEntity userDetailsEntity;
我的要求已完成,与 UserDetailsEntity 无关。
我需要在 mysql table 中同时保存两个实体。下面是两个实体 一个是 UserEntity,另一个是 UserDetailsEntity。我想在 UserDetails table 中插入 UserId 列作为外键。但是 UserId 没有被插入到 UserDetails Table 中。请在这方面帮助我。
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;
// getters 和 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 和二传手在这里
我自己找到了解决方案。让我更新。只需在 parents table(这里是 Users)中提及注解 OneToOne。子 table 即 UserDetails 的 ID 将存储在父 table 中。 @OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@JoinColumn(name = "UserDetailsId", nullable = false)
private UserDetailsEntity userDetailsEntity;
我的要求已完成,与 UserDetailsEntity 无关。