package cn.edu.hpu.listMapping; import java.util.ArrayList; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="m_group") public class Group { private int id; private String name; private List<User> users=new ArrayList<User>(); @OneToMany(mappedBy="group", cascade={CascadeType.ALL}, fetch=FetchType.LAZY ) public List<User> getUsers() { return users; } public void setUsers(List<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; } }
package cn.edu.hpu.listMapping; 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="m_user") public class User { private int id; private String name; private Group group; //只要有双向就要指定制定一个属性(mapedby) //不指定的话会有两个相同的字段产生 @ManyToOne(cascade={CascadeType.ALL},fetch=FetchType.EAGER) 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; } }
<mapping class="cn.edu.hpu.listMapping.Group"/> <mapping class="cn.edu.hpu.listMapping.User"/>
@OrderBy public List<User> getUsers() { return users; } //@OrderBy默认按主键id排序 //@OrderBy("X DESC")/@OrderBy("X ASC")指按照X属性的ASC(升序)或DESC(降序)排序
package cn.edu.hpu.listMapping; import java.util.HashMap; import java.util.Map; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.MapKey; import javax.persistence.OneToMany; import javax.persistence.OrderBy; import javax.persistence.Table; @Entity @Table(name="m_group") public class Group { private int id; private String name; private Map<Integer,User> users=new HashMap<Integer,User>(); @OneToMany(mappedBy="group") @MapKey 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; } }其中@MapKey(name="id")指定map的Key值是属性的哪一个值。
@Test public void testMapGroup(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session s=sessionFactory.openSession(); s.beginTransaction(); Group g=(Group)s.load(Group.class,1); for(Map.Entry<Integer, User> entry:g.getUsers().entrySet()){ System.out.println(entry.getValue().getName()); } s.getTransaction().commit(); }
u3
尊重开源精神,尊重劳动成果,转载请注明出处: http://blog.csdn.net/acmman/article/details/43907125