gpt4 book ai didi

java - org.hibernate.StaleStateException : Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

转载 作者:行者123 更新时间:2023-12-01 12:36:48 30 4
gpt4 key购买 nike

我正在 STS 中使用 JUnit 编写单元测试用例。这些类是

测试配置.java

 @Configuration
@ComponentScan("com.kgfsl.collections.core")
public class TestConfiguration {

@Bean
@Inject
public org.hibernate.SessionFactory sessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder sessionBuilder = new
LocalSessionFactoryBuilder(dataSource);
sessionBuilder.scanPackages("com.kgfsl.collections.core.models");
sessionBuilder.addProperties(getHibernateProperties());
return sessionBuilder.buildSessionFactory();
}

private static Properties getHibernateProperties() {
final org.hibernate.cfg.Configuration configuration = new
org.hibernate.cfg.Configuration();
configuration.setProperty(HBM2DDL_AUTO, "create");
configuration.setProperty(DIALECT, "org.hibernate.dialect.MySQL5Dialect");

// Test Default settings
configuration.setProperty(CURRENT_SESSION_CONTEXT_CLASS, "thread");
configuration.setProperty(USE_GET_GENERATED_KEYS, "true");
configuration.setProperty(USE_REFLECTION_OPTIMIZER, "true");
configuration.setProperty(ORDER_UPDATES, "true");
configuration.setProperty(ORDER_INSERTS, "true");
configuration.setProperty(USE_NEW_ID_GENERATOR_MAPPINGS, "true");
return configuration.getProperties();
}


}

BaseRepositoryTest.java

@RunWith (SpringJUnit4ClassRunner. class)
@ContextConfiguration(classes = {UserManagementTestConfiguration.class})
public abstract class BaseRepositoryTest extends
AbstractJUnit4SpringContextTests {

@Inject
SessionFactory sessionFactory;

private Transaction transaction;

public Session getSession() {
Session session;
try {
session = sessionFactory.getCurrentSession();
} catch (SessionException se) {
session = sessionFactory.openSession();
}
return session;
}

@Before
public void setUp() throws Exception {
transaction = getSession().beginTransaction();
}

@After
public void tearDown() throws Exception {
if (transaction != null) {
transaction.rollback();
}
getSession().close();
}

@Before
public void baseSetUp() {
MockitoAnnotations.initMocks(this);
}
}

UserRoleDaoTest.java

  public class UserRoleDaoTest extends BaseRepositoryTest
{
@Autowired
private UserRoleDao userroleDao;

@Test
public void shouldCreateRole()
{
UserRole userRole=new UserRoleBuilder().userroleBuilder(10,"Md Aslam",
"N","1").build();

UserRole userroleCreated=userroleDao.persist(userRole);
UserRole userroleGet = userroleDao.findById(userroleCreated.getRoleId());
Assert.assertThat(userroleGet.getRoleName(), Is.is("Md Aslam"));
Assert.assertThat(userroleGet.getRoleId(), Is.is(10));

UserRole userroleCreate=userroleDao.persist(userRole);
Assert.assertNotNull(userRole.getRoleId());
}

@Test
public void shouldGetRoleById()
{
UserRole userRole=new UserRoleBuilder().userroleBuilder(10,"Md Aslam",
"N","1").build();
UserRole userroleCreated=userroleDao.persist(userRole);
UserRole userroleGet = userroleDao.findById(userroleCreated.getRoleId());
assertNotNull(userroleGet);
Assert.assertThat(userroleGet.getRoleId(), Is.is(10));
}

@Test
public void shouldUpdateAccount()
{
UserRole userRole=new UserRoleBuilder().userroleBuilder(10,"aslam",
"N","1").build();
UserRole userroleCreated=userroleDao.persist(userRole);

UserRole userroleGet = userroleDao.findById(userroleCreated.getRoleId());
Assert.assertEquals(userRole.getRoleId(), userroleGet.getRoleId());
UserRole userRoleForUpdate=new
UserRoleBuilder().userroleBuilder(userroleCreated.getRoleId(),"Md Aslam",
"Y","1").build();
userroleDao.update(userRoleForUpdate);
}

@Test
public void shouldGetRoleByName()
{

UserRole userRole=new UserRoleBuilder().userroleBuilder(10,"aslam",
"N","1").build();
UserRole userroleCreated=userroleDao.persist(userRole);

UserRole userrolegetbyname=userroleDao.findByRoleName(userRole.getRoleName());
assertNotNull(userrolegetbyname);

}

}

当我运行这个类时,它显示类似的错误

    shouldGetRoleByName:org.hibernate.StaleStateException: Batch update returned  
unexpected row count from Update [0]; actual row count: 0; expected: 1

shouldGetRoleByName 的实际方法是

   public UserRole findByRoleName(String roleName) {
UserRole userPermission = (UserRole)
criteria().setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.add(eq("roleName", roleName.trim().toLowerCase())).uniqueResult();
return userPermission;
}

测试用例中除shouldGetRoleByName()这个方法外的所有方法都通过了。并且我尝试了很多方法,但无法找到解决方案,如果有人知道解决方案,请帮助我。

最佳答案

Hibernate 似乎将所有更改保留在内存中,并且仅当在“shouldGetRoleByName”方法中触发查询时才执行刷新(将更改发送到数据库)。此时“shouldUpdateAccount”中调用的更新被发送到数据库并失败(影响零条记录)。

尝试在“shouldUpdateAccount”方法末尾调用“flush”并检查错误是否移至此处。如果是这样,您可以研究您的代码以更好地了解其行为。请注意, session 中执行的所有实体修改仅在特定时间发送到数据库(例如:当您提交事务或执行查询时)。

关于java - org.hibernate.StaleStateException : Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25506098/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com