のために動作しません。私は、次のtecnologiesを使用します。はsessionFactory.getCurrentSessionを休止状態()フラッシュ()テスト
- TestNGの(6.9.10)
- 春(4.3.2.RELEASE)
にJavaの8
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
p:dataSource-ref="dataSource" p:hibernateProperties="jdbcProperties">
<property name="packagesToScan" value="my.package"/>
</bean>
とテストクラス例:
@ContextConfiguration(locations = {"classpath:/applicationContext-test.xml",
"classpath:/applicationContext-dao.xml",
"classpath:/applicationContext-orm.xml"})
public class AccountServiceTest extends AbstractTransactionalTestNGSpringContextTests {
@Autowired
private SomeService someService;
@Autowired
private SessionFactory sessionFactory;
@Test
public void updateEntity() {
//given
Long entityId = 1L;
SomeClass expected = someService.get(entityId);
String newPropertyValue = "new value";
//when
someService.changeEntity(entity, newPropertyValue);
// Manual flush is required to avoid false positive in test
sessionFactory.getCurrentSession().flush();
//then
expected = someService.get(entityId);
Assert.assertEquals(expected.getChangedProperty() , newPropertyValue);
}
サービス方法:
@Transactional
@Override
public int changeEntity(entity, newPropertyValue) {
return dao().executeNamedQuery(REFRESH_ACCESS_TIME_QUERY,
CollectionUtils.arrayToMap("id", entity.getId(), "myColumn", newPropertyValue));
}
DAO:
@Override
public int executeNamedQuery(final String query, final Map<String, Object> parameters) {
Query queryObject = sessionFactory.getCurrentSession().getNamedQuery(query);
if (parameters != null) {
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
NamedQueryUtils.applyNamedParameterToQuery(queryObject, entry.getKey(), entry.getValue());
}
}
return queryObject.executeUpdate();
}
私の.xmlでSessionFactoryの設定があります。 しかし、ここで説明したように私のエンティティプロパティは、)(フラッシュ後change @Autowire SessionFactory with @PersistenceContext EntityManagerを
を変更していない、私はflush()
にEntityManager
を使用する必要があります - しかし、私はこれを行うことはできません - 私はEntityManager
にsessionFactory
を変換することができず、私は自分のアプリケーションにEntityManager
を作成する必要はありません。.xml設定ファイルなどを変更する必要があるためです。
この問題の別の解決方法はありますか?
何が問題なのですか? 'EntityManager'や' SessionFactory'を使用しても違いはありません。あなたのポストにあなたのサービスとDAOを追加してください。 –
私にとって、 'EntityManager'は' Session'と共通していて、 'SessionFactory'とはもっと共通しています。 – Antoniossss
正しい。しかし、単純な休止状態またはJPA抽象化とそれらの1つをフラッシュすることを使用することは重要ではありません。したがって、何が動作していないか、より多くのコードについての追加情報の要求。 –