- package com.hibernate.jpa.bean1;
- import java.util.HashSet;
- import java.util.Set;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.OneToMany;
- public class Garage {
- /**
- * many to one 多对一
- */
- private Integer gid;
- private String garagenum;
- private Set<Auto> autos = new HashSet<Auto>();
- @Id @GeneratedValue
- public Integer getGid() {
- return gid;
- }
- public void setGid(Integer gid) {
- this.gid = gid;
- }
- (length=20)
- public String getGaragenum() {
- return garagenum;
- }
- public void setGaragenum(String garagenum) {
- this.garagenum = garagenum;
- }
- @OneToMany(cascade={CascadeType.PERSIST},mappedBy="garage")
- public Set<Auto> getAutos() {
- return autos;
- }
- public void setAutos(Set<Auto> autos) {
- this.autos = autos;
- }
- public void addGarageAuto(Auto auto) {
- auto.setGarage(this);
- this.autos.add(auto);
- }
- }
- package com.hibernate.jpa.bean1;
- import javax.persistence.CascadeType;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- @Entity
- public class Auto {
- /**
- * one to many 一对多关联
- */
- private Integer autoId;
- private String autotype;
- private String autonum;
- private Garage garage;
- @Id @GeneratedValue
- public Integer getAutoId() {
- return autoId;
- }
- public void setAutoId(Integer autoId) {
- this.autoId = autoId;
- }
- public String getAutotype() {
- return autotype;
- }
- public void setAutotype(String autotype) {
- this.autotype = autotype;
- }
- public String getAutonum() {
- return autonum;
- }
- public void setAutonum(String autonum) {
- this.autonum = autonum;
- }
- @ManyToOne()
- @JoinColumn(name="garageid")
- public Garage getGarage() {
- return garage;
- }
- public void setGarage(Garage garage) {
- this.garage = garage;
- }
- }
- @Test public void delete() {
- EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-hibernate");
- EntityManager em = factory.createEntityManager();
- em.getTransaction().begin();
- Garage garage = em.find(Garage.class, 3);
- em.remove(garage);
- em.getTransaction().commit();
- em.close();
- factory.close();
- }
- Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=?
- @OneToMany(cascade={CascadeType.PERSIST,CascadeType.REMOVE},mappedBy="garage")
- public Set<Auto> getAutos() {
- return autos;
- }
- Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=?
- Hibernate: select autos0_.garageid as garageid1_, autos0_.autoId as autoId1_, autos0_.autoId as autoId0_0_, autos0_.autonum as autonum0_0_, autos0_.autotype as autotype0_0_, autos0_.garageid as garageid0_0_ from Auto autos0_ where autos0_.garageid=?
- Hibernate: delete from Auto where autoId=?
- Hibernate: delete from Auto where autoId=?
- Hibernate: delete from Garage where gid=?
- mysql> select * from garage;
- +-----+-----------+
- | gid | garagenum |
- +-----+-----------+
- | 1 | room1 |
- | 2 | room2 |
- +-----+-----------+
- mysql> select * from auto;
- +--------+---------+----------+----------+
- | autoId | autonum | autotype | garageid |
- +--------+---------+----------+----------+
- | 1 | hk2222 | car | 1 |
- | 2 | bj0000 | car | 1 |
- +--------+---------+----------+----------+