- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试对 HibernateTemplate 进行快速的小测试。但我总是遇到异常:org.hibernate.HibernateException:没有绑定(bind)到线程的 Hibernate session ,并且配置不允许在此处创建非事务性 session 。
我怎样才能使代码工作?这是代码:
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>
和 Hibernate 映射文件:
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>
最佳答案
我认为您正在尝试使用未包装的对象而不是富含事务行为的代理。您的初始化应该是:
private static SpringDao springRunner = (SpringDao) ctx.getBean("springDaoTarget");
相应地更改您的名字。顺便说一句,TransactionProxyFactoryBean
如今被认为是“旧式”。您可以考虑改用 @Transactional
。
关于java - 如何在我的测试代码中直接使用 HibernateTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8950777/
如果我在 C 中调用一个函数并传入一个结构(对那些 C++ 读者来说不是通过指针或引用),它会复制该对象。如果我传入一个包含数组的结构,它会复制该数组(如教授在类里面所说)。但是,如果我传入一个包含对
在 vim 等中,您可以使用 CTRLA 和 CTRLX 增加或减少光标所在的数字。然而,这会增加总数,但我想简单地增加光标正下方的数字。这有点难以描述,所以这就是我的意思: Ctrl+A usage
我正在将 Spring 4.3.2 项目升级到 Spring 5.1.5。我的一个测试用例开始因错误而失败。 ClassNotFoundException: org.hibernate.propert
我想在 Java 中分配一个直接 IntBuffer,比如说 10 亿个元素(64 位系统)。我知道的唯一方法是创建一个直接 ByteBuffer 并将其视为直接 IntBuffer。但是,4*1,0
我正在寻找特定的打印机或某些打印机上存在的技术(接口(interface)、标准、协议(protocol)),这使得可以使用 AJAX 从 Web 浏览器实现直接打印。 这意味着打印机必须: 网络接口
我正在寻求实现删除确认表单的最佳实践建议。 除其他选项外,以下页面包含删除按钮... /website/features/f/123 ...当点击一个简单的表单时,会在以下 url 下加载: /web
我正在使用直接 Web 远程处理库在我的应用程序中执行一些 ajax 调用。我有一个问题,我认为归结为服务调用的延迟响应。以下是我认为有问题的部分代码。问题出在 getDefaultReviewerT
我想替换 Javascript confirm() 函数以允许自定义按钮而不是 Yes/Cancel。我尝试搜索,但所有解决方案都是事件驱动的,例如 jquery 对话框(代码不等待响应但它是事件驱动
我知道有几个类似的问题,但是,其中的示例并没有说明问题,或者我无法从中获利 - 我真可耻。 所以我的问题是在带有 GUI 的简单应用程序中加载图像。例如: 我在 "D:\javaeclipseprog
我想用不同的颜色为表格的行着色,所以我正在使用它 table#news tr:nth-child(even) { background-color: red; } table#news
下面的测试代码不起作用 from("direct:start").setExchangePattern(ExchangePattern.InOnly).threads(5).delay(2000).b
我在 python 中实现的第一个项目之一是对棒渗流进行蒙特卡罗模拟。代码不断增长。第一部分是棍子渗滤的可视化。在宽度*长度的区域中,使用随机起始坐标和方向绘制具有一定长度的直棒的定义密度(棒/面积)
跟踪直接文件下载的最佳方法是什么?我找到了一些解决方案,例如这个: http://www.gayadesign.com/diy/download-counter-in-php-using-htacce
我在一个线程中有一个直接的 ByteBuffer(堆外),并使用 JMM 给我的一种机制将它安全地发布到另一个线程。 happens-before 关系是否扩展到由 ByteBuffer 包装的 na
当我测试直接 java.nio.ByteBuffer 的读取性能时,我注意到绝对读取平均比相对读取快 2 倍。此外,如果我比较相对读取与绝对读取的源代码,除了相对读取维护和内部计数器外,代码几乎相同。
我知道这个问题已经被问了无数次,并且在很多情况下都得到了答案。我相信我已经阅读了其中的大部分内容。不幸的是,我在这上面能找到的一切 简单说明 ElementRef.nativeElement不好,不要
回到一些 C 语言工作。 我的许多函数看起来像这样: int err = do_something(arg1, arg2, arg3, &result); 根据意图,结果由函数填充,返回值是调用的状态
当我将 XML 提交到 https://secure-test.WorldPay.com/jsp/merchant/xml/paymentService.jsp 时: Personalised
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我的 Angular 路由行为有问题。刷新或输入的 url 像/user 总是将我重定向到/home。我还在 index.html 文件中设置了 。通过单击导航菜单按钮一切正常。但是一旦我尝试刷新页面
我是一名优秀的程序员,十分优秀!