Hibernate使用註解實現1VN的關係?
阿新 • • 發佈:2018-11-22
示例:國家、省份、城市的三級連動。
一、資料庫要求:
1、國家表nation:nid國家編號、nname國家名稱
2、省份表province:pid省份編號、pname省份名稱、nid國家的編號
3、城市表city:cid城市編號、cname城市名稱、pid省份編號
二、實體類:
1、國家表Nation.java的實體類:
@Entity//指定為實體類 @Table(name="nation")//物件資料庫的表 public class Nation { @Id @GenericGenerator(name="myid",strategy="guid") @GeneratedValue(generator="myid")//指名主鍵 private String nid;//國家編號 @Column(name="nname") private String nname;//國家名稱 //設定與省份表的1VN關係 @OneToMany(targetEntity=Province.class,cascade=CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumn(name="nid") private Set<Province> sp=new HashSet<>();//set集合使用者存放N個省份物件 public String getNid() { return nid; } public void setNid(String nid) { this.nid = nid; } public String getNname() { return nname; } public void setNname(String nname) { this.nname = nname; } public Set<Province> getSp() { return sp; } public void setSp(Set<Province> sp) { this.sp = sp; } public Nation(String nid, String nname, Set<Province> sp) { super(); this.nid = nid; this.nname = nname; this.sp = sp; } public Nation() { super(); // TODO Auto-generated constructor stub } }
2、省份表Province.java的實體類:
@Entity//指名實體類 @Table(name="province")//對應資料庫表 public class Province { @Id @GenericGenerator(name="myid",strategy="guid") @GeneratedValue(generator="myid")//指名主鍵 private String pid;//省份編號 @Column(name="pname") private String pname;//省份名稱 //設定與國家表的NV1關係 @ManyToOne(targetEntity=Nation.class) @JoinColumn(name="nid",insertable = false) private Nation nation;//國家表的物件 //設定與城市表的1VN關係 @OneToMany(targetEntity=City.class,cascade=CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumn(name="pid") private Set<City> sc=new HashSet<>();//用於儲存城市表的N個物件 public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Nation getNation() { return nation; } public void setNation(Nation nation) { this.nation = nation; } public Set<City> getSc() { return sc; } public void setSc(Set<City> sc) { this.sc = sc; } public Province(String pid, String pname, Nation nation, Set<City> sc) { super(); this.pid = pid; this.pname = pname; this.nation = nation; this.sc = sc; } public Province() { super(); // TODO Auto-generated constructor stub } }
3、城市表City.java的實體類:
@Entity//指名為實體類 @Table(name="city")//對應資料庫的表 public class City { @Id @GenericGenerator(name="myid",strategy="guid") @GeneratedValue(generator="myid")//指名主鍵 private String cid;//城市編號 @Column(name="cname") private String cname;//城市名稱 //設定與省份表的NV1關係 @ManyToOne(targetEntity=Province.class) @JoinColumn(name="pid") private Province province;//省份表的物件 public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public Province getProvince() { return province; } public void setProvince(Province province) { this.province = province; } public City() { super(); // TODO Auto-generated constructor stub } public City(String cid, String cname, Province province) { super(); this.cid = cid; this.cname = cname; this.province = province; } }
三、配置對映檔案:
在src/main/resources路徑下建立連線資料庫的hibernate.cfg.xml檔案,具體如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">sasa</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/chencao?characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 配置對映(與配置式的區別:class屬性值為實體類的許可權定名)-->
<mapping class="com.zking.hibernate05_02.entity.Nation" />
<mapping class="com.zking.hibernate05_02.entity.Province" />
<mapping class="com.zking.hibernate05_02.entity.City" />
</session-factory>
</hibernate-configuration>
註解式和配置的區別:註解式則省略實體類的對映檔案。
四、測試類中實現CRUD:
public class OneToManyAction extends ActionSupport {
/**
* @Fields serialVersionUID : TODO(描述這個變量表示什麼)
*/
private static final long serialVersionUID = -1669427098473278330L;
/**
*
* @Title: add
* @Description: 新增資料的方法
* @return void
*/
@Test
public void OneToManyActionTest() {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
// 定義Nation物件
Nation nation = new Nation();
nation.setNname("中國");
// 定義Province物件
Province p01 = new Province();
p01.setPname("湖南");
Province p02 = new Province();
p02.setPname("湖北");
// 定義City物件
City c01 = new City();
c01.setCname("長沙");
City c02 = new City();
c02.setCname("武漢");
// 互設 c
nation.getSp().add(p01);
nation.getSp().add(p02);
p01.setNation(nation);
p02.setNation(nation);
p01.getSc().add(c01);
p02.getSc().add(c02);
c01.setProvince(p01);
c02.setProvince(p02);
// 儲存
session.save(nation);
transaction.commit();
sessionFactory.close();
session.close();
}
/**
*
* @Title: find
* @Description: 查詢的方法
* @return void
*/
@Test
public void find() {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
// 查詢國家
// 查詢可能會出現懶載入異常(Session 關閉後再查)
Nation n = session.get(Nation.class, "befa7666-b8c2-11e8-a42a-80fa5b5015f0");
System.out.println(n);
transaction.commit();
session.close();
sessionFactory.close();
}
/**
*
* @Title: edit
* @Description: 修改資料的方法
* @return void
*/
// @Test
public void edit() {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
// 得到國家
// Nation n1 = session.get(Nation.class,
// "e6a74fce-b4ae-11e8-8972-80fa5b5015f0");
// n1.setNname("Chain");
// 得到省份
// Province p1=session.get(Province.class,
// "e6a74fce-b4ae-11e8-8972-80fa5b5015f0");
// p1.setPname("Hunan");
// 得到城市
City c1 = session.get(City.class, "e6a77e3c-b4ae-11e8-8972-80fa5b5015f0");
c1.setCname("Yiyang");
transaction.commit();
session.close();
sessionFactory.close();
}
/**
*
* @Title: remove
* @Description: 刪除資料的方法
* @return void
*/
// @Test
public void remove() {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
// 查詢國家
Nation n = session.get(Nation.class, "670dc90a-b4c3-11e8-8972-80fa5b5015f0");
session.delete(n);
transaction.commit();
session.close();
sessionFactory.close();
}
}