Hibernate学习篇07——manytoone简单实例

来源:互联网 发布:淘宝首页资源位 编辑:程序博客网 时间:2024/05/01 12:26

项目结构



表结构

drop table orders;drop table Customers;create table Customers(id varchar2(32) primary key,name varchar2(32) not null);create table orders(id varchar2(32) primary key,bianhao varchar2(32) not null,customer_id varchar2(32));--alter table orders add constraint FK_ORDERS_CUSTOMER_ID foreign key (customer_id) references customers(id);insert into Customers (id,name) values ('8a8703de51d2e86e0151d2e870690000','zhengliusu');insert into Customers (id,name) values ('ages2f4s38as2a6e0143d2e848334898','zhengyuxing');insert into Orders (id,bianhao,customer_id) values ('46wgfas4df6wasdgergerh4gsdgrea3f','AAABBBCCC','8a8703de51d2e86e0151d2e870690000');insert into Orders (id,bianhao,customer_id) values ('oiaghsdakdsajdhjgsjgwoiwofsfsghs','AAABBBDDD','8a8703de51d2e86e0151d2e870690000');insert into Orders (id,bianhao,customer_id) values ('aloaigwoghgwvvskfklajfowejfsofjg','lkjfdsafe','ages2f4s38as2a6e0143d2e848334898');insert into Orders (id,bianhao,customer_id) values ('pwogsjavncvnxjwoeiflzlvkdsfjlsdf','oiowshscn','ages2f4s38as2a6e0143d2e848334898');commit;

Customer.bhm.xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.masteringhibernate.a052manytoone.easy"><class name="Customer" table="CUSTOMERS"><id name="id" type="string" length="32"><generator class="uuid" /></id><property name="name" /></class></hibernate-mapping>

Order.hbm.xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.masteringhibernate.a052manytoone.easy"><class name="Order" table="ORDERS"><id name="id" type="string" length="32"><generator class="uuid" /></id><property name="bianhao" /><many-to-one name="customer" column="customer_id" class="Customer" /></class></hibernate-mapping>

Customer.java

package com.masteringhibernate.a052manytoone.easy;public class Customer {private String id;private String name;//getters and setters}

Order.java

package com.masteringhibernate.a052manytoone.easy;public class Order {private String id;private String bianhao;private Customer customer;        //getters and setters}

Test.java

package com.masteringhibernate.a052manytoone.easy;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class Test {private static SessionFactory sessionFactory;public static void main(String[] args) {initHibernate();select();System.out.println("======================================");Session session = sessionFactory.openSession();session.beginTransaction();Customer customer = new Customer();customer.setName("wangerma");Order order1 = new Order();order1.setBianhao("ABCDEF");order1.setCustomer(customer);Order order2 = new Order();order2.setBianhao("123456");order2.setCustomer(customer);session.save(order1);session.save(order2);session.save(customer);session.getTransaction().commit();session.close();System.out.println("======================================");select();}public static void select(){Session session = sessionFactory.openSession();List<Order> orderList = (List<Order>)session.createQuery("from Order").list();for(Order order:orderList){System.out.println("bianhao :\t"+order.getBianhao());System.out.println("customer:\t"+order.getCustomer().getName());System.out.println("======================");}session.close();}public static void initHibernate(){Configuration configuration = new Configuration().configure("com/masteringhibernate/a052manytoone/easy/hibernate.cfg.xml");sessionFactory = configuration.buildSessionFactory();}}

日志

Hibernate: select order0_.id as id1_, order0_.bianhao as bianhao1_, order0_.customer_id as customer3_1_ from ORDERS order0_bianhao :AAABBBCCCHibernate: select customer0_.id as id0_0_, customer0_.name as name0_0_ from CUSTOMERS customer0_ where customer0_.id=?customer:zhengliusu======================bianhao :AAABBBDDDcustomer:zhengliusu======================bianhao :lkjfdsafeHibernate: select customer0_.id as id0_0_, customer0_.name as name0_0_ from CUSTOMERS customer0_ where customer0_.id=?customer:zhengyuxing======================bianhao :oiowshscncustomer:zhengyuxing============================================================Hibernate: insert into ORDERS (bianhao, customer_id, id) values (?, ?, ?)Hibernate: insert into ORDERS (bianhao, customer_id, id) values (?, ?, ?)Hibernate: insert into CUSTOMERS (name, id) values (?, ?)Hibernate: update ORDERS set bianhao=?, customer_id=? where id=?Hibernate: update ORDERS set bianhao=?, customer_id=? where id=?======================================Hibernate: select order0_.id as id1_, order0_.bianhao as bianhao1_, order0_.customer_id as customer3_1_ from ORDERS order0_bianhao :AAABBBCCCHibernate: select customer0_.id as id0_0_, customer0_.name as name0_0_ from CUSTOMERS customer0_ where customer0_.id=?customer:zhengliusu======================bianhao :AAABBBDDDcustomer:zhengliusu======================bianhao :lkjfdsafeHibernate: select customer0_.id as id0_0_, customer0_.name as name0_0_ from CUSTOMERS customer0_ where customer0_.id=?customer:zhengyuxing======================bianhao :oiowshscncustomer:zhengyuxing======================bianhao :ABCDEFHibernate: select customer0_.id as id0_0_, customer0_.name as name0_0_ from CUSTOMERS customer0_ where customer0_.id=?customer:wangerma======================bianhao :123456customer:wangerma======================


0 0
原创粉丝点击