我正在通过使用Struts Spring 和 Hibernate与 mysql 和 Eclipse 开普勒的集成来进行在线考试项目。在registration.jsp 页面中提交值时,我试图将这些值存储在同一个数据库中的两个不同的表(user_details、address)中。我可以将它们存储在数据库中,但我无法获取作为地址表外键的 user_id。user_id 是 user_details 表中的主键。除了 address 表中的 user_id 外,其他字段都填写了正确的值。我正在尝试在地址表中使用它。但我不能那样做。我附上了我现在正在使用的代码,
用户.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>
用户地址.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>
用户详细信息.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
用户地址.java
公共类 UserAddress 扩展 UserDetails {
@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
我想我在休眠映射部分遗漏了一些东西,因为我可以存储除 user_id 之外的其他地址表值。如果有人有兴趣使用完整的代码,我准备好了。谢谢。