hibernate单向多对一关联

应用场景:

在电子购物模型中,一个顾客可以发出多个订单,而每个订单却只能属于一个顾客,现在要建立从订单到顾客的多对一的单向关联。

Customer :顾客类

Order:订单类

该多对一关系在javabean中表现为:

    在Order类中有一个customer的顾客属性;在Customer中没有Order的信息,不建立从Customer到Order的关联

该多对一关系在数据库表现为:

    在Order对应的表中有Customer对应的行的id作为foreign key

 

package oneToMany;

import java.io.Serializable;

public class Order  implements Serializable{
 private static final long serialVersionUID = 1L;
 
 private Long id;
 private String orderNumber;
 private Customer customer;
 
 public Order() {}
 
 public Long getId() {
  return id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 public String getOrderNumber() {
  return orderNumber;
 }
 public void setOrderNumber(String orderNumber) {
  this.orderNumber = orderNumber;
 }
 public Customer getCustomer() {
  return customer;
 }
 public void setCustomer(Customer customer) {
  this.customer = customer;
 }
}

 

 

package oneToMany;

import java.io.Serializable;

public class Customer implements Serializable{
 private static final long serialVersionUID = 1L;
 
 private Long id;
 private String name;
 
 public Customer() {}
 
 public Long getId() {
  return id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}

 

以下是oracle的数据库模型:

create sequence customers_sequence;
create table customers(
 id number(20) primary key,
 name varchar2(50)
)
create sequence orders_sequence;
create table orders(
 id number(20) primary key,
 order_number varchar2(100),
 customer_id number(20) references customers(id)
)

 

下面是hibernate的映射文件:

Customer.hbm.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping package="oneToMany">
  <class name="Customer" table="customers">
    <id name="id" column="id">
      <generator class="sequence">
        <param name="sequence">customers_sequence</param>
      </generator>
    </id>
    <property name="name" column="name"/>
   </class>   
</hibernate-mapping>

 

Order.hbm.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping package="oneToMany">
  <class name="Order" table="orders">
    <id name="id" column="id">
      <generator class="sequence">
        <param name="sequence">orders_sequence</param>
      </generator>
    </id>
    <property name="orderNumber" column="order_number"/>
    <many-to-one name="customer"
                 column="customer_id"
                 class="Customer"
                 not-null="true"></many-to-one>
   </class>   
</hibernate-mapping>

 

 

下面是hibernate的session工厂类:

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {

    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

 static {
     try {
   configuration.configure(configFile);
   sessionFactory = configuration.buildSessionFactory();
  } catch (Exception e) {
   System.err.println("Error Occurs When Creating SessionFactory ");
   e.printStackTrace();
  }
    }
    private HibernateSessionFactory() {}
 
 public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
  if (session == null || !session.isOpen()) {
   if (sessionFactory == null) {
    rebuildSessionFactory();
   }
   session = (sessionFactory != null) ? sessionFactory.openSession(): null;
   threadLocal.set(session);
  }
        return session;
    }

 public static void rebuildSessionFactory() {
  try {
   configuration.configure(configFile);
   sessionFactory = configuration.buildSessionFactory();
  } catch (Exception e) {
   System.err.println("Error Occurs When Creating SessionFactory");
   e.printStackTrace();
  }
 }

 public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

        if (session != null) {
            session.close();
        }
    }

 public static org.hibernate.SessionFactory getSessionFactory() {
  return sessionFactory;
 }

 public static void setConfigFile(String configFile) {
  HibernateSessionFactory.configFile = configFile;
  sessionFactory = null;
 }

 public static Configuration getConfiguration() {
  return configuration;
 }

}

 

利用hibernate的session来添加一下order和customer对象:

package oneToMany;

import org.hibernate.Session;
import com.thomas.util.HibernateSessionFactory;

public class Test Save{
 public static void main(String[] args) {
  Session session = HibernateSessionFactory.getSession();
  session.beginTransaction();
  
  Customer customer = new Customer();
  customer.setName("thomas");
  session.save(customer);
  
  Order order1 = new Order();
  order1.setOrderNumber("thomas1");
  order1.setCustomer(customer);
  
  Order order2 = new Order();
  order2.setOrderNumber("thomas2");
  order2.setCustomer(customer);
  
  session.save(order1);
  session.save(order2);
  
  session.getTransaction().commit();
  session.close();
 }
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值