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

标签 java mysql sql spring hibernate

我正在使用 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/

相关文章:

SQL select语句类似于Excel中垂直搜索近似匹配

php - 防止在 php/mysql 中重复插入行

Java 和 Android 正则表达式的区别

java - 泛型类和列表的奇怪编译错误

java - Navigation Drawer Android 列表项高度

php - MySQL - 在需要时添加增值税

mysql - Perl:在 Perl-Hash 中存储来自 Mysql 表的列

MySQL Query 选择上周的数据?

java - 在 keystore 文件中查找别名

Mysql 最大查询