hibernate的one-to-many和many-to-one实例

one-to-many:

Userinfo.java

  1. package airticket.po;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. /**
  5.  * Userinfo generated by MyEclipse - Hibernate Tools
  6.  */
  7. @SuppressWarnings("serial")
  8. public class Userinfo  implements java.io.Serializable {
  9.     // Fields    
  10.      private Long userid;
  11.      private String username;
  12.      private String pasword;
  13.      private Set userscores = new HashSet(0);
  14.     // Constructors
  15.     /** default constructor */
  16.     public Userinfo() {
  17.     }
  18.     
  19.     /** full constructor */
  20.     public Userinfo(Long userid, String username, String pasword, Set userscores){
  21.         this.username = username;
  22.         this.pasword = pasword;
  23.         this.userscores = userscores;
  24.     }
  25.    
  26.     // Property accessors
  27.     public Long getUserid() {
  28.         return this.userid;
  29.     }
  30.     
  31.     public void setUserid(Long userid) {
  32.         this.userid = userid;
  33.     }
  34.     public String getUsername() {
  35.         return this.username;
  36.     }
  37.     
  38.     public void setUsername(String username) {
  39.         this.username = username;
  40.     }
  41.     public String getPasword() {
  42.         return this.pasword;
  43.     }
  44.     
  45.     public void setPasword(String pasword) {
  46.         this.pasword = pasword;
  47.     }
  48.     public Set getUserscores() {
  49.         return userscores;
  50.     }
  51.     public void setUserscores(Set userscores) {
  52.         this.userscores = userscores;
  53.     }
  54. }

对应关系存放在Set中

     private Set userscores = new HashSet(0);

 

Userinfo.hbm.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <!-- 
  5.     Mapping file autogenerated by MyEclipse - Hibernate Tools
  6. -->
  7. <hibernate-mapping>
  8.     <class name="airorder.Userinfo" table="USERINFO" schema="TICKET">
  9.         <id name="userid" type="java.lang.Long">
  10.             <column name="USERID" precision="22" scale="0" />
  11.             <generator class="assigned"></generator>
  12.         </id>
  13.         <property name="username" type="java.lang.String">
  14.             <column name="USERNAME" length="12" not-null="true" />
  15.         </property>
  16.         <property name="pasword" type="java.lang.String">
  17.             <column name="PASWORD" length="32" />
  18.         </property>
  19.         <set name="userscores" inverse="true">
  20.             <key>
  21.                 <column name="USERID" precision="22" scale="0" not-null="true" />
  22.             </key>
  23.             <one-to-many class="airticket.po.Userscore" />
  24.         </set>
  25.     </class>
  26. </hibernate-mapping>

对应关系存放在<set>标签对中

        <set name="userscores" inverse="true">
            <key>
                <column name="USERID" precision="22" scale="0" not-null="true" />
            </key>
            <one-to-many class="airticket.po.Userscore" />
        </set>

 

many-to-one:

