2012-01-21 4 views
2

나는 HibernateTemplate에 대한 빠른 테스트를 시도하고있다. 하지만 예외가 항상 발생합니다 : org.hibernate.HibernateException : 하이버 네이트 세션이 쓰레드에 바인드되지 않았으며, 컨피규레이션은 비 트랜잭션 (non-transactional) 객체를 생성 할 수 없습니다.내 테스트 코드에서 HibernateTemplate을 직접 사용하는 방법

어떻게 코드를 작동시킬 수 있습니까? 코드를 바로 여기에 :

SpringDao.java :

package com.question; 

import java.util.Iterator; 

import java.util.List; 
import java.util.Map; 
import java.util.Set; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.springframework.aop.framework.Advised; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.orm.hibernate3.HibernateTemplate; 
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 
import org.springframework.transaction.support.TransactionSynchronizationManager; 

import com.lavin.test.app.dao.hibernate.a.PersonC; 
import com.lavin.test.app.dao.hibernate.a.PersonCId; 
import com.lavin.test.app.net.ISimpleServerRunner; 

public class SpringDao { 

private HibernateTemplate hibernateTemplate = null; 

public void setSessionFactory(SessionFactory sessionFactory) { 
this.hibernateTemplate = new HibernateTemplate(sessionFactory, false); 
} 


public static ApplicationContext ctx = new ClassPathXmlApplicationContext("dao.xml"); 

private static SpringDao springRunner = (SpringDao) ctx.getBean("springDao"); 


public static SpringDao getInstance() { 
return springRunner; 
} 

PersonC newPC(String str) { 
PersonC p0 = new PersonC(); 
PersonCId i0 = new PersonCId(); 
i0.setFirstname(str); 
i0.setLastname(str); 
p0.setId(i0); 
return p0; 
} 



void test1() { 
PersonC p = new PersonC(); 
PersonCId id = new PersonCId(); 
id.setFirstname("b"); 
id.setLastname("b"); 
p.setId(id); 
springRunner.getHibernateTemplate().save(p); 
} 


public static void main(String[] s) throws Exception { 
new SpringDao().test1(); 
} 




/** 
* @return Returns the hibernateTemplate. 
*/ 
public HibernateTemplate getHibernateTemplate() { 
return hibernateTemplate; 
} 



} 

dao.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:context="http://www.springframework.org/schema/context" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
      http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-2.5.xsd 
      http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 


<context:property-placeholder location="classpath:jdbc.properties" /> 
<context:annotation-config /> 

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
    lazy-init="true"> 
    <property name="driverClass"> 
     <value>${jdbc.driverClassName}</value> 
    </property> 
    <property name="jdbcUrl"> 
     <value>${jdbc.url}</value> 
    </property> 
    <property name="user"> 
     <value>${jdbc.username}</value> 
    </property> 
    <property name="password"> 
     <value>${jdbc.password}</value> 
    </property> 
    <property name="initialPoolSize"> 
     <value>5</value> 
    </property> 
    <property name="minPoolSize"> 
     <value>5</value> 
    </property> 
    <property name="maxPoolSize"> 
     <value>30</value> 
    </property> 
    <property name="idleConnectionTestPeriod"> 
     <value>10</value> 
    </property> 
    <property name="testConnectionOnCheckin"> 
     <value>true</value> 
    </property> 
    <property name="maxIdleTime"> 
     <value>1800</value> 
    </property> 
    <property name="properties"> 
     <props> 
      <prop key="user">${jdbc.username}</prop> 
      <prop key="password">${jdbc.password}</prop> 
     </props> 
    </property> 
