Hi all,
I've searched through the forum but could not find a solution to my problem ...
I have created a very simple example in order to see how to test my Reposity implemented using JPA+Hibernate with Spring.
But when i run my test and do some insertion in (em.persist()), and then i call immediately a request to get all elements - using a simple query ("from User"), it desperately returns an empty list, always. I'm stuck trying to figure out what's going on... i double checked my context configuration, and it seems ok.
Any help is immensely appreciated ?
Here are the codes (and i also attached a maven project)
Code:@Entity public class User { private String firstName; private String lastName; Long id; User() { } public User(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public Long getId() { return id; } }Code:import java.util.List; public interface UserRepository { List<User> findAllUser(); User findUserById(Long id); void store(User user); void remove(User user); }Code:@Repository public class UserRepositoryJpa implements UserRepository { @PersistenceContext EntityManager em; @Override @SuppressWarnings("unchecked") public List<User> findAllUser() { return em.createQuery("select c from User c").getResultList(); } @Override public User findUserById(Long id) { return em.find(User.class, id); } @Override public void store(User newUser) { em.persist(newUser); } @Override public void remove(User user) { em.remove(user); } }... and finally my test casesHTML Code:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="example" /> <context:annotation-config /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:oms" p:username="SA" p:password="" /> <!-- JPA EntityManagerFactory --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="dataSource"> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:database="HSQL"> <property name="generateDdl" value="true" /> <property name="showSql" value="true" /> </bean> </property> <property name="persistenceXmlLocation" value="classpath:META-INF/jpa-persistence.xml" /> </bean> <!-- Transaction --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven /> </beans>
Code:import static org.junit.Assert.*; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath*:spring/applicationContext.xml" }) @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false) @Transactional public class UserRepositoryJpaTest { @Autowired UserRepository userRepo; @Test public void testCreateUser() { User u = new User("Jim", "Doe"); userRepo.store(u); User user = userRepo.findUserById(u.getId()); assertNotNull(user); } @Test public void testFindAll() { // Create many users for (int i = 0; i < 10; ++i) { User u = new User("Jim" + i, "Doe" + i); userRepo.store(u); } // Check an element exists assertNotNull(userRepo.findUserById(4L)); // Check that we have ten elements <==== FAILURES HERE! assertEquals(10, userRepo.findAllUser().size()); } }


Reply With Quote