Hibernate 再接触 集合映射

不太重要

List 用于排序

Map  key一般是user的某个字段(多半是主键 integer)

package com.bjsxt.hibernate;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="t_group")
public class Group {
    private int id;
    private String name;
    //private Set<User> users = new HashSet<User>();
    
    /*private List<User> users = new ArrayList<User>();   //List不常用 主要用在排序
    
    @OneToMany(mappedBy="group",
            cascade={CascadeType.ALL}
            )
    @OrderBy("name ASC")                    //按照name的大小顺序排序
    public List<User> getUsers() {
        return users;
    }
    public void setUsers(List<User> users) {
        this.users = users;
    }*/
    
    
    private Map<Integer, User> users = new HashMap<Integer, User>();
    
    @OneToMany(mappedBy="group", cascade=CascadeType.ALL)
    @MapKey(name="id")
    public Map<Integer, User> getUsers() {
        return users;
    }
    public void setUsers(Map<Integer, User> users) {
        this.users = users;
    }
    @Id
    @GeneratedValue
    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;
    }
    
    
    
}

User.java

package com.bjsxt.hibernate;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="t_user")
public class User {
    private int id;
    private String name;
    private Group group;
    @ManyToOne(cascade={CascadeType.ALL})
    public Group getGroup() {
        return group;
    }
    public void setGroup(Group group) {
        this.group = group;
    }
    @Id
    @GeneratedValue
    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;
    }
}
    @Test
    public void testLoadGroup() {
        
        
        Session s = sessionFactory.getCurrentSession();
        s.beginTransaction();
        //User u = (User)s.load(User.class, 1);
        //u.setGroup(null);
        //s.delete(u);
        Group g = (Group)s.load(Group.class, 1);
        for(Map.Entry<Integer, User> entry : g.getUsers().entrySet()) {             
            System.out.println(entry.getValue().getName());
        }
        //s.createQuery("delete from User u where u.id = 1").executeUpdate();
        s.getTransaction().commit();
        
    }
    

 

posted @ 2018-09-06 22:01  橘柑之味  阅读(90)  评论(0编辑  收藏  举报