</bean> 

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="mappingLocations"> 
     <value>classpath:com/question/*.hbm.xml</value> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect"> 
       ${hibernate.dialect} 
      </prop> 
      <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
      <prop key="hibernate.cache.use_query_cache">false</prop> 
      <prop key="hibernate.cache.use_second_level_cache"> 
       true 
      </prop> 
      <prop key="hibernate.cache.provider_class"> 
       org.hibernate.cache.EhCacheProvider 
      </prop> 
      <prop key="hibernate.cache.provider_configuration_file_resource_path"> 
       ehcache-hibernate.xml 
      </prop> 
     </props> 
    </property> 
</bean> 

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

<bean id="txProxyTemplate" lazy-init="true" 
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
    <property name="transactionManager"> 
     <ref local="transactionManager" /> 
    </property> 

    <property name="transactionAttributes"> 
     <props> 
      <prop key="test*">PROPAGATION_REQUIRED</prop> 
      <prop key="oper*">PROPAGATION_MANDATORY</prop> 
      <prop key="*">PROPAGATION_REQUIRED</prop> 
      <prop key="nTest*">PROPAGATION_REQUIRES_NEW</prop> 
     </props> 
    </property> 
</bean> 

<bean id="springDaoTarget" parent="txProxyTemplate"> 
    <property name="target"> 
     <ref local="springDao" /> 
    </property> 
</bean> 


<bean id="springDao" class="com.question.SpringDao"> 
    <property name="sessionFactory"> 
     <ref local="sessionFactory" /> 
    </property> 
</bean> 

및 최대 절전 모드 매핑 파일 :

package com.question; 

import java.io.Serializable; 
public class PersonCId implements Serializable { 

private String firstname; 
private String lastname; 

public PersonCId() { 
} 

public PersonCId(String firstName, String lastName) { 
    this.firstname = firstName; 
    this.lastname = lastName; 
} 

public String getFirstname() { 
    return firstname; 
} 

public void setFirstname(String firstname) { 
    this.firstname = firstname; 
} 

public String getLastname() { 
    return lastname; 
} 

public void setLastname(String lastname) { 
    this.lastname = lastname; 
} 

} 

package com.question; 

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

public class PersonC { 

private int age; 
private PersonCId id; 

private Set addresses; 

public PersonC() { 
} 

private Set emailAddresses = new HashSet(); 

public Set getEmailAddresses() { 
    return emailAddresses; 
} 

public void setEmailAddresses(Set emailAddresses) { 
    this.emailAddresses = emailAddresses; 
} 

private Set events = new HashSet(); 

// Defensive, convenience methods 
protected Set getEvents() { 
    return events; 
} 

protected void setEvents(Set events) { 
    this.events = events; 
} 



/** 
* @return Returns the addresses. 
*/ 
public Set getAddresses() { 
    return addresses; 
} 

/** 
* @param p_addresses 
*   The addresses to set. 
*/ 
public void setAddresses(Set p_addresses) { 
    addresses = p_addresses; 
} 

/** 
* @return Returns the age. 
*/ 
public int getAge() { 
    return age; 
} 

/** 
* @param p_age 
*   The age to set. 
*/ 
public void setAge(int p_age) { 
    age = p_age; 
} 

/** 
* @return Returns the id. 
*/ 
public PersonCId getId() { 
    return id; 
} 

/** 
* @param p_id 
*   The id to set. 
*/ 
public void setId(PersonCId p_id) { 
    id = p_id; 
} 

} 

<?xml version="1.0"?> 
<!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.question.PersonC" table="PERSONC"> 

    <composite-id name="id" class="com.question.PersonCId"> 
     <key-property name="firstname" type="string"> 
      <column name="firstname" length="50"/> 
     </key-property> 
     <key-property name="lastname" type="string"> 
      <column name="lastname" length="50"/> 
     </key-property>    
    </composite-id> 

    <property name="age"/> 




</class> 

</hibernate-mapping> 

답변

0

I을 너는 너를 사용하려한다고 생각해. 트랜잭션 대신 트랜잭션을 사용하는 프록시 대신 nwrapped 객체. 초기화는 다음과 같아야합니다.

private static SpringDao springRunner = (SpringDao) ctx.getBean("springDaoTarget"); 

이름도 적절하게 변경하십시오. 그런데 TransactionProxyFactoryBean는 요즘 "구식"으로 간주됩니다. 대신 @Transactional을 사용해보세요.

관련 문제