`

hibernate 映射的配置及基本的hql语句

 
阅读更多

1、 属性的映射文件
<!—name是实体类的全路径 ,table重命名
<class name="com.creasoft.entity.Products" table="product">
		<id name="pid" column="p_id" type="long">
			<generator class="increment"></generator>
		</id>
<!—unique 定义的是否重复
		<property name="productName" 
					column="product_name" 
					type="string" 
					not-null="true"
					unique="true"
					length="20">
		</property>
		<property name="factory" type="string"></property>
		<property name="price" type="double"></property>
		
	</class>

2、配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory>
	<property name="connection.url">
		jdbc:mysql://localhost:3306/hibernate
	</property>
	<property name="connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="connection.username">root</property>
	<property name="connection.password">123456</property>
<!-方言-
	<property name="dialect">
		org.hibernate.dialect.MySQLDialect
	</property>
<!-在控制台显示sql语句-
	<property name="show_sql">true</property>
<!-sql语句格式化-
	<property name="format_sql">true</property>
<!-建表的方式-
<prrperty name=” hbm2ddl.auto”>update</property>
<!-连接映射文件-
	<mapping resource="com/creasoft/entity2/Customer.hbm.xml" />
	<mapping resource="com/creasoft/entity2/VipCard.hbm.xml" />
</session-factory>
</hibernate-configuration>

3、hibernate的工具类
package com.creasoft.util;

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

public class Hibernateutil {
	
	private static SessionFactory sessionFactory ;

	//私有的构造方法,防止用户直接new
	private Hibernateutil(){
		
	}
	//获取sessionfactory
	public static SessionFactory getsessionFactory(){
		
		if(sessionFactory == null || sessionFactory.isClosed()){
			
			sessionFactory = new Configuration().configure().buildSessionFactory();
			
		}
		return sessionFactory;
	}
	//打开一个session
	public static Session getSession(){
		return getsessionFactory().openSession();
	}
	//关闭session
	public static void close(){
		getSession().close();
	}
	
}

4、创建表
package com.creasoft.util;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class CreateTable {
	public static void main(String[] args) {
		//读取配置文件
		Configuration cfg = new Configuration().configure();
		System.out.println("a");
		SchemaExport schemaExport = new SchemaExport(cfg);
		System.out.println("b");
		schemaExport.create(true, true);
		System.out.println("c");
	}
}


5、一对多单向关联
实例:顾客—订单
1)实体类
a、一
package com.creasoft.entity;

import java.util.HashSet;
import java.util.Set;

public class Customer {
	


	private long cid;
	private String customerName;
	private String gender;
//构造方法
	public Customer(String customerName, String gender) {
		super();
		this.customerName = customerName;
		this.gender = gender;
	}
	public Customer(long cid, String customerName, String gender,
			Set<CustomerOrder> sustomerOrders) {
		super();
		this.cid = cid;
		this.customerName = customerName;
		this.gender = gender;
		this.sustomerOrders = sustomerOrders;
	}
	
	public Customer() {
		super();
	}

//标准的set、get方法
	public long getCid() {
		return cid;
	}
	public void setCid(long cid) {
		this.cid = cid;
	}
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
}

b、多实体类:把一的所有的属性都要包含进来
package com.creasoft.entity;

public class CustomerOrder {
	private long uid;
	private String product;
	private Customer customer;
	
	
	public CustomerOrder(String product, Customer customer) {
		super();
		this.product = product;
		this.customer = customer;
	}
	
	
	public CustomerOrder() {
		super();
	}


	public long getUid() {
		return uid;
	}
	public void setUid(long uid) {
		this.uid = uid;
	}
	public String getProduct() {
		return product;
	}
	public void setProduct(String product) {
		this.product = product;
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
}

2)映射文件
A、一的映射文件:一般的映射文件一样
<?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>
 <class name="com.creasoft.entity.Customer" table="customer">
  <id name="cid" type="long">
  	<generator class="identity"></generator>
  </id>
  <property name="customerName"  type="string" not-null="true"/>
  <property name="gender" type="string" length="2"></property>
 </class>
</hibernate-mapping>

B、多:自己的基本属性和一般的映射文件一样,复杂属性放到<many-to-one>的标签,其他的都一样
<?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>
 <class name="com.creasoft.entity.CustomerOrder" table="customer_order">
  <id name="uid" type="long">
  	<generator class="identity"></generator>
  </id>
  <property name="product"  type="string" not-null="true"/>
  <many-to-one name="customer" 
  				class="com.creasoft.entity.Customer"
  				not-null="true"
  				column="customer_id"
  				cascade="delete"
  				></many-to-one>
 </class>
</hibernate-mapping>

1.1 3)插入一条数据:先要保存一的那一边的实体类
Session session = Hibernateutil.getSession();
		
		//开始事务
		Transaction tsaction = session.beginTransaction();
		
		Customer cs = new Customer("chen","m");
		CustomerOrder so = new CustomerOrder("手机",cs);
		session.save(cs);
		session.save(so);
		//提交事务
		tsaction.commit();
		//关闭session
		session.close();



6、一对多的双向关联
1)实体类
A、一:与单向关联相比多了一个集合
package com.creasoft.entity;

import java.util.HashSet;
import java.util.Set;

public class Customer {
	


	private long cid;
	private String customerName;
	private String gender;
	private Set<CustomerOrder> sustomerOrders = new HashSet<CustomerOrder>();
	
	public Customer(String customerName, String gender) {
		super();
		this.customerName = customerName;
		this.gender = gender;
	}
	public Customer(long cid, String customerName, String gender,
			Set<CustomerOrder> sustomerOrders) {
		super();
		this.cid = cid;
		this.customerName = customerName;
		this.gender = gender;
		this.sustomerOrders = sustomerOrders;
	}
	
	public Customer() {
		super();
	}


	public long getCid() {
		return cid;
	}
	public void setCid(long cid) {
		this.cid = cid;
	}
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}


	public Set<CustomerOrder> getSustomerOrders() {
		return sustomerOrders;
	}


	public void setSustomerOrders(Set<CustomerOrder> sustomerOrders) {
		this.sustomerOrders = sustomerOrders;
	}
	
	
}

B、多的实体类不变
package com.creasoft.entity;

public class CustomerOrder {
	private long uid;
	private String product;
	private Customer customer;
	
	
	public CustomerOrder(String product, Customer customer) {
		super();
		this.product = product;
		this.customer = customer;
	}
	
	
	public CustomerOrder() {
		super();
	}


	public long getUid() {
		return uid;
	}
	public void setUid(long uid) {
		this.uid = uid;
	}
	public String getProduct() {
		return product;
	}
	public void setProduct(String product) {
		this.product = product;
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
}

1.2 2)映射文件
1.2.1 A、一:把集合放入set标签中,set标签中还增了一个<one-to-many>标签
<?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>
 <class name="com.creasoft.entity.Customer" table="customer">
  <id name="cid" type="long">
  	<generator class="identity"></generator>
  </id>
  <property name="customerName"  type="string" not-null="true"/>
  <property name="gender" type="string" length="2"></property>
  <!-- 双向的关联 ,key column的值一定要与CustomerOder.hbm.xml中的外键的值要一样 。name放的是集合属性
  	inverse="true",表示多的一方不必要在等一这边操作完,这的效率要高-->
  <set name="sustomerOrders" inverse="true">
  		<key column="customer_id"></key>
  		<one-to-many class="com.creasoft.entity.CustomerOrder"/>
  </set>
 </class>
</hibernate-mapping>

1.2.2 B、多:与单向的一样
<?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>
 <class name="com.creasoft.entity.CustomerOrder" table="customer_order">
  <id name="uid" type="long">
  	<generator class="identity"></generator>
  </id>
  <property name="product"  type="string" not-null="true"/>
  <many-to-one name="customer" 
  				class="com.creasoft.entity.Customer"
  				not-null="true"
  				column="customer_id"
  				cascade="delete"
  				></many-to-one>
 </class>
</hibernate-mapping>

1.3 3)插入一条数据:先要保存一的那一边的实体类

Session session = Hibernateutil.getSession();
		
		//开始事务
		Transaction tsaction = session.beginTransaction();

	Customer cs = new Customer();
	cs.setCustomerName("chen");
		cs.setGender("m");
		
		CustomerOrder co = new CustomerOrder("sdf",cs);
		CustomerOrder ct = new CustomerOrder("d",cs);
		cs.getSustomerOrders().add(co);
		cs.getSustomerOrders().add(ct);
		
		session.save(cs);
		session.save(co);
		session.save(ct);
		
		//提交事务
		tsaction.commit();
		//关闭session
		session.close();

7、一对一
1)实体类
A
package com.creasoft.entity2;

public class Customer {
	private long cid;
	private String customerName;
	private VipCard vipcard;
	
	
	/*
	 * 构造方法
	 */
	public Customer() {
		super();
	}
	public Customer(String customerName, VipCard vipcard) {
		super();
		this.customerName = customerName;
		this.vipcard = vipcard;
	}
	
	
	/*
	 * 标准的get、set方法
	 */
	public long getCid() {
		return cid;
	}
	
	public void setCid(long cid) {
		this.cid = cid;
	}
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
	public VipCard getVipcard() {
		return vipcard;
	}
	public void setVipcard(VipCard vipcard) {
		this.vipcard = vipcard;
	}
}

B、
package com.creasoft.entity2;

public class VipCard {
	private long vid;
	private float discount;
	private Customer customer;
	
	
	public VipCard() {
		super();
	}
	public VipCard(float discount, Customer customer) {
		super();
		this.discount = discount;
		this.customer = customer;
	}
	public long getVid() {
		return vid;
	}
	public void setVid(long vid) {
		this.vid = vid;
	}
	public float getDiscount() {
		return discount;
	}
	public void setDiscount(float discount) {
		this.discount = discount;
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
}

2)映射文件
A、主导类的映射文件:<one-to-one>
<?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>
 <class name="com.creasoft.entity2.Customer" table="vcustomer">
  <id name ="cid" type="long">
  	<generator class="identity"></generator>
  </id>
  <property name="customerName" type="string"></property>
 <one-to-one name="vipcard" 
 			class="com.creasoft.entity2.VipCard"
 			cascade="all"></one-to-one>
 </class>
</hibernate-mapping>
B、
<?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>
 <class name="com.creasoft.entity2.VipCard" table="vipcard">
  <id name="vid" type="long">
  	<generator class="foreign">
<!—customer必须与vipcard类中的属性名相同
  		<param name="property">customer</param>
  	</generator>
  </id>
  <!-- constrained为true表示name既是主键又是外键 -->
  <property name="discount" type="float"></property>
  <one-to-one name="customer" 
  			  constrained="true"
  			  class="com.creasoft.entity2.Customer"
  			  cascade="save-update"></one-to-one>
 </class>
</hibernate-mapping>

1.4 3)插入一条数据
Session session = Hibernateutil.getSession();
		
		//开始事务
		Transaction tsaction = session.beginTransaction();
		//数据的操作
		
		Customer cs = new Customer();
		cs.setCustomerName("chen");
		
		VipCard vc = new VipCard();
		vc.setDiscount(0.8f);
		
		vc.setCustomer(cs);
		cs.setVipcard(vc);
		
		
		session.save(cs);
		
		//提交事务
		tsaction.commit();
		//关闭session
		session.close();
	
	}

8、多对多
1)实体类
A、多
package com.creasoft.entity3;

import java.util.HashSet;
import java.util.Set;

public class Customer {
	private long cid;
	private String customerName;
	private Set<Address> addresses = new HashSet<Address>();
	
	
	
	
	
	public Customer() {
		super();
	}
	public Customer(String customerName, Set<Address> addresses) {
		super();
		this.customerName = customerName;
		this.addresses = addresses;
	}
	public long getCid() {
		return cid;
	}
	public void setCid(long cid) {
		this.cid = cid;
	}
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
	public Set<Address> getAddresses() {
		return addresses;
	}
	public void setAddresses(Set<Address> addresses) {
		this.addresses = addresses;
	}
}

B、多
package com.creasoft.entity3;

import java.util.HashSet;
import java.util.Set;

public class Address {
private long aid;
private String addressDetail;
private Set<Customer> customers = new HashSet<Customer>();






public Address() {
	super();
}
public Address(String addressDetail, Set<Customer> customers) {
	super();
	this.addressDetail = addressDetail;
	this.customers = customers;
}
public long getAid() {
	return aid;
}
public void setAid(long aid) {
	this.aid = aid;
}
public String getAddressDetail() {
	return addressDetail;
}
public void setAddressDetail(String addressDetail) {
	this.addressDetail = addressDetail;
}
public Set<Customer> getCustomers() {
	return customers;
}
public void setCustomers(Set<Customer> customers) {
	this.customers = customers;
}


}

2)映射文件:
有且只能在一边必须要加上inverse="true",不然出错
A、set标签中<many-to-many>
<?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>
 <class name="com.creasoft.entity3.Customer" table="acutstomer">
  <id name="cid" type="long">
  	<generator class="identity"></generator>
  </id>
  <property name="customerName" type="string"></property>
<!-- 两边的table,column必须要一致。这边key中column的必须与另一边many-to-many中column-->
  <set name="addresses" inverse="true" table="customer_address">
  	<key column="customerid"></key>
  	<many-to-many  class="com.creasoft.entity3.Address" 
  					column="addressid">
  	</many-to-many>
  </set>
 </class>
</hibernate-mapping>

B、set标签中<many-to-many>
<?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>
 <class name="com.creasoft.entity3.Address">
  <id name="aid">
  	<generator class="identity"></generator>
  </id>
  <property name="addressDetail"></property>
<!-- 两边的table,column必须要一致。这边key中column的必须与另一边many-to-many中column-->
  <set name="customers" table="customer_address">
  	<key column="addressid"></key>
  	<many-to-many class="com.creasoft.entity3.Customer"
  				  column="customerid"></many-to-many>
  </set>
 </class>
</hibernate-mapping>

3)插入数据库中
把属性都加入两集合中
package com.creasoft.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;


import com.creasoft.entity3.Customer;
import com.creasoft.entity3.Address;
import com.creasoft.util.Hibernateutil;

public class Test03 {

	
	public static void main(String[] args) {
		Session session =Hibernateutil.getSession();
		Transaction ts = session.beginTransaction();
		
		//对数据库的一些操作
		Customer cs1 = new Customer();
		Customer cs2 = new Customer();
		
		cs1.setCustomerName("盛开");
		cs2.setCustomerName("sk");
		
		Address ad1= new Address();
		Address ad2 = new Address();
		
		ad1.setAddressDetail("长沙");
		ad2.setAddressDetail("香港");
		
		cs1.getAddresses().add(ad1);
		cs1.getAddresses().add(ad2);
		ad1.getCustomers().add(cs1);
		ad1.getCustomers().add(cs2);
		
		ad1s =cs2.getAddresses();
		ad1s.add(ad1);
		cs2.getAddresses().add(ad2);
		ad2.getCustomers().add(cs1);
		ad2.getCustomers().add(cs2);
		//set集合到customer中
		Cs2.setAddresses(ad1s);
		
		session.save(cs1);
		session.save(cs2);
		session.save(ad1);
		session.save(ad2);
		
		
		ts.commit();
		session.close();

	}

}

9、 Hibernate对数据库的基本操作依赖的五个核心接口:
         Configuration界面:配置Hibernate,预启动Hibernate,创建SessionFactory对象
         SessionFactory界面:初始化Hibernate,充当数据源代理,创建Session对象
         Session界面:负责保存,更新,删除,加载和查询对象
         Transaction界面:管理事务
         Query和Criteria界面:执行数据库查询
10、hql
1)、Query接口:生命周期与session绑定
package com.creasoft.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.creasoft.entity.SCourse;
import com.creasoft.util.Hibernateutil;

public class Test2 {
	public static void main(String[] args) {
		Session session = Hibernateutil.getSesion();
<!—其中的scourse是类名
		Query query = session.createQuery("FROM SCourse");
<!—list只能适用小量的数据,效率比较高,容易产生内存溢出
		
		List<SCourse> c = query.list();
		
		for(SCourse s: c){
			System.out.println(s.getCourseName());
		}
		<!—iterator能查找大量的数据,但效率低
//		Iterator<SCourse> iterator = query.iterate();
//		
//		while(iterator.hasNext()){
//			SCourse s = iterator.next();
//			System.out.println(s.getCourseName());
//		}
		session.close();
	}
}

2)、增删改查的封装
//增加用户
Static void addUser(User user){
    Session s=null;
    Transaction tx=null;
        Try{
           S=Hibernateutil.getSession();
           tx=s.beginTransaction();
           s.save(User);
           tx.commit();
          }catch(HibernateException e){
              If(tx!=null){
                  tx.rollback();
                  throw e;
          }finally{
            If(s!=null){
                s.close();
               }
           }
  }
//修改
public static void update(User user){
		Session s=null;
		Transaction ts=null;
		try{
			s=sessionFactory.openSession();
			ts=s.beginTransaction();
			s.update(user);
			ts.commit();
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}
//删除
	public static void delete(User user){
		Session s=null;
		Transaction ts=null;
		try{
			s=sessionFactory.openSession();
			ts=s.beginTransaction();
			s.delete(user);
			ts.commit();
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}

2、 查询
1) Hql:是对对象操作,而不是表。
在sql中能用的方法在hql中一定能够使用,只是hql操作的是对象,sql是表。
a、 模糊查询
public List<TicketSite> findByTname(String Tname) {
		List<TicketSite> list = new ArrayList<TicketSite>();
		Session session = HibernateUtil.getSession();
// TicketSite是一个类名,而不是表名
		String hql = "from TicketSite t where t.train.Tname like ‘%"+Tname+“%’”;
		Query query = session.createQuery(hql);
		query.setString(0, Tname);
		
		list = query.list();
		
		
		return list;
	}

1.4.1.1 B、条件查询
1.4.2 //根据站名查询
	public List<TicketSite> findBySites(String start, String end) {
		List<TicketSite> list = new ArrayList<TicketSite>();
		Session session = HibernateUtil.getSession();
		String hql = "from TicketSite where siteFrom=? and siteTo=?";
		Query query = session.createQuery(hql);
		query.setString(0, start);
		query.setString(1,end);
		list = query.list();
		
		
		return list;
	}

	//根据车次查询
	public List<TicketSite> findByTname(String Tname) {
		List<TicketSite> list = new ArrayList<TicketSite>();
		Session session = HibernateUtil.getSession();
		String hql = "from TicketSite t where t.train.Tname=?";
		Query query = session.createQuery(hql);
		query.setString(0, Tname);
		
		list = query.list();
		
		
		return list;
	}

1.4.3 返回的是一个实体
//根据订单的编号查找订单的信息
public Order findOrder(long orderNumberd) {
		Order od = new Order();
		Session session = HibernateUtil.getSession();
		Transaction ts = session.beginTransaction();
		
		String hql="from Order where orderNumberd = ?";
		Query query = session.createQuery(hql);
		query.setLong(0, orderNumberd);
		od = (Order)query.uniqueResult();
		
		ts.commit();
		session.close();
		
		return od;
	}
2)	get、load
   session.get(Clazz, id);
session.load(Clazz, id);


1.5 级联删除
删除多的一边,先要使一的一边为空.
1.5.1 一的那边
//根据id,修改订单详细
	public boolean updateOrderProduct(OrderProduct op){
		boolean flag =false;
		Session session = HibernateUtil.getSession();
		Transaction ts = session.beginTransaction();
		System.out.println(op.getId());
		op = (OrderProduct)session.get(OrderProduct.class,op.getId());
		op.setOrder(null);
		session.update(op);
		flag = true;
		ts.commit();
		session.close();
		return flag;
	}

1.5.2 多
//删除订单中的商品
	public boolean delete(OrderProduct op){
		boolean flag = false;
		Session session = HibernateUtil.getSession();
		Transaction ts = session.beginTransaction();
		
		op = (OrderProduct)session.get(OrderProduct.class, op.getId());
		
		session.delete(op);
		
		flag = true;
		
		ts.commit();
		session.close();
		return flag;
	}

1.5.3 业务层
//删除订单中的商品
	public boolean delete(OrderProduct op){
		boolean flag = false;
		
		IOrderDao orderDao = new OrderDaoImpl();
		orderDao.updateOrderProduct(op);
		flag = orderDao.delete(op);
		
		return flag;
	}

1.6 级联插入
先要插入多的一面
1.6.1 Dao层
1.6.1.1 查找到一的实体
//根据id查找订单信息
	public Order findOd(long id){
		Order od = new Order();
		Session session = HibernateUtil.getSession();
		String hql = "from Order where id = ?";
		Query query = session.createQuery(hql);
		query.setLong(0, id);
		od = (Order)query.uniqueResult();
		session.close();
		return od;
	}

1.6.1.2 多
//插入操作历史
	public boolean insertOrderHistory(Order od, OrderHistory oh){
		boolean flag = false;
		Session session = HibernateUtil.getSession();
		Transaction ts = session.beginTransaction();
		
		oh.setOd(od);
		session.save(oh);
		session.update(od);
		flag = true;
		
		ts.commit();
		session.close();
		return flag;
	}

1.6.2 业务层
//判断插入的订单操作历史是否成功
	public boolean insertOrderHistory(long id, OrderHistory oh){
		boolean flag = false;
		
		IOrderDao orderDao = new OrderDaoImpl();
		//取得订单
		Order od = orderDao.findOd(id);
		flag = orderDao.insertOrderHistory(od, oh);
		
		return flag;
		
	}
分享到:
评论

相关推荐

    常用 Hibernate 映射配置说明.doc

    ### 常用Hibernate映射配置详解 #### 1. hibernate-mapping节点解析 `hibernate-mapping`节点是Hibernate映射文件中的顶级节点,用于定义一系列配置选项,控制整个映射文件的行为和映射规则。这些配置包括数据库...

    Hibernate映射配置详解

    本篇文章将深入探讨Hibernate的映射配置,帮助你理解如何在实际项目中有效地使用Hibernate。 一、Hibernate概述 Hibernate的核心功能在于它的持久化层,通过ORM机制将Java对象与数据库表进行绑定。它提供了一种在...

    Hibernate调用配置文件中的sql语句

    本篇将深入探讨如何在Hibernate中调用配置文件中的SQL语句,以此提高代码的可维护性和灵活性。 首先,理解Hibernate的核心概念至关重要。Hibernate是一个对象关系映射(ORM)框架,它将Java对象与关系数据库中的...

    hibernate映射和查询

    **hibernate映射与查询** Hibernate 是一个流行的 Java 应用程序开发框架,它提供了一个持久层解决方案,简化了数据库操作。对于初学者来说,理解 Hibernate 的映射和查询机制是至关重要的,因为它们构成了 ...

    hibernate查询语句--HQL

    ### Hibernate 查询语句 -- HQL #### 概述 Hibernate 是一个开源的对象关系映射 (ORM) 框架,它允许开发人员将 Java 对象映射到数据库表中的记录,从而简化了数据访问逻辑。在 Hibernate 中,执行查询操作的主要...

    Hibernate-HQL语句多对多写法

    "Hibernate-HQL语句多对多写法" Hibernate 是一个流行的 Java 持久层框架,它提供了强大的对象关系映射(ORM)功能,使得开发者可以方便地与数据库交互。但是,在使用 Hibernate 时,我们经常会遇到多对多关系的...

    myeclipse自动生成hibernate映射文件

    Hibernate映射文件是XML格式的文件,它定义了数据库表与Java实体类之间的对应关系,包括字段映射、主键策略、关联关系等。映射文件让开发者无需编写SQL语句,就能通过Java对象进行数据库操作。 2. **MyEclipse ...

    java根据实体类生成Hibernate映射文件

    首先,我们需要了解Hibernate映射文件的基本结构。通常,一个映射文件以`.hbm.xml`为扩展名,它使用XML格式来描述Java类和数据库表的对应关系。映射文件包含了类名、表名、字段及其数据类型等信息。例如: ```xml ...

    hql语句大全hql语句大全

    ### HQL语句详解:精通Hibernate查询语言 #### 引言 HQL(Hibernate Query Language)是Hibernate框架中用于执行数据库操作的一种强大的查询语言。它提供了面向对象的语法,允许开发人员以一种接近于编程语言的方式...

    Hibernate使用xdoclet生成映射文件和sql语句

    对于Hibernate,开发者可以在实体类上添加如`@Entity`、`@Table`、`@Column`等注解,XDoclet会读取这些注解,然后自动生成对应的Hibernate映射文件和数据库表创建的SQL语句。 1. **XDoclet使用**: - 安装与集成:...

    hibernate执行原生sql语句

    然而,在一些情况下,我们需要直接执行原生 SQL 语句,而不是使用 Hibernate 的查询语言(HQL)。本文将介绍如何使用 Hibernate 执行原生 SQL 语句。 为什么需要执行原生 SQL 语句 在实际开发中,我们可能需要执行...

    hibernate 查询?Hibernate的HQL查询

    1. HQL基础:HQL基于面向对象的概念,将表映射为类,列映射为属性,使得查询更符合Java开发者的思维模式。它支持连接(inner/outer/full joins)、投影、聚合、分组、排序、子查询以及SQL函数调用。 2. 简单HQL示例...

    hibernate映射配置文件不支持union联合查询[参照].pdf

    本案例中,开发者遇到了一个与Hibernate映射配置文件相关的异常,该异常涉及到不支持的“union”联合查询。具体表现为在执行查询时抛出了`SQLGrammarException`,提示为`ORA-00907: 缺失右括号`,这通常意味着SQL...

    常用的hql语句用法解析

    在Java开发中,Hibernate是一个非常流行的ORM(对象关系映射)框架,它提供了一种将数据库操作转换为面向对象的方式。HQL(Hibernate Query Language)是Hibernate提供的面向对象的查询语言,它类似于SQL,但更加...

    hibernate3.2 (十)HQL查询

    【hibernate3.2 (十)HQL查询】这篇博文主要探讨了Hibernate框架中的HQL(Hibernate Query Language)查询语言,它是Hibernate提供的一种面向对象的查询语言,用于操作持久化对象,类似于SQL但更加面向对象。...

    hibernate hql where语句拼接工具类

    HQL(Hibernate Query Language)是Hibernate提供的一个SQL的面向对象的查询语言,它使得开发者可以避免直接编写SQL语句,从而更好地进行数据库操作。本文将深入探讨“hibernate hql where语句拼接工具类”,并结合...

    hibernate映射心得 原创

    访问这个链接可以获取更详细的信息,比如作者可能深入讨论了Hibernate映射的各个方面,包括XML配置、注解方式的映射,以及在实际项目中的应用案例。 **标签解析:**“源码”和“工具” “源码”标签表明文章可能会...

    常用的HQL语句下载

    在本文中,我们将深入探讨HQL(Hibernate Query Language),一种专为Hibernate ORM框架设计的查询语言,用于在Java应用程序中操作数据库。以下是一些常用HQL语句的使用与说明: 1. **HQL更新语句**: 更新操作...

Global site tag (gtag.js) - Google Analytics