spring 直接使用 hibernate native api

===========spring.xml,TestDao.java,DomainObject.java,Client.java========

 

===========spring.xml=========

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
		   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">


	<bean id="placeHolder"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
		p:locations="classpath:jdbc.properties" />

	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName"
			value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="annotatedClasses">
			<list>
				<value>com.spring.ch11.dao.hibernate.demo.DomainObject</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQL5Dialect
				</prop>
			</props>
		</property>
	</bean>

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<bean id="com.spring.ch11.dao.hibernate.demo.TestDao"
		class="com.spring.ch11.dao.hibernate.demo.TestDao"
		p:sessionFactory-ref="sessionFactory">
	</bean>

</beans>

 

===================TestDao.java

package com.spring.ch11.dao.hibernate.demo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class TestDao {
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getSession() {
		return this.sessionFactory.openSession();
	}

}

  

 

===================DomainObject.java

package com.spring.ch11.dao.hibernate.demo;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "demo")
public class DomainObject implements Serializable {
	private int id;
	private float unitPrice;
	private String name;

	@Id
	public int getId() {
		return id;
	}

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

	public float getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(float unitPrice) {
		this.unitPrice = unitPrice;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

  

 

===================Client.java

 

package com.spring.ch11.dao.hibernate.demo;

import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml",Client.class);
		TestDao testBean = (TestDao) context.getBean(TestDao.class.getName());
		Session init = testBean.getSession();
		System.out.println(init);
//		init.beginTransaction();
		List<DomainObject> demoList = init.createQuery("from DomainObject").list();
		for (DomainObject demo : demoList) {
			System.out.println(ToStringBuilder.reflectionToString(demo));
		}
//		init.getTransaction().commit();
	}
}

 

 

说明:openSession改成getCurrentSession的话就会报错No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here。

此时解决方案是:

在hibernateProperties中添加

<prop key="hibernate.current_session_context_class">
	thread
</prop>
<prop key="hibernate.transaction.factory_class">
	org.hibernate.transaction.JDBCTransactionFactory
</prop>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值