- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在 FUSE 服务器上进行 Hibernate 的简单演示,我在这个 github location 中找到了这个示例代码示例.我尝试在集成了 Hibernate 4 的 FUSE 6.3 中运行此代码,我能够安装所有必需的依赖项,但是当我尝试执行数据库操作时,出现以下错误。
org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:997)
at com.vcc.poc.dao.impl.StudentDaoImpl.getStudent(StudentDaoImpl.java:67)
at sun.reflect.GeneratedMethodAccessor242.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_111]
at java.lang.reflect.Method.invoke(Method.java:498)[:1.8.0_111]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.osgi.service.importer.support.internal.aop.ServiceInvoker.doInvoke(ServiceInvoker.java:58)[org.springframework.osgi:spring-osgi-core:1.2.1]
at org.springframework.osgi.service.importer.support.internal.aop.ServiceInvoker.invoke(ServiceInvoker.java:62)[org.springframework.osgi:spring-osgi-core:1.2.1]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:132)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:120)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.osgi.service.util.internal.aop.ServiceTCCLInterceptor.invokeUnprivileged(ServiceTCCLInterceptor.java:56)[org.springframework.osgi:spring-osgi-core:1.2.1]
at org.springframework.osgi.service.util.internal.aop.ServiceTCCLInterceptor.invoke(ServiceTCCLInterceptor.java:39)[org.springframework.osgi:spring-osgi-core:1.2.1]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.osgi.service.importer.support.LocalBundleContextAdvice.invoke(LocalBundleContextAdvice.java:59)[org.springframework.osgi:spring-osgi-core:1.2.1]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:132)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:120)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at com.sun.proxy.$Proxy93.getStudent(Unknown Source)
at com.vcc.poc.service.impl.StudentServiceImpl.getStudent(StudentServiceImpl.java:15)
at sun.reflect.GeneratedMethodAccessor240.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_111]
at java.lang.reflect.Method.invoke(Method.java:498)[:1.8.0_111]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at com.sun.proxy.$Proxy94.getStudent(Unknown Source)
at sun.reflect.GeneratedMethodAccessor240.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_111]
at java.lang.reflect.Method.invoke(Method.java:498)[:1.8.0_111]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.osgi.service.importer.support.internal.aop.ServiceInvoker.doInvoke(ServiceInvoker.java:58)[org.springframework.osgi:spring-osgi-core:1.2.1]
at org.springframework.osgi.service.importer.support.internal.aop.ServiceInvoker.invoke(ServiceInvoker.java:62)[org.springframework.osgi:spring-osgi-core:1.2.1]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:132)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:120)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.osgi.service.util.internal.aop.ServiceTCCLInterceptor.invokeUnprivileged(ServiceTCCLInterceptor.java:56)[org.springframework.osgi:spring-osgi-core:1.2.1]
at org.springframework.osgi.service.util.internal.aop.ServiceTCCLInterceptor.invoke(ServiceTCCLInterceptor.java:39)[org.springframework.osgi:spring-osgi-core:1.2.1]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.osgi.service.importer.support.LocalBundleContextAdvice.invoke(LocalBundleContextAdvice.java:59)[org.springframework.osgi:spring-osgi-core:1.2.1]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:132)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:120)[org.apache.servicemix.bundles:org.apache.servicemix.bundles.spring-aop:3.2.16.RELEASE_1]
这是我的 StudentImpl.java 文件内容。
package com.vcc.poc.dao.impl;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.internal.SessionImpl;
import com.vcc.poc.dao.StudentDao;
import com.vcc.poc.model.Student;
public class StudentDaoImpl implements StudentDao{
private static final transient Log LOG = LogFactory.getLog(StudentDaoImpl.class);
/** The session factory. */
private SessionFactory sessionFactory;
/** The q. */
private Query q = null;
/** The Constant findStudentByReference. */
private final static String findStudentByReference =
"select i from Student as i where i.studentRef = :ref";
/** The Constant findStudent. */
private final static String findStudent =
"select i from Student as i";
/**
* Sets the session factory.
*
* @param sessionFactory the new session factory
*/
public void setSessionFactory( SessionFactory sessionFactory )
{
this.sessionFactory = sessionFactory;
}
/*
* (non-Javadoc)
* @see org.apache.camel.example.reportincident.dao.IncidentDAO#findIncident()
*/
public List<Student> findStudent()
throws HibernateException
{
// Prepare query
q = this.sessionFactory.getCurrentSession().createQuery( findStudent );
// Retrieve the Incidents from database
List<Student> list = q.list();
return list;
}
/*
* (non-Javadoc)
* @see org.apache.camel.example.reportincident.dao.IncidentDAO#getIncident(long)
*/
public Student getStudent(int id)
{
return (Student) this.sessionFactory.getCurrentSession().get( Student.class, id );
}
/*
* (non-Javadoc)
* @see org.apache.camel.example.reportincident.dao.IncidentDAO#saveIncident(org.apache.camel.example.reportincident.model.Incident)
*/
public void saveStudent(Student Student)
{
SessionImpl session = (SessionImpl) this.sessionFactory.getCurrentSession();
this.sessionFactory.getCurrentSession().saveOrUpdate( Student );
}
}
根据异常下面是抛出异常的那一行
return (Student) this.sessionFactory.getCurrentSession().get( Student.class, id );
这是我在 META-INF/spring 文件夹中的 data-source-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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:osgi="http://www.springframework.org/schema/osgi"
xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://www.springframework.org/schema/osgi-compendium
http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd">
<context:annotation-config />
<context:property-placeholder properties-ref="preProps" />
<!-- DB connection and persistence layer -->
<!-- DataSource Definition -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!-- Spring Data Access Exception Translator Definition -->
<bean id="jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- Hibernate SessionFactory Definition -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="mappingLocations">
<list>
<value>classpath*:META-INF/com/vcc/poc/model/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</prop>
<!--
<prop key="hibernate.transaction.manager_lookup_class">org.springframework.orm.hibernate3.LocalTransactionManagerLookup</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
-->
</props>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!--
<property name="jtaTransactionManager">
<osgi:reference>
<osgi:interfaces>
<value>org.springframework.transaction.PlatformTransactionManager</value>
<value>javax.transaction.TransactionManager</value>
</osgi:interfaces>
</osgi:reference>
</property>
-->
</bean>
<!-- Hibernate Transaction Manager Definition -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!--
Hibernate Transaction Manager exported as OSGI service because used
org.apache.camel.example.reportincident.service
<osgi:service id="transactionManagerOsgi" ref="transactionManager"
interface="org.springframework.transaction.PlatformTransactionManager" />
-->
<osgix:cm-properties id="preProps" persistent-id="com.vcc.poc.dao.datasource">
<prop key="driverClassName">com.mysql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql:///demo</prop>
<prop key="username">root</prop>
<prop key="password">password</prop>
</osgix:cm-properties>
</beans>
这是我的项目结构。
有没有人在 FUSE hibernate 集成中遇到过此类问题。我能够毫无问题地安装项目。但是每当我尝试执行数据库操作时,我都会收到此错误。
请专家帮我解决这个问题。
最佳答案
您可以通过更改以下内容来解决它。
根据以下帖子将以下内容添加到 beans 标记中的 schemaLocation。
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
您还应该在其中添加以下行。基于following post.
<tx:annotation-driven/>
<context:annotation-config />
<context:component-scan base-package="com.vcc.poc"></context:component-scan>
然后为例如方法添加@Transactional注解
@Transactional
public Student getStudent(int id)
更改的 beans 注释应类似于以下内容。
<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:osgi="http://www.springframework.org/schema/osgi"
xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://www.springframework.org/schema/osgi-compendium
http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
<tx:annotation-driven/>
<context:annotation-config />
<context:property-placeholder properties-ref="preProps" />
<context:component-scan base-package="com.vcc.poc"></context:component-scan>
关于java - FUSE hibernate 应用程序抛出 No Session found for current thread 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41174926/
什么是 hibernate 和n- hibernate ?我可以在 Visual Studio 2008 中使用它进行 C# Web 应用程序开发吗?请给我建议...我是 asp.net Web 应用
我有一个不系统地发生的异常(exception)。 我试图通过在每次迭代中刷新和清理 session 来解决此问题,但没有成功。 [quartzScheduler_Worker-7] ERROR jd
使用 Hibernate 在数据库中存储 IP 地址的最佳类型是什么? 我虽然是 Byte[] 或 String,但有没有更好的方法,或者你用什么? @Column(name = "range_fr
我正在尝试制定一个公式来选择用户个人资料的用户友好名称。它选择名字 + ' ' + 姓氏 如果其中至少有一个不为空且不为空(包含非空白字符),否则选择 短名称 (条件相同),最后,如果 短名称 为空或
在hibernate中,是否可以将鉴别器作为一个实体?例如,如果我将 Department 作为基类,将 AdminDepartment 和 ProcessingDepartment 作为子类。 De
我只想从表中获取一些列值。因此,我已经使用投影来实现这一目标。该代码有效,但我认为它无效。 我的问题是当我使用ProjectionsList并将标准条件列表设置为ArrayList时-Bulletin
你好: 我对 hibernate 缓存缓存的内容感到困惑。 从文档中,我知道 hibernate 中有缓存类型。 一级 :交易级别。 似乎要被 session 持久化的实体被缓存在这里。 二级缓存 :
我遇到了一个情况: save或update hibernate 的目标表中的某些数据 在目标表上有一个触发器,该触发器将在目标表的insert或update操作之前执行 由 hibernate 将此记
我有一个名为 Master_Info_tbl 的表。它是一个查询表: 这是该表的代码: @Entity @Table(name="MASTER_INFO_T") public class Code
我想知道如何在 Hibernate 查询语言中使用日期文字。我在我的 JPA 项目中做了如下操作(作为 Eclipselink 提供者)并且它工作正常。 SELECT m FROM Me m WHER
@Entity public class Troop { @OneToMany(mappedBy="troop") public Set getSoldiers() { ...
我正在尝试使用 hibernate 查询删除表 'user_role' 中的所有行。但每次我都会出错。有人可以帮我吗。 DaoImpl @Override public void deleteAll(
不是将数据库操作分散在四个 (osgi) 包中,而是在那里做略有不同的事情。我想创建一个负责所有持久性问题的(简单的)OSGi 包。我觉得这并不像听起来那么简单,因为“每个包都有独特的类加载器”。 因
这就是我使用生成器的方式: private Integer id; 我看到的行为是: 创建第一个对象 hibernate 分配 id = 1 删除该对象 关闭服务
对象级别的实体和值类型有什么区别。我知道实体将有一个 id 但值不会,但为什么我们需要不同的方法来映射实体与值类型? 这样做是为了让hibernate可以对值类型应用任何优化吗? 最佳答案 一个实体已
我正在使用 HibernateTemplate.findByCriteria 方法进行一些查询。现在我想在标准上创建一些 SQL 限制,比如 criteria.add(Restrictions.sql
所以我有以下代码: Query query = session.createQuery("from Weather"); List list = query.list();
如何使用Hibernate映射具有多个实体的 View ? 问候, 混沌 最佳答案 请参见Hibernate文档中第5.1.3节“类”,紧接在“Id”节之前: There is no differen
据我所知,Hibernate 有两种类型的实现 JPA的实现(2)(@Entity,@Table注解) 扩展到旧的(传统的) hibernate (没有 JPA),使用 HSQL 查询,没有注释 如果
我需要一个将条目存储为键值对的集合(因此我可以通过键查找值),但我需要一个允许多个值使用 hibernate 共享同一个键的集合 最佳答案 一个键具有多个值的映射称为多映射 - 在 Apache 公共
我是一名优秀的程序员,十分优秀!