gpt4 book ai didi

java - 无法在 hibernate 映射中加载数据

转载 作者:行者123 更新时间:2023-11-30 23:13:36 24 4
gpt4 key购买 nike

我正在使用 Struts Spring 和 Hibernate 集成 与 mysql 和 Eclipse kepler 进行在线考试项目。在 registration.jsp 页面中提交值时,我试图将该值存储在同一数据库中的两个不同表(user_details、地址)中。我可以将它们存储在数据库中,但我无法获取作为地址表外键的 user_id。 user_id是user_details表中的主键。除address表中的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>

用户地址.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

用户地址.java

公共(public)类 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

我想我在 hibernate 映射部分遗漏了一些东西,因为我可以存储除 user_id 之外的其他地址表值。如果有人有兴趣使用完整代码,我随时准备提供。谢谢。

最佳答案

堆栈跟踪中的错误是什么?您还应该使用注释来映射列和属性。你有没有把映射资源放在hibernate配置文件中?

关于java - 无法在 hibernate 映射中加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18874839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com