我在SO上经历了很多类似的问题,通常答案是只将#include "myclass.h"
MyClass::MyClass(QObject * parent) : QObject{parent}
{
connect(&timer, SIGNAL(timeout()), SLOT(on_timeout()));
}
void MyClass::timerStart()
{
timer.start(1000);
}
void MyClass::on_timeout()
{
msg.setText("updated");
msg.show();
}
添加到类和方法中。但这对我不起作用,因此我假设我做错了什么?
MyRepository:
@Transactional
AbstractRepository:
@Repository
public class MyRepository extends AbstractRepository<MyEntity> implements org.springframework.data.repository.Repository<MyEntity, Long> {
@PersistenceContext
private EntityManager em;
public MyRepository() {
super(MyEnitiy.class);
}
@Override
public EntityManager getEntityManager() {
return em;
}
@SuppressWarnings("unchecked")
@Transactional
public List<MyEnitiy> getAllFor(Integer id) {
return getEntityManager().createQuery("SELECT k FROM MyEntity k WHERE k.otherid = :otherid ORDER BY k.something", MyEntity.class)
.setParameter("otherid", id).getResultList();
}
}
我的Sping网络控制器(代码段):
@Transactional(readOnly = true)
public abstract class AbstractRepository<T> {
...
@Modifying
@Transactional
public void edit(T entity) {
getEntityManager().merge(entity);
}
...
}
更新:
spring-database.xml(使用池试图尝试修复Tomcat内存泄漏):
@Controller
public class MyController {
@Autowired
private MyRepository myRepository;
@RequestMapping(value = {"/here/there"}, method = RequestMethod.GET)
@ResponseBody
@Transactional
public String hereAndTherePage(@RequestParam(value = "id", required = true) Integer id,
HttpServletRequest request, HttpSession session) {
List<MyEntity> myEntities = myRepository.getAllFor(id);
for(MyEntity myEntity : myEntities) {
...
myEntity.setSomeValue(myEntity.getSomeValue() + 1);
...
myRepository.edit(myEntity);
调用<bean class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.example.*" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="generateDdl" value="true" />
</bean>
</property>
</bean>
<bean id="poolProperties" class="org.apache.tomcat.jdbc.pool.PoolProperties">
<property name="url" value="jdbc:mysql://localhost:3306/bfwinkel?noAccessToProcedureBodies=true"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="testOnBorrow" value="true"/>
<property name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="10"/>
<property name="username" value="d[-.-]b"/>
<property name="password" value="~!~"/>
</bean>
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="poolProperties" ref="poolProperties"/>
</bean>
时会抛出异常:
getEntityManager().merge(entity);
答案 0 :(得分:1)
将以下行添加到spring-database.xml
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
答案 1 :(得分:0)
此错误意味着,当您尝试合并实体时,它们没有可用的持久性上下文。
您有三个选项可以按优先级解决此问题。
1-您是否已将@EnableTransactionManagement添加到您的配置中?
2-如果你有一个persistence.xml文件,你应该为@PersistenceContext添加一个unitname属性。
3-使用扩展持久性上下文而不是事务范围的持久性上下文。
我希望我的回答会对你有帮助。
祝你好运