欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Hibernate(三)--关联映射

程序员文章站 2022-10-04 11:50:15
1.多对一 product category category.hbm.xml

1.多对一

product----category

 

Hibernate(三)--关联映射

 

 Hibernate(三)--关联映射

 

 Hibernate(三)--关联映射

 

 

category.hbm.xml

<?xml version="1.0"?>
<!doctype hibernate-mapping public
        "-//hibernate/hibernate mapping dtd 3.0//en"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.h.pojo">
    <class name="category" table="category_">
        <id name="id" column="id">
            <generator class="native">
            </generator>
        </id>
        <property name="name" />
    </class>

</hibernate-mapping>
product.hbm.xml

<?xml version='1.0' encoding='utf-8'?>
        <!doctype hibernate-mapping public
        "-//hibernate/hibernate mapping dtd 3.0//en"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.h.pojo">
    <class name="product" table="product_">
        <id name="id" column="id">
            <generator class="native"></generator>
        </id>
        <property name="name" />
        <property name="price" />

<many-to-one name="category" class="category" column="cid"></many-to-one> product多 对 一category
</class> </hibernate-mapping>
hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!doctype hibernate-configuration public
        "-//hibernate/hibernate configuration dtd//en"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test?characterencoding=utf-8</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <!-- sql dialect -->
        <property name="dialect">org.hibernate.dialect.mysqldialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>


        <mapping resource="com/h/pojo/product.hbm.xml" />
        <mapping resource="com/h/pojo/category.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

测试类:

public class test1 {
    public static void main(string[] args) {

        sessionfactory factory=new configuration().configure().buildsessionfactory();
        session session = factory.opensession();
        session.begintransaction();


        category c =new category();
        c.setname("c1");
        session.save(c);

        product p = (product) session.get(product.class, 2);
        p.setcategory(c);
        session.update(p);

        session.gettransaction().commit();
        session.close();
        factory.close();
    }
}

 

 

2.一对多

category 对 product,与多对一相反,就是一对多

Hibernate(三)--关联映射

 

 Hibernate(三)--关联映射

 

 

测试类:

Hibernate(三)--关联映射

 

 

 

3.多对多

product  ----  user

一种product可以被多个user购买
一个user可以购买多种product
所以product和user之间的关系是多对多 many-to-many
要实现多对多关系,必须有一张中间表 user_product 用于维护 user和product之间的关系

Hibernate(三)--关联映射

 

 

Hibernate(三)--关联映射

 

 

Hibernate(三)--关联映射

 

 

Hibernate(三)--关联映射

 

 

测试类:

Hibernate(三)--关联映射

 

 

 

数据库:

Hibernate(三)--关联映射