Java Hibernate EntityManager.merge() 不更新数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24922294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate EntityManager.merge() does not update database
提问by MeetJoeBlack
I have a Spring MVC webapp using Hibernate, and my problem is that em.merge
doesn't respond after the call.
我有一个使用 Hibernate 的 Spring MVC webapp,我的问题是em.merge
调用后没有响应。
Here is my Controller
这是我的控制器
@RequestMapping(value = "/updDep", method = RequestMethod.PUT)
@ResponseBody
public String updDeps(@RequestBody Department department ) {
departmentService.addDepartment(department);
System.out.println("after merge; in controller");
return "Success";
}
And Department Service class with addDepartment() method:
以及带有 addDepartment() 方法的 Department Service 类:
@Repository
public class DepartmentServiceImpl implements DepartmentService {
@PersistenceContext(unitName = "MyPersistenceUnit")
private EntityManager entityManager;
....
//getter/setter entityManager....
...
@Override
@Transactional
public void addDepartment(Department department) {
System.out.println(department.getName() + " merge: " + department.getId());
getEntityManager().merge(department);
getEntityManager().flush();
System.out.println("after");
}
So i try to explain problem:
After addDepartment(Department department)
method is called from my Controllers updDeps(..)
method.
I see this in my servers log:
所以我尝试解释问题:addDepartment(Department department)
从我的控制器updDeps(..)
方法调用方法之后。我在我的服务器日志中看到了这一点:
SECURITY23 merge: 3
Hibernate:
select
department0_.id as
department0_.name a
from
departments departm
where
department0_.id=?
Hibernate:
update
departments
set
name=?
where
id=?
And that's all! Why didSystem.out.println("after");
and System.out.println("after merge; in controller");
not execute? After em.merge
was called nothing happened.
就这样!为什么System.out.println("after");
并System.out.println("after merge; in controller");
没有执行?之后em.merge
叫什么都没有发生。
And data in MySQL table still not updated. But if I add
并且 MySQL 表中的数据仍未更新。但如果我添加
department.setId(null);
before, like that
以前那样
@Transactional
public void addDepartment(Department department) {
department.setId(null);
System.out.println(department.getName() + " merge: " + department.getId());
getEntityManager().merge(department);
getEntityManager().flush();
System.out.println("after");
}
All works and it created new instance in db, but I want update current.
What's the problem?
UPDATE 1Project Structure:
一切正常,它在数据库中创建了新实例,但我想更新当前。有什么问题?
更新 1项目结构:
PERSISTENCE.XML
持久化文件
<persistence-unit name="MyPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/testbd"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<!--<property name="hibernate.hbm2ddl.import_files" value= "import.sql"/>-->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
SPRING CONFIG
弹簧配置
<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="epam.rest" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="MyPersistenceUnit"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>
UPDATE 3ADD MY LOG4J log in period of em.merge() transaction, where I founded ROLLBACK log but I dont know whats the problem?:
UPDATE 3ADD MY LOG4J log in period of em.merge() transaction,我在那里创建了 ROLLBACK 日志,但我不知道是什么问题?:
08:54:28,291 DEBUG LogicalConnectionImpl:218 - Obtained JDBC connection
08:54:28,291 DEBUG JdbcTransaction:69 - initial autocommit status: true
08:54:28,292 DEBUG JdbcTransaction:71 - disabling autocommit
08:54:28,301 TRACE IdentifierValue:139 - ID unsaved-value: null
08:54:28,302 TRACE AbstractSaveEventListener:504 - Detached instance of: epam.rest.entity.Department
08:54:28,304 TRACE DefaultMergeEventListener:245 - Merging detached instance
08:54:28,310 TRACE DefaultLoadEventListener:240 - Loading entity: [epam.rest.entity.Department#16]
08:54:28,311 TRACE DefaultLoadEventListener:403 - Attempting to resolve: [epam.rest.entity.Department#16]
08:54:28,311 TRACE DefaultLoadEventListener:427 - Object not resolved in any cache: [epam.rest.entity.Department#16]
08:54:28,312 TRACE AbstractEntityPersister:3923 - Fetching entity: [epam.rest.entity.Department#16]
08:54:28,312 DEBUG Loader:2109 - Loading entity: [epam.rest.entity.Department#16]
08:54:28,312 DEBUG SQL:104 -
select
department0_.id as id1_0_0_,
department0_.name as name2_0_0_
from
departments department0_
where
department0_.id=?
08:54:28,314 TRACE JdbcCoordinatorImpl:319 - Registering statement [com.mysql.jdbc.JDBC4PreparedStatement@6c31a161: select department0_.id as id1_0_0_, department0_.name as name2_0_0_ from departments department0_ where department0_.id=** NOT SPECIFIED **]
08:54:28,314 TRACE JdbcCoordinatorImpl:329 - Registering last query statement [com.mysql.jdbc.JDBC4PreparedStatement@6c31a161: select department0_.id as id1_0_0_, department0_.name as name2_0_0_ from departments department0_ where department0_.id=** NOT SPECIFIED **]
08:54:28,320 TRACE BasicBinder:84 - binding parameter [1] as [BIGINT] - 16
08:54:28,320 TRACE Loader:1909 - Bound [2] parameters total
08:54:28,321 TRACE JdbcCoordinatorImpl:374 - Registering result set [com.mysql.jdbc.JDBC4ResultSet@4e1d82e2]
08:54:28,322 TRACE Loader:937 - Processing result set
08:54:28,322 DEBUG Loader:942 - Result set row: 0
08:54:28,323 DEBUG Loader:1476 - Result row: EntityKey[epam.rest.entity.Department#16]
08:54:28,323 TRACE Loader:1652 - Initializing object from ResultSet: [epam.rest.entity.Department#16]
08:54:28,333 TRACE AbstractEntityPersister:2844 - Hydrating entity: [epam.rest.entity.Department#16]
08:54:28,334 TRACE BasicExtractor:74 - Found [REWQQ] as column [name2_0_0_]
08:54:28,337 TRACE Loader:962 - Done processing result set (1 rows)
08:54:28,338 TRACE Loader:1106 - Total objects hydrated: 1
08:54:28,338 DEBUG TwoPhaseLoad:158 - Resolving associations for [epam.rest.entity.Department#16]
08:54:28,343 DEBUG TwoPhaseLoad:277 - Done materializing entity [epam.rest.entity.Department#16]
08:54:28,344 TRACE JdbcCoordinatorImpl:358 - Releasing statement [com.mysql.jdbc.JDBC4PreparedStatement@6c31a161: select department0_.id as id1_0_0_, department0_.name as name2_0_0_ from departments department0_ where department0_.id=16]
08:54:28,344 TRACE JdbcCoordinatorImpl:519 - Closing result set [com.mysql.jdbc.JDBC4ResultSet@4e1d82e2]
08:54:28,345 TRACE JdbcCoordinatorImpl:476 - Closing prepared statement [com.mysql.jdbc.JDBC4PreparedStatement@6c31a161: select department0_.id as id1_0_0_, department0_.name as name2_0_0_ from departments department0_ where department0_.id=16]
08:54:28,345 TRACE JdbcCoordinatorImpl:249 - Starting after statement execution processing [ON_CLOSE]
08:54:28,345 TRACE StatefulPersistenceContext:1022 - Initializing non-lazy collections
08:54:28,346 DEBUG Loader:2133 - Done entity load
08:54:28,352 TRACE UnresolvedEntityInsertActions:121 - No entity insert actions have non-nullable, transient entity dependencies.
08:54:28,353 DEBUG AbstractTransactionImpl:173 - committing
08:54:28,353 TRACE SessionImpl:403 - Automatically flushing session
08:54:28,353 TRACE AbstractFlushingEventListener:82 - Flushing session
08:54:28,355 DEBUG AbstractFlushingEventListener:144 - Processing flush-time cascades
08:54:28,358 TRACE Cascade:151 - Processing cascade ACTION_PERSIST_ON_FLUSH for: epam.rest.entity.Department
08:54:28,359 TRACE Cascade:188 - Done processing cascade ACTION_PERSIST_ON_FLUSH for: epam.rest.entity.Department
08:54:28,359 DEBUG AbstractFlushingEventListener:185 - Dirty checking collections
08:54:28,360 TRACE AbstractFlushingEventListener:200 - Flushing entities and processing referenced collections
08:54:28,365 TRACE AbstractEntityPersister:4097 - epam.rest.entity.Department.name is dirty
08:54:28,366 TRACE DefaultFlushEntityEventListener:642 - Found dirty properties [[epam.rest.entity.Department#16]] : [Ljava.lang.String;@3aa8208a
08:54:28,366 TRACE DefaultFlushEntityEventListener:281 - Updating entity: [epam.rest.entity.Department#16]
08:54:28,371 TRACE AbstractFlushingEventListener:242 - Processing unreferenced collections
08:54:28,371 TRACE AbstractFlushingEventListener:254 - Scheduling collection removes/(re)creates/updates
08:54:28,372 DEBUG AbstractFlushingEventListener:118 - Flushed: 0 insertions, 1 updates, 0 deletions to 1 objects
08:54:28,372 DEBUG AbstractFlushingEventListener:125 - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
08:54:28,373 DEBUG EntityPrinter:114 - Listing entities:
08:54:28,373 DEBUG EntityPrinter:121 - epam.rest.entity.Department{id=16, name=REWQQ112}
08:54:28,373 TRACE AbstractFlushingEventListener:327 - Executing flush
08:54:28,383 TRACE AbstractEntityPersister:3166 - Updating entity: [epam.rest.entity.Department#16]
08:54:28,384 TRACE AbstractServiceRegistryImpl:146 - Initializing service [role=org.hibernate.engine.jdbc.batch.spi.BatchBuilder]
08:54:28,391 TRACE AbstractServiceRegistryImpl:146 - Initializing service [role=org.hibernate.service.jmx.spi.JmxService]
08:54:28,393 TRACE BatchBuilderImpl:68 - Building batch [size=1]
08:54:28,395 DEBUG SQL:104 -
update
departments
set
name=?
where
id=?
08:54:28,397 TRACE JdbcCoordinatorImpl:319 - Registering statement [com.mysql.jdbc.JDBC4PreparedStatement@15f07169: update departments set name=** NOT SPECIFIED ** where id=** NOT SPECIFIED **]
08:54:28,397 TRACE AbstractEntityPersister:2780 - Dehydrating entity: [epam.rest.entity.Department#16]
08:54:28,399 TRACE BasicBinder:84 - binding parameter [1] as [VARCHAR] - REWQQ112
08:54:28,400 TRACE BasicBinder:84 - binding parameter [2] as [BIGINT] - 16
08:54:28,436 TRACE JdbcCoordinatorImpl:358 - Releasing statement [com.mysql.jdbc.JDBC4PreparedStatement@15f07169: update departments set name='REWQQ112' where id=16]
08:54:28,437 TRACE JdbcCoordinatorImpl:476 - Closing prepared statement [com.mysql.jdbc.JDBC4PreparedStatement@15f07169: update departments set name='REWQQ112' where id=16]
08:54:28,437 TRACE JdbcCoordinatorImpl:249 - Starting after statement execution processing [ON_CLOSE]
08:54:28,439 TRACE JdbcCoordinatorImpl:249 - Starting after statement execution processing [ON_CLOSE]
08:54:28,440 DEBUG AbstractTransactionImpl:203 - rolling back
08:54:28,480 DEBUG JdbcTransaction:164 - rolled JDBC Connection
08:54:28,481 DEBUG JdbcTransaction:126 - re-enabling autocommit
08:54:28,482 TRACE TransactionCoordinatorImpl:136 - after transaction completion
08:54:28,486 TRACE SessionImpl:624 - after transaction completion
08:54:28,488 TRACE SessionImpl:342 - Closing session
08:54:28,489 TRACE JdbcCoordinatorImpl:171 - Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl@26d77937]
08:54:28,490 DEBUG JdbcCoordinatorImpl:173 - HHH000420: Closing un-released batch
08:54:28,490 TRACE LogicalConnectionImpl:164 - Closing logical connection
08:54:28,491 DEBUG LogicalConnectionImpl:232 - Releasing JDBC connection
08:54:28,492 TRACE DriverManagerConnectionProviderImpl:233 - Returning connection to pool, pool size: 1
08:54:28,493 DEBUG LogicalConnectionImpl:250 - Released JDBC connection
08:54:28,493 TRACE LogicalConnectionImpl:176 - Logical connection closed
采纳答案by zhurlik
I am not sure but I think that's problem with 'detached' objects. Look at this link http://www.objectdb.com/java/jpa/persistence/detach
我不确定,但我认为这是“分离”对象的问题。看看这个链接http://www.objectdb.com/java/jpa/persistence/detach
回答by user3073234
Why not use entity.save(department)?
为什么不使用 entity.save(department)?
回答by EpicPandaForce
I was using JpaRepository<T, ID extends Serializable>
interface and Spring-Data with Hibernate, and what I had to do is I added @Transactional
on the method where I create the object and save and flush.
我在JpaRepository<T, ID extends Serializable>
Hibernate 中使用了接口和 Spring-Data,我必须做的是@Transactional
在创建对象并保存和刷新的方法中添加。