- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尽管这里有一堆类似的问题,但对我来说似乎没什么用。当我调用 sessionFactory.getCurrentSession().createQuery("from Strings").list(); 时出现异常;
这是我的 root-context.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config/>
<context:component-scan base-package="org.vadim.testmvc.dao"/>
<context:component-scan base-package="org.vadim.testmvc.service"/>
<import resource="data.xml"/>
</beans>
我的数据.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/jdbc.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
</props>
</property>
</bean>
</beans>
TestController.java(我删除了 import 语句以免使问题过载):
package org.vadim.testmvc;
@Controller
public class TestController {
@Autowired
TestService testservice;
List<String> list = new LinkedList<String>();
int i=0;
@RequestMapping("/")
public String hello() {
return "redirect:/helloworld";
}
@RequestMapping(value = "/helloworld")
String listWord(Map<String, Object> map){
map.put("addRepeat", new Strings());
map.put("listStrings", testservice.listStrings());
return "helloworld";
}
@RequestMapping(value="/repeat", method = RequestMethod.POST)
String addRepeat(@ModelAttribute("addRepeat") Strings strings, BindingResult result) {
testservice.addStrings(strings);
return "redirect:/helloworld";
}
}
测试服务.java:
package org.vadim.testmvc.service;
@Service
public class TestService {
@Autowired
TestDAO testdao;
@Transactional
public List<Strings> listStrings(){
return testdao.listStrings();
}
@Transactional
public void addStrings(Strings strings){
testdao.addStrings(strings);
}
}
最后是 TestDAO.java:
package org.vadim.testmvc.dao;
@Transactional
@Repository
public class TestDAO{
@Autowired
private SessionFactory sessionFactory;
public void addStrings(Strings strings){
sessionFactory.getCurrentSession().save(strings);
}
@SuppressWarnings("unchecked")
public List<Strings> listStrings(){
return sessionFactory.getCurrentSession().createQuery("from Strings").list();
}
}
异常发生后的堆栈:
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/TestMVC] threw exception [Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here] with root cause
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:544)
at org.vadim.testmvc.dao.TestDAO.listStrings(TestDAO.java:23)
at org.vadim.testmvc.service.TestService.listStrings(TestService.java:18)
at org.vadim.testmvc.TestController.listWord(TestController.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:851)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.invoke(HttpRequestOperationCollectionValve.java:84)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:278)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
hibernate .cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="org.vadim.testmvc.model.Strings"/>
</session-factory>
</hibernate-configuration>
字符串.java:
package org.vadim.testmvc.model;
@Entity
@Table(name="STRINGS")
public class Strings {
@Id
@Column(name="ID")
@GeneratedValue
private Integer id;
public Integer getId(){
return id;
}
@Column(name="TEXT")
private String text;
public String getText(){
return text;
}
public void setText(String text){
this.text=text;
}
}
在此问题上,我将不胜感激。
最佳答案
我认为问题出在您没有通过界面访问服务这一事实。默认情况下,Spring 使用基于 Java 接口(interface)的代理。
顺便说一句,堆栈跟踪不包含任何事务拦截器调用。
请参阅 this paragraph of the Spring 下的第二条注释文档:
The proxy-target-class attribute on the element controls what type of transactional proxies are created for classes annotated with the @Transactional annotation. If proxy-target-class attribute is set to true, class-based proxies are created. If proxy-target-class is false or if the attribute is omitted, standard JDK interface-based proxies are created. (See Section 7.6, “Proxying mechanisms” for a discussion of the different proxy types.)
因此,要么为您的交易服务和 DAO 引入接口(interface),要么使用 proxy-target-class="true"
。
关于java - 我收到 HibernateException "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7685403/
我正在做这个项目,其中我有一个用 Java (HttpServlet) 完成的 Web 服务,它使用 Hibernate 选择、插入和更新信息到数据库中,并将对象数组作为 JSON 返回给连接到 JS
我尝试同时修改大约 14 个 REST 实体。 前几次修改是成功的(以及中间的一些修改),其他一些修改偶尔会失败,出现 HibernateException。 这是我的 servlet: public
假设我们有一些看起来像这样的实体(我会尽量保持简单): @Entity class MainEntity { //nothing too special here } @Entity class
我有这个项目设置,我正在尝试使用 JPA 和 Hibernate 作为提供程序来将数据存储在我的本地 h2 数据库中。它全部配置为我的 spring 项目的一部分,我使用 tomcat 作为我的服务器
我正在尝试测试 hibernate 在我的项目中是否工作,因为我正在尝试创建 session ,IDEA 告诉我 org.hibernate.HibernateException 和 java.lan
我正在使用带有基本 hibernate 程序的 MySQL Workbench 5.2 CE 数据库在运行时它给我错误: Caused by: org.hibernate.HibernateExcep
我有一个执行以下步骤的应用程序: 在 session 中放置对象: def product = Product.get(1) session["product"] = product 执行 Ajax
我在 Hibernate 和 Spring 开发方面绝对是新手,我有以下问题 我有一个名为 KM_ProjectInfoStatus 的实体类,它映射一个名为 KM_PROJECT_INFO_STAT
我不断收到此错误...使用 appach tomcat 的 Eclips IDE 根本原因 java.lang.ClassNotFoundException: org.hibernate.Hibern
我是 Hibernate 的新手。我创建了一个简单的程序来使用 Hibernate 将值插入数据库。 我的文件结构是这样的 src -> com.visolve -> AddStudent.java
嗨,我面临一个非常奇怪的问题。我正在使用 Spring MVC 3.0+Spring Security 3.2+hibernate 4.3。在登录之前,我可以在登录之前访问数据库并获取实体。但是通过
我想使用审核数据库插件将构建信息记录到数据库中。我已正确安装插件并使用以下配置: JDBC Driver class: org.hsqldb.jdbc.JDBCDriver** JDBC Url:jd
我在我的 Web 应用程序中使用 Spring Transactional 和 AspectJ。 应用程序上下文.xml
我目前使用confluence和mysql制作个人wiki。但是我有 500 异常 HibernateException。 汇合日志: Caused by: com.atlassian.conflue
这个问题在这里已经有了答案: java.lang.ClassNotFoundException: org.hibernate.HibernateException (5 个答案) 关闭 5 年前。
我是 Hibernate 的新手,在应用程序中定义 JPQL 查询时遇到以下问题。 所以我有以下情况: 1)我有这个模型KMCountryArea类,它被注释为将其映射到数据库表并定义了一个查询 @N
这是代码的最小版本,我花了很多时间才弄清楚为什么它不起作用: Query q = session.createQuery(queryString); q.setTimestamp(0, new jav
我试图删除数据库中不存在的实体,但 delete() 方法没有抛出任何异常。 当我试图删除一个不存在的实体时,如何得到一个错误? 我已经在下面复制了我的代码: public void remove(M
我收到此错误消息: error: Found shared references to a collection: Person.relatedPersons 当我尝试执行 addToRelatedP
完整的堆栈跟踪: org.hibernate.HibernateException: Could not instantiate resultclass: com.firstbankpr.lmu.da
我是一名优秀的程序员,十分优秀!