Hibernate的那些事-manyToOne映射

----多对一的关系映射,在这里我们有一个实例,就是销售订单与销售人员的关系,我们假设一个销售人员可以有多个订单,同时一个订单只能有一个销售人员来产生。以下为这两个类:

订单类:

package manyToOne;

public class CarOrder {
 private int id;
 private String carName;
 private Salesman salesman;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getCarName() {
  return carName;
 }

 public void setCarName(String carName) {
  this.carName = carName;
 }

 public Salesman getSalesman() {
  return salesman;
 }

 public void setSalesman(Salesman salesman) {
  this.salesman = salesman;
 }

}
销售人员类:

package manyToOne;

public class Salesman implements java.io.Serializable {
 /**
  *
  */
 private static final long serialVersionUID = 1L;
 private int id;
 private String salesName;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getSalesName() {
  return salesName;
 }

 public void setSalesName(String salesName) {
  this.salesName = salesName;
 }

 public static long getSerialVersionUID() {
  return serialVersionUID;
 }

}
两个映射配置文件:

CarOrder映射文件


        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


 
 
  
  

 

 
 

 
 
 


 

 

Salesman映射文件:

 


        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


 
 
  
  

 

 
 

 

编写一个测试类:

在这里本人使用的是Junit测试:

package manyToOne;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class JunitTestManyToOne {
 

  private static SessionFactory sessionFactory;
  @BeforeClass
  public static void beforClass(){
   sessionFactory = new Configuration ().configure()
   .buildSessionFactory();
  
  }
  @AfterClass
  public static void afterClass(){
   if(sessionFactory!=null){
    sessionFactory.close();
   }
  
  }
  @Test
  public void createEntity(){
   Session session =sessionFactory.openSession();
   session.beginTransaction();
   Salesman salesman = new Salesman();
   salesman.setSalesName("skewen");

   CarOrder carOrder = new CarOrder();
   carOrder.setCarName("Volovo");
   CarOrder carOrder1 = new CarOrder();
   carOrder1.setCarName("Ford");
   CarOrder carOrder2 = new CarOrder();
   carOrder2.setCarName("BMW");
   carOrder.setSalesman(salesman);
   carOrder1.setSalesman(salesman);
   carOrder2.setSalesman(salesman);
  /***由于在CarOrder的配置文件——CarOrder.hbm.xml*******
   ***文件的 的属性中已经指明了cascade="all"***********
   ****这样当CarOrder发生改变时会进行级联操作,我们可以不用保存salesman******
    ***而在保存CarOrder的对象时会自动保存Salesman的实例******/
   //session.save(salesman);
   session.save(carOrder);
   session.save(carOrder1);
   session.save(carOrder2);

  
   session.getTransaction().commit();
   session.close();
  }
  public static void main(String [] args){
   beforClass();
  }

 }

 


 

你可能感兴趣的:(hibernate,string,generator,class,encoding,session)