我有一个使用Hibernate的Spring MVC webapp,我的问题是em.merge
在通话后没有响应。
这是我的控制器
@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";
}
使用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");
}
所以我试着解释一下问题:
从我的控制器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=?
这就是全部!为什么呢
System.out.println("after");
和System.out.println("after merge; in controller");
没有执行?调用em.merge
后没有任何事情发生。
MySQL表中的数据仍未更新。 但是,如果我添加
department.setId(null);
之前,就像那样
@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");
}
所有工作都在db中创建了新实例,但我希望更新当前。 问题是什么? 更新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>
更新3 添加我的LOG4J登录期间的em.merge()事务,我在那里创建了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
答案 0 :(得分:1)
我不确定,但我认为这个问题与'分离'&#39;对象。 请看这个链接http://www.objectdb.com/java/jpa/persistence/detach
答案 1 :(得分:0)
我正在使用JpaRepository<T, ID extends Serializable>
接口和Spring-Data与Hibernate,我必须做的是在我创建对象并保存和刷新的方法上添加@Transactional
。
答案 2 :(得分:0)
我遇到了完全相同的问题。问题出在不同的地方。
我在 servlet.xml 文件中输入了
答案 3 :(得分:-4)
为什么不使用entity.save(department)?