hibernate 一对多映射
package com.entity.onetomany; import java.util.ArrayList; import java.util.List; import javax.persistence.Entity; import javax.persistence.OneToMany; import com.entity.BaseEntity; @Entity public class Team extends BaseEntity{ private String name; @OneToMany(mappedBy="team") //被Member类的team对象维护即Member.team private List<Member> members; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Member> getMembers() { return members; } public void setMembers(List<Member> members) { this.members = members; } }
team.java
package com.entity.onetomany; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.NamedQuery; import com.entity.BaseEntity; @Entity @NamedQuery(name = "aa", query = "select m from Member m where team_id=:tid") public class Member extends BaseEntity { private String name; private int no; @ManyToOne(cascade=CascadeType.PERSIST) //级联保存 保存member的时候自动保存team @JoinColumn(name="team_id") //外键team_id //member为多的一方 所以由member来维护 private Team team; public int getNo() { return no; } public void setNo(int no) { this.no = no; } public Team getTeam() { return team; } public void setTeam(Team team) { this.team = team; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
member.java
package com; import java.util.List; import java.util.Map; import javax.persistence.Entity; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.annotations.NamedQuery; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; import org.hibernate.transform.Transformers; import org.jboss.logging.annotations.Transform.TransformType; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.entity.onetomany.Member; import com.entity.onetomany.Team; public class TestOneToMany { private Session s; private Transaction tran; @Before public void before(){ s=HibernateUtils.getSession(); tran=s.beginTransaction(); } public void one2many(){ Team team=new Team(); Team team1=new Team(); team.setName("lakers"); team1.setName("thunder"); Member member1=new Member(); member1.setName("kobe"); member1.setTeam(team);//建立关系 Member member2=new Member(); member2.setName("nash"); member2.setTeam(team);//建立关系 Member member3=new Member(); member3.setName("boozer"); member3.setTeam(team);//建立关系 Member member4=new Member(); member4.setName("westbrook"); member4.setTeam(team1);//建立关系 Member member5=new Member(); member5.setName("durant"); member5.setTeam(team1);//建立关系 Member member6=new Member(); member6.setName("ibaka"); member6.setTeam(team1);//建立关系 s.persist(member1); s.persist(member2); s.persist(member3); s.persist(member4); s.persist(member5); s.persist(member6); } public void one2manyquery(){ Team team= (Team)s.get(Team.class, 1); //获得team对象 for(Member m:team.getMembers()){ System.out.println(m.getName()); } } @After public void after(){ tran.commit(); HibernateUtils.closeSession(s); } }
testonetomany.java
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。