Userscore.java

  1. package airticket.po;
  2. import java.util.Date;
  3. /**
  4.  * Userscore generated by MyEclipse - Hibernate Tools
  5.  */
  6. @SuppressWarnings("serial")
  7. public class Userscore  implements java.io.Serializable {
  8.     // Fields    
  9.      private String scoreid;
  10.      private Userinfo userinfo;
  11.      private String username;
  12.      private Long obtainscore;
  13.      private Long costscore;
  14.      private Date updscotime;
  15.      private String orderid;
  16.      private String eventid;
  17.     // Constructors
  18.     /** default constructor */
  19.     public Userscore() {
  20.     }
  21.     /** minimal constructor */
  22.     public Userscore(String scoreid, Userinfo userinfo, String username, Date updscotime) {
  23.         this.scoreid = scoreid;
  24.         this.userinfo = userinfo;
  25.         this.username = username;
  26.         this.updscotime = updscotime;
  27.     }
  28.     
  29.     /** full constructor */
  30.     public Userscore(String scoreid, Userinfo userinfo, String username, Long obtainscore, Long costscore, Date updscotime, String orderid, String eventid) {
  31.         this.scoreid = scoreid;
  32.         this.userinfo = userinfo;
  33.         this.username = username;
  34.         this.obtainscore = obtainscore;
  35.         this.costscore = costscore;
  36.         this.updscotime = updscotime;
  37.         this.orderid = orderid;
  38.         this.eventid = eventid;
  39.     }
  40.    
  41.     // Property accessors
  42.     public String getScoreid() {
  43.         return this.scoreid;
  44.     }
  45.     
  46.     public void setScoreid(String scoreid) {
  47.         this.scoreid = scoreid;
  48.     }
  49.     public Userinfo getUserinfo() {
  50.         return this.userinfo;
  51.     }
  52.     
  53.     public void setUserinfo(Userinfo userinfo) {
  54.         this.userinfo = userinfo;
  55.     }
  56.     public String getUsername() {
  57.         return this.username;
  58.     }
  59.     
  60.     public void setUsername(String username) {
  61.         this.username = username;
  62.     }
  63.     public Long getObtainscore() {
  64.         return this.obtainscore;
  65.     }
  66.     
  67.     public void setObtainscore(Long obtainscore) {
  68.         this.obtainscore = obtainscore;
  69.     }
  70.     public Long getCostscore() {
  71.         return this.costscore;
  72.     }
  73.     
  74.     public void setCostscore(Long costscore) {
  75.         this.costscore = costscore;
  76.     }
  77.     public Date getUpdscotime() {
  78.         return this.updscotime;
  79.     }
  80.     
  81.     public void setUpdscotime(Date updscotime) {
  82.         this.updscotime = updscotime;
  83.     }
  84.     public String getOrderid() {
  85.         return this.orderid;
  86.     }
  87.     
  88.     public void setOrderid(String orderid) {
  89.         this.orderid = orderid;
  90.     }
  91.     public String getEventid() {
  92.         return this.eventid;
  93.     }
  94.     
  95.     public void setEventid(String eventid) {
  96.         this.eventid = eventid;
  97.     }
  98. }

对应关系保存在类中

     private Userinfo userinfo;

Userscore.hbm.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <!-- 
  5.     Mapping file autogenerated by MyEclipse - Hibernate Tools
  6. -->
  7. <hibernate-mapping>
  8.     <class name="airorder.Userscore" table="USERSCORE" schema="TICKET">
  9.         <id name="scoreid" type="java.lang.String">
  10.             <column name="SCOREID" length="32" />
  11.             <generator class="assigned"></generator>
  12.         </id>
  13.         <many-to-one name="userinfo" class="airticket.po.Userinfo" fetch="select">
  14.             <column name="USERID" precision="22" scale="0" not-null="true" />
  15.         </many-to-one>
  16.         <property name="username" type="java.lang.String">
  17.             <column name="USERNAME" length="12" not-null="true" />
  18.         </property>
  19.         <property name="obtainscore" type="java.lang.Long">
  20.             <column name="OBTAINSCORE" precision="22" scale="0" />
  21.         </property>
  22.         <property name="costscore" type="java.lang.Long">
  23.             <column name="COSTSCORE" precision="22" scale="0" />
  24.         </property>
  25.         <property name="updscotime" type="java.util.Date">
  26.             <column name="UPDSCOTIME" length="7" not-null="true" />
  27.         </property>
  28.         <property name="orderid" type="java.lang.String">
  29.             <column name="ORDERID" length="32" />
  30.         </property>
  31.         <property name="eventid" type="java.lang.String">
  32.             <column name="EVENTID" length="32" />
  33.         </property>
  34.     </class>
  35. </hibernate-mapping>

对应关系保存在<many-to-one>标签对中

        <many-to-one name="userinfo" class="airticket.po.Userinfo" fetch="select">
            <column name="USERID" precision="22" scale="0" not-null="true" />
        </many-to-one>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡矣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值