gpt4 book ai didi

java - org.hibernate.service.UnknownServiceException : Unknown service requested

转载 作者:搜寻专家 更新时间:2023-10-31 08:26:03 24 4
gpt4 key购买 nike

我正在为我的 AbstractHibernateRepository 保存方法编写单元测试。我正在使用 spring 测试运行器,但在运行时出现以下异常:

org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:201)

我的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/spring-hibernate.xml")
public class AbstractHibernateRepoTest extends AbstractHibernateRepo<Video> {
@Autowired private SessionFactory sessionFactory;
private Video video;

public AbstractHibernateRepoTest()
{
super(Video.class);
}

@Before
public void setUp ()
{
video = new Video();
video.setId("xyz");
video.setName("Video Name");
video.setSrc("Source");
video.setThumbnail("Thumbnail");
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(video) ;
session.close();
}

@Test
public void testSaveMethod ()
{
video.setId("asa");
String id = (String) save(video);
Assert.assertEquals(video.getId(), id);
}

@After
public void breakDown ()
{
sessionFactory.close();
}
}

存储库:

    @Autowired private SessionFactory sessionFactory;
private final Class<T> clazz;

public AbstractHibernateRepo(Class<T> clazz)
{
this.clazz = clazz;
}

@SuppressWarnings("unchecked")
@Transactional(rollbackFor = HibernateException.class)
@Override
public T findById(Serializable id)
{
if (id == null)
throw new NullPointerException();

return (T) getSessionFactory().getCurrentSession().get(getClazz(), id);
}

@Override
@Transactional(rollbackFor = HibernateException.class)
public Serializable save(T entity)
{
if (entity == null)
throw new NullPointerException();

return getSessionFactory().getCurrentSession().save(entity);
}

@Override
@Transactional(rollbackFor = HibernateException.class)
public void delete(T entity)
{
if (entity == null)
throw new NullPointerException();

getSessionFactory().getCurrentSession().delete(entity);
}

@Override
@Transactional(rollbackFor = HibernateException.class)
public void update(T entity)
{
if (entity == null)
throw new NullPointerException();

getSessionFactory().getCurrentSession().update(entity);
}
}

Spring 配置:

<tx:annotation-driven transaction-manager="transactionManager"/> 

<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE"/>
<property name="username" value="someuser"/>
<property name="password" value="somepassword"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.package.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
</props>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>

是什么导致了这个问题,我该如何解决?

最佳答案

我可能完全偏离了这里,但对我来说这似乎是一个 session 处理异常。在 @Before 中打开和关闭 session ,然后在 save() 中获取当前 session ,这可能是您刚刚关闭的 session ,从而导致异常。如果您不在 @Before 中关闭它,请尝试它是否有效(我知道这不是解决方案,只是为了测试理论)。您也可以尝试在存储库中打开一个新 session 而不是获取当前 session (也不是解决方案)。与我们的工作测试设置相比,我看到的唯一区别是,在 @Before 中,我们还调用了我们的存储库方法,标记为 @Transactional,而不是直接创建 session 。

关于java - org.hibernate.service.UnknownServiceException : Unknown service requested,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26469263/

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