您当前的位置: 首页 >  hibernate

宝哥大数据

暂无认证

  • 0浏览

    0关注

    1029博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Hibernate08_映射关系---OneToMany单向

宝哥大数据 发布时间:2017-08-27 02:57:12 ,浏览量:0

组和用户的关系–oneToMany 1.1、用户User, 多的一方
package com.chb.model;

public class User {
    private int id;
    private String username;
    private String passwd;

    public User() {}

    public User(int id, String username, String passwd) {
        super();
        this.id = id;
        this.username = username;
        this.passwd = passwd;
    }

    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;
    }


}
1.2、User.hbm.xml, 由于OneToMany,是在“一”的一方管理关系, 所以User.hbm.xml只需要关注自己的属性映射关系。




    
    
        
            
            
        
        
        
    
1.3、实体类Group , “一”的一方, oneToMany 在”一”的一方对象中, 设置多的一方作为属性。
package com.chb.model;

import java.util.Set;

public class Group {
    private int id;
    private String groupName;
    //oneToMany 在"一"的一方对象中, 设置多的一方作为属性。
    private Set users;
    public Group(){}
    public Group(int id, String groupName, Set users) {
        super();
        this.id = id;
        this.groupName = groupName;
        this.users = users;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getGroupName() {
        return groupName;
    }
    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }
    public Set getUsers() {
        return users;
    }
    public void setUsers(Set users) {
        this.users = users;
    }
}
1.4、Group的映射文件,




    
    
        
        
            
        

        

        
            
            
            
            
        
    
1.5、配置文件hibernate.cfg.xml




    
        com.mysql.jdbc.Driver
        jdbc:mysql://localhost:3307/hibernate
        root
        root
        org.hibernate.dialect.MySQL5InnoDBDialect

        true
        true
        
        update

        
        
    
1.6、测试添加
package com.chb.testgroup;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import com.chb.model.Group;
import com.chb.model.User;

public class TestOneToMany {
    private SessionFactory sessionFactory;
    private Session session;
    public void getConnect() {
        Configuration config = new Configuration().configure();
        // 根据 configuration 建立 sessionFactory
        sessionFactory = config.buildSessionFactory();
         // 开启 session(相当于开启 JDBC 的 connection)
        session = sessionFactory.openSession();
        //开启事务
        session.beginTransaction();

    }
    public void testOneToMany() {
        getConnect();
        Group group = new Group();
        group.setId(1);
        group.setGroupName("LOL");
        //one to many 先添加“多”的一方
        User u1 = new User();
        u1.setUsername("chb");
        u1.setPasswd("123456");

        User u2 = new User();
        u2.setUsername("tom");
        u2.setPasswd("Net123!#");

        Set users = new HashSet();
        users.add(u1);
        users.add(u2);

        group.setUsers(users);


        for (User user : users) {
            session.save(user);
        }
        //然后添加"一"的一方
        session.save(group);
        close();
    }

    public static void main(String[] args) {
        TestOneToMany test = new TestOneToMany();
        test.testOneToMany();
    }

    /**
     * 关闭Sesson 相当于关闭JDBC的connection
     */
    public void  close() {
        if (session != null) {
            //提交事务
            session.getTransaction().commit();
            session.close();
        }
        if (sessionFactory != null) {
            sessionFactory.close();
        }
    }

}
结果:会提交5条sql, 相对于manyToOne , 效率低一些。 特别注意: oneToMany在添加和维护关系比较麻烦, 所以在开发中, 不建议使用oneToMany的单向 1.7 、测试查询。

这里写图片描述

1.8、测试查询group中user的条数

这里写图片描述

1.8.1、使用lazy=extra, 稍微智能一些,会根据查询取得值的类型不同来判断是调用count还是投影

这里写图片描述

1.8.2、再次查询,注意sql变化

这里写图片描述

关注
打赏
1587549273
查看更多评论
立即登录/注册

微信扫码登录

0.0432s