Hibernate 多对多的映射关系(many-to-many)

  1. Hibernate 多对多的映射关系(many-to-many)
  2. 我首先使用了学生表和图书表并产生了学生图书表(mysql数据库)
  3. 学生表:(学生占主动 所以在学生的集合中设置inverse=flase 在多对多的关系中inverse=flase是谁占
  4. 主动但在一对多的关系中inverse=true占主动)
  5. CREATE TABLE `stu` (
  6.   `sid` int(11) NOT NULL auto_increment,
  7.   `sname` varchar(50default NULL,
  8.   PRIMARY KEY  (`sid`)
  9. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  10. Stu Bean:
  11.    private Integer sid;
  12.      private String sname;
  13.      private Set book=new HashSet();
  14.     public Set getBook() {
  15.         return book;
  16.     }
  17.     public void setBook(Set book) {
  18.         this.book = book;
  19.     }
  20.     public Stu() {
  21.     }
  22.     public Stu(String sname) {
  23.         this.sname = sname;
  24.     }
  25.     public Integer getSid() {
  26.         return this.sid;
  27.     }
  28.     public void setSid(Integer sid) {
  29.         this.sid = sid;
  30.     }
  31.     public String getSname() {
  32.         return this.sname;
  33.     }
  34.     public void setSname(String sname) {
  35.         this.sname = sname;
  36.     }
  37. Stu.hbm.xml:
  38. <hibernate-mapping>
  39.     <class name="com.vo.Stu" table="stu">
  40.         <id name="sid" type="java.lang.Integer">
  41.             <column name="sid" />
  42.             <generator class="native" />
  43.         </id>
  44.         <property name="sname" type="java.lang.String">
  45.             <column name="sname" length="50" />
  46.         </property>
  47.         <set name="book" table="s_book" cascade="save-update" >
  48.             <key column="sid"/>
  49.             <many-to-many column="bid" class="com.vo.Book"></many-to-many>
  50.         </set>
  51.     </class>
  52. </hibernate-mapping>
  53. 图书表:
  54. CREATE TABLE `book` (
  55.   `bid` int(11) NOT NULL auto_increment,
  56.   `bname` varchar(50default NULL,
  57.   `bprice` varchar(50default NULL,
  58.   PRIMARY KEY  (`bid`)
  59. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  60. BookBean:
  61.  private Integer bid;
  62.      private String bname;
  63.      private String bprice;
  64.      private Set stu=new HashSet();
  65.        public Book() {
  66.     }
  67.     public Book(String bname, String bprice) {
  68.         this.bname = bname;
  69.         this.bprice = bprice;
  70.     }
  71.         public Integer getBid() {
  72.         return this.bid;
  73.     }
  74.     public void setBid(Integer bid) {
  75.         this.bid = bid;
  76.     }
  77.     public String getBname() {
  78.         return this.bname;
  79.     }
  80.     public void setBname(String bname) {
  81.         this.bname = bname;
  82.     }
  83.     public String getBprice() {
  84.         return this.bprice;
  85.     }
  86.     public void setBprice(String bprice) {
  87.         this.bprice = bprice;
  88.     }
  89.     public Set getStu() {
  90.         return stu;
  91.     }
  92.     public void setStu(Set stu) {
  93.         this.stu = stu;
  94.     }
  95. Book.hbm.xml:
  96. <hibernate-mapping>
  97.     <class name="com.vo.Book" table="book">
  98.         <id name="bid" type="java.lang.Integer">
  99.             <column name="bid" />
  100.             <generator class="native" />
  101.         </id>
  102.         <property name="bname" type="java.lang.String">
  103.             <column name="bname" length="50" />
  104.         </property>
  105.         <property name="bprice" type="java.lang.String">
  106.             <column name="bprice" length="50" />
  107.         </property>
  108.          <set name="stu" table="s_book" cascade="save-update" inverse="false">
  109.             <key column="bid"/>
  110.             <many-to-many column="sid" class="com.vo.Stu"></many-to-many>
  111.         </set>
  112.     </class>
  113. </hibernate-mapping>
  114. 学生图书表:
  115. CREATE TABLE `s_book` (
  116.   `sid` int(11default NULL,
  117.   `bid` int(11default NULL
  118. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  119. 注意:
  120. 学生图书表不需要映射关系而且三张表之间也不需要在数据库中建立关系,直接在Hibernate中建立就可
  121. 以.
  122. Test.java:
  123. public class Test {
  124.     SessionFactory sf;
  125.     Session session;
  126.     private Test()
  127.     {
  128.         try
  129.         {
  130.             Configuration cfg = new Configuration();
  131.             sf = cfg.configure().buildSessionFactory();
  132.             session = sf.openSession();
  133.         }
  134.         catch(HibernateException ex)
  135.         {
  136.             ex.printStackTrace();
  137.         }
  138.         
  139.     }
  140.     public void addMethod(Stu st)
  141.        {
  142.            Transaction t=null;
  143.            t=session.beginTransaction();
  144.            session.save(st);
  145.            t.commit();
  146.            
  147.        }
  148.     
  149.     public static void main(String[] args) {
  150.         
  151.         Stu st=new Stu();
  152.         st.setSname("zhang");
  153.         
  154.         Book book=new Book();
  155.         book.setBname("is");
  156.         book.setBprice("100");
  157.         
  158.         Book book2=new Book();
  159.         book2.setBname("is1");
  160.         book2.setBprice("102");
  161.        
  162.         Book book3=new Book();
  163.         book3.setBname("is2");
  164.         book3.setBprice("103");
  165.         
  166.         st.getBook().add(book);
  167.         st.getBook().add(book2);
  168.         st.getBook().add(book3);
  169.         
  170.         new Test().addMethod(st);
  171.     }
  172. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值