- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我想通过 hibernate 连接到我的数据库时,我遇到了这个异常,我尝试了很多在互联网上找到的东西,但没有任何帮助,我的一些文件:带连接的 dao 类:
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
SessionFactory sessionFactory;
//the problem with query is here
public List<User> getAllUsers() {
return sessionFactory.getCurrentSession().createSQLQuery("SELECT * FROM user").list();
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
我的servlet:
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.lime" />
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
和context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop>
<prop key="hibernate.connection.url">jdbc:postgresql://localhost:5432/come_to_blog_db</prop>
<prop key="hibernate.connection.username">postgres</prop>
<prop key="hibernate.connection.password">admin</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.lime.model.User</value>
</list>
</property>
</bean>
</beans>
最佳答案
试试这个
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
SessionFactory sessionFactory;
//the problem with query is here
public List<User> getAllUsers() {
Session session=null;
try
{
Session session = sessionFactory.openSession();
return session.createSQLQuery("SELECT * FROM user").list();
}
catch(Exception e)
{
//Logging
}
finally
{
if(session !=null && session.isOpen)
{
session.close();
session=null;
}
}
}
}
更新
使用 genericDAO,它获取需要使用 openSession() 显式打开的当前 session ,而 getCurrentSession() 只是将其附加到当前 session 。据作者介绍
GenericDAO makes the assumption that you will be handling transactions externally to the DAO
关于java - org.hibernate.HibernateException : createSQLQuery is not valid without active transaction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24468602/
我正在做这个项目,其中我有一个用 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
我是一名优秀的程序员,十分优秀!