hibernate 一对一实践 - -

原创 2007年09月19日 13:51:00
Content.hbm.xml
<hibernate-mapping>
    <class name="org.hibernate.Content" table="content" catalog="exam">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="foreign">
            <param name="property">info</param>
            </generator>
           
        </id>
        <property name="type" type="java.lang.String">
            <column name="type" length="20" />
        </property>
        <property name="point" type="java.lang.String">
            <column name="point" length="50" />
        </property>
        <property name="score" type="java.lang.Integer">
            <column name="score" />
        </property>
        <property name="modulus" type="java.lang.Float">
            <column name="modulus" precision="12" scale="0" />
        </property>
        <property name="text" type="java.lang.String">
            <column name="text" length="1500" />
        </property>
        <property name="answer" type="java.lang.String">
            <column name="answer" length="1500" />
        </property>
        <property name="pretime" type="java.util.Date">
            <column name="pretime" length="0" />
        </property>
        <one-to-one name="info" class="org.hibernate.Info" constrained="true"></one-to-one>
    </class>
</hibernate-mapping>
Info.hbm.xml
<hibernate-mapping>
    <class name="org.hibernate.Info" table="info" catalog="exam">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="major" type="java.lang.String">
            <column name="major" length="50" />
        </property>
        <property name="subject" type="java.lang.String">
            <column name="subject" length="50" />
        </property>
        <property name="unit" type="java.lang.String">
            <column name="unit" length="50" />
        </property>
       <one-to-one
       name="content"
       class="org.hibernate.Content"
       cascade="all"
       ></one-to-one>
    </class>
</hibernate-mapping>
Info.java
     private Integer id;
     private String major;
     private String subject;
     private String unit;
     private Content content;
+getter+setter
Content.java
private Integer id;
     private String type;
     private Integer score;
     private String point;
     private Float modulus;
     private String text;
     private String answer;
     private Date pretime;
     private Info info;
+getter+setter
添加的Action中
           InfoDAO infodao = new InfoDAO();
            Content content = new Content();
            Info info = new Info();
       其他对象赋值
           content.setInfo(info);
            info.setContent(content);
            infodao.save(info);
之后两个表都添加成功
          infodao.save(info);
删除,更新类似,注意Info.hbm.xml中 cascade="all" 
版权声明:欢迎加入开源技术 Q 群:242561391,让学习和分享成为一种习惯! 举报

相关文章推荐

返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)