我使用 Struts Spring和Hibernate集成与mysql&进行在线考试项目Eclipse kepler。 在registration.jsp页面中提交值时,我试图将这些值存储在同一数据库中的两个不同的表(user_details,address)中。我可以将它们存储在DB中,但是我无法获取user_id,这是地址表的外键。 user_id是user_details表中的主键。除了地址表中的user_id外,所有其他字段都填充了正确的值。我试图在地址表中使用它。但我无法做到这一点。我已经附上了我现在正在使用的代码,
user.hbm.xml
<hibernate-mapping>
<class name="UserDetails" table="user_details">
<id name="user_id" type="int" column="user_id" >
<generator class="identity">
</generator>
</id>
<property name="first_name" type="string">
<column name="first_name"/>
</property>
<property name="last_name" type="string">
<column name="last_name"/>
</property>
<property name="email" type="string">
<column name="email"/>
</property>
<property name="password" type="string">
<column name="password"/>
</property>
<property name="gender" type="string">
<column name="gender"/>
</property>
<property name="dob" type="int">
<column name="dob"/>
</property>
<property name="phone" type="int">
<column name="phone"/>
</property>
<property name="experience" type="float">
<column name="experience"/>
</property>
<set name="addr" table="address"
inverse="true" lazy="true" fetch="select" cascade = "save-update">
<key>
<column name="user_id" not-null="false" />
</key>
<one-to-many class="UserAddress" />
</set>
</class>
</hibernate-mapping>
useraddress.hbm.xml
<hibernate-mapping>
<class name="UserAddress" table="address">
<id name="address_id" type="int" column="address_id">
<generator class="identity"/>
</id>
<property name="addr_line1" type="string">
<column name="addr_line_1"/>
</property>
<property name="addr_line2" type="string">
<column name="addr_line_2"/>
</property>
<property name="addr_line3" type="string">
<column name="addr_line_3"/>
</property>
<property name="city" type="string">
<column name="city"/>
</property>
<property name="zipcode" type="int">
<column name="zipcode"/>
</property>
<property name="state" type="string">
<column name="state"/>
</property>
<property name="country" type="string">
<column name="country"/>
</property>
<many-to-one name="user_detail" class="UserDetails" fetch="select">
<column name="user_id" not-null="false"></column>
</many-to-one>
</class>
</hibernate-mapping>
UserDetails.java
public class UserDetails {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
//@OneToMany (mappedBy="user_details", cascade = CascadeType.ALL)
@OneToMany (cascade = { CascadeType.PERSIST, CascadeType.MERGE}, mappedBy="user_detail")
public int user_id; //primary key
private String first_name;
private String last_name;
private String email;
private String password;
private String gender;
private int dob;
private int phone;
private float experience;
private Set<UserAddress> addr;//set name
//getters and setters created
UserAddress.java
public class UserAddress {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int address_id; //primary key
@ManyToOne(fetch=FetchType.EAGER, targetEntity=UserDetails.class)
@JoinColumn(name="user_id")
private UserDetails user_detail;
private String addr_line1;
private String addr_line2;
private String addr_line3;
private String city;
private int zipcode;
private String state;
private String country;
//getters and setters created
我认为我在hibernate映射部分缺少一些东西,因为我能够存储除user_id之外的其他地址表值。如果有人有兴趣使用完整的代码