@OneToOne relations hibernate question
I'm working in a project using Hibernate +JPA. I have this Entity class:
@Entity
public class CafeUser implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer userId;
@Column
private String userName;
@Column
private String userPassword;
@Column(unique=true)
private String userEmail;
@Column
private String userAddress;
@Column
private String userCountry;
@Column
private String userState;
@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
private DateTime userDateOfBirth;
@Column
private String userSex;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(n开发者_开发百科ame="userAccountingDetailsId", referencedColumnName="userAccountingDetailsId")
private AccountingDetails accountingDetails;
@OneToOne(cascade=CascadeType.ALL)
private InvestorProfile investorProfile;
@OneToMany(mappedBy="user")
private Set<UserProfileAnswer> userProfileAnswers;
@OneToMany(cascade=CascadeType.ALL, mappedBy="user")
private Set<MoneyHealth> userMoneyHealths;
@OneToMany(cascade=CascadeType.ALL, mappedBy="user")
private Set<LifePlanning> lifePlannings;
getters-setters
}
If you can see, the field userEmail is declared as unique, and if there is a duplicate of CafeUser entity in the DB, it doesn't insert the entity. BUT, the fields "investorProfile" and "accountingDetails" get inserted even if associated with a user that have an email already registered in the database.
What I'm doing wrong??
Thanks in advance!
set hibernate property hibernate.show_sql
to true and see what SQL statements are getting generated. Also check if you are doing the persistence in transaction.
Resolved. I misinterpreted the @OneToOne relation. Now the investorProfile and AccountingDetails holds one key for one user and it's all set.
精彩评论