美文网首页
4.hibernate映射(多对一)

4.hibernate映射(多对一)

作者: arkulo | 来源:发表于2017-07-23 21:41 被阅读14次

    映射关系

    映射是对象到表,映射关系是多个对象的关系映射到多张表的关系!

    cascade级联

    hibernate的级联是用来说明数据库中两个表的对应关系(一对一,一对多,多对多...,但是多对一一般不需要级联),当对主对象进行操作时,是否对其关联的从对象也进行相应的操作,常见的级联关系有:

    • none:对保存,删除,修改主对象时,从对象不做任何操作
    • save-update:在保存,更新当前对象时,级联保存,更新附属对象
    • delete:在删除当前对象时,级联删除附属对象
    • all:所有情况下均进行级联操作,即包含save-update和delete

    现在我们来说说,多对一这种关系

    项目配置文件

        <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
        
        <hibernate-configuration>
            <session-factory>
        <!--        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
                <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test"</property>
                <property name="hibernate.connection.username">root</property>
                <property name="hibernate.connection.password">root</property>
                <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
                
                <mapping resource="entity/User.hbm.xml" /> -->
                
                <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
                <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test</property>
                <property name="hibernate.connection.username">root</property>
                <property name="hibernate.connection.password">root</property>
                <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
                <property name="hibernate.show_sql">true</property>
                <property name="hibernate.hbm2ddl.auto">create</property>
                
                <mapping resource="entity/User.hbm.xml"/>   
                <mapping resource="entity/Group.hbm.xml" /> 
                
            </session-factory>
        </hibernate-configuration>
    

    实体类:

        package entity;
        
        public class Group {
            private int id;
            private String name;
            
            public int getId() {
                return id;
            }
            public void setId(int id) {
                this.id = id;
            }
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
        }
    

        package entity;
        
        import java.util.Date;
        
        public class User {
            private int id;
            private String userName;
            private String passWd;
            private Date addtime;
            private Group group;
            
            public Date getAddtime() {
                return addtime;
            }
        
            public void setAddtime(Date addtime) {
                this.addtime = addtime;
            }
        
            public User(){}
        
        
        
            public int getId() {
                return id;
            }
        
            public void setId(int id) {
                this.id = id;
            }
        
            public String getUserName() {
                return userName;
            }
        
            public void setUserName(String userName) {
                this.userName = userName;
            }
        
            public String getPassWd() {
                return passWd;
            }
        
            public void setPassWd(String passWd) {
                this.passWd = passWd;
            }
        
            public Group getGroup() {
                return group;
            }
        
            public void setGroup(Group group) {
                this.group = group;
            }
            
            
        }
    

    映射文件:

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
        <class name="entity.Group" table="_group">
            <id name="id">
                <generator class="identity" />
            </id>
            <property name="name" />        
        </class>    
    </hibernate-mapping>
    

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
        <class name="entity.User" >
            <id name="id">
                <!-- 如果是uuid的话,需要id配置为String类型 -->
                <!--  <generator class="uuid"/>-->
                <generator class="identity" />
            </id>
            <property name="userName" />        
            <property name="passWd" />
            <property name="addtime" type="time" />
            <many-to-one name="group" column="group_id" class="entity.Group" />
        </class>    
    </hibernate-mapping>
    

    单元测试:

    /**
     * 初始化数据写入,测试普通级联写入数据,和数据读取
     */
    public void test1(){
        Session session = null;
        
        try {
            session = hibernateUtil.getSession();
            session.beginTransaction();
            
            Group gp1 = new Group();
            gp1.setName("产品组");
            session.save(gp1);
            
            User user1 = new User();
            user1.setUserName("王蕊");
            user1.setPassWd("123456");
            user1.setGroup(gp1);
            user1.setAddtime(new Date());
            
            session.save(user1);
            
            System.out.println("--------------------------------");
            
            User userTmp = (User)session.load(User.class, 1);
            
            System.out.println("用户名:"+userTmp.getUserName());
            System.out.println("组名:"+userTmp.getGroup().getName());
            
            
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }finally{
            hibernateUtil.closeSession(session);
        }       
    
    }

    相关文章

      网友评论

          本文标题:4.hibernate映射(多对一)

          本文链接:https://www.haomeiwen.com/subject/sivvzttx.html