Hibernate 注解(Annotations 四)多对多双向注解
注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。
接下来讲解多对多双向注解:
我以用户和房子作为示例讲解。
第一步:准备实体类
House(房间表)
package cn.manytomany.entity; import javax.persistence.*; import java.util.HashSet; import java.util.Set; /** * Created by accp on 2017/2/10. */ @Entity @Table(name = "House") public class House { @Id @GeneratedValue private Integer id; @Column private String title; @Column private String description; @Column private Integer price; @Column private String pubdate; @Column private Integer floorage; @Column private String contact; @Column private Integer userid; @Column private Integer streetid; @Column private Integer typeid; /** * 将控制权交给主表 */ @ManyToMany(mappedBy = "houses") private Set<Users> userss=new HashSet<Users>(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } public String getPubdate() { return pubdate; } public void setPubdate(String pubdate) { this.pubdate = pubdate; } public Integer getFloorage() { return floorage; } public void setFloorage(Integer floorage) { this.floorage = floorage; } public String getContact() { return contact; } public void setContact(String contact) { this.contact = contact; } public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } public Integer getStreetid() { return streetid; } public void setStreetid(Integer streetid) { this.streetid = streetid; } public Integer getTypeid() { return typeid; } public void setTypeid(Integer typeid) { this.typeid = typeid; } public Set<Users> getUserss() { return userss; } public void setUserss(Set<Users> userss) { this.userss = userss; } }
User(用户表):
package cn.manytomany.entity; import javax.persistence.*; import java.util.HashSet; import java.util.Set; /** * Created by accp on 2017/2/10. */ @Entity @Table(name = "Users") public class Users { @Id @GeneratedValue private Integer id; @Column private String name; @Column private String password; @Column private String telephone; @Column private String username; @Column private String isadmin; /** * 以房间表作为主导 设置级联 * 只需要保存用户 即可保存房间 */ @ManyToMany(cascade = {CascadeType.ALL}) @JoinTable( name = "RUHS", joinColumns =@JoinColumn(name = "RUID"), inverseJoinColumns = @JoinColumn(name = "RHID") ) private Set<House> houses=new HashSet<House>(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getIsadmin() { return isadmin; } public void setIsadmin(String isadmin) { this.isadmin = isadmin; } public Set<House> getHouses() { return houses; } public void setHouses(Set<House> houses) { this.houses = houses; } }
第二步:在hibernate.cfg.xml文件中配置<mapping>节点
<mapping class="cn.manytomany.entity.House"/> <mapping class="cn.manytomany.entity.Users"/>
第三步:书写测试类
package cn.manytomany.test; import cn.manytomany.entity.House; import cn.manytomany.entity.Users; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * Created by accp on 2017/2/10. */ public class DemoTest { Configuration cfg; Session session; Transaction tx; @Before public void myBefore(){ cfg=new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactory(); session = factory.openSession(); tx=session.beginTransaction(); } @After public void myAfter(){ tx.commit(); session.close(); } @Test public void add(){ Users users1=new Users(); users1.setName("hiber1"); users1.setPassword("635241"); users1.setTelephone("0102541634"); users1.setUsername("Jim"); users1.setIsadmin("0"); Users users2=new Users(); users2.setName("hiber2"); users2.setPassword("111111"); users2.setTelephone("0102571456"); users2.setUsername("Jim2"); users2.setIsadmin("0"); House house=new House(); house.setTitle("元宵佳节房屋便宜"); house.setDescription("经济实惠,元宵佳宿!"); house.setPrice(155); house.setPubdate("2017-02-11"); house.setFloorage(65); house.setContact("圆圆"); house.setStreetid(5); house.setTypeid(22); House house2=new House(); house2.setTitle("共度元宵"); house2.setDescription("便宜喽,赶快下手!"); house2.setPrice(187); house2.setPubdate("2017-01-10"); house2.setFloorage(73); house2.setContact("佳佳"); house2.setStreetid(3); house2.setTypeid(3); /*关联关系 * 同一个房间对应不同的用户 * * 同一个用户对应不同的房间 * */ users1.getHouses().add(house); users1.getHouses().add(house2); users2.getHouses().add(house2); session.save(users1); session.save(users2); } }
在配置多对多时要选取那张表生成第三张表非常重要,在第三张表的外键也要搞清楚。