// repository public interface EmployeeJpaRepo extends JpaRepository<Employee,Integer> { }
// EmployeeJpaRepoTest public class EmployeeJpaRepoTest { private ApplicationContext ctx = null; private EmployeeJpaRepo employeeJpaRepo = null; @Before public void setUp(){ ctx = new ClassPathXmlApplicationContext("bean-new.xml"); employeeJpaRepo = ctx.getBean(EmployeeJpaRepo.class); System.out.println(" setup is running..... "); } @After public void close(){ ctx = null; System.out.println(" close is running..... "); } @Test public void findOne(){ Employee employee = employeeJpaRepo.findOne(10); System.out.println(employee); //这个方法在JpaRepository接口中有出现,我开始觉得会返回一个Employee对象,结果看返回值是一个抽象类 //那么这个geOne方法放在类里面主要做什么用,该怎么用 employeeJpaRepo.getOne(11); System.out.println("id102是否存在"+employeeJpaRepo.exists(102)); } }
在方法上加上
@Transactional
这个注解
我碰到的问题是测试运行报异常:org.hibernate.LazyInitializationException:could not initialize proxy - no Session
查百度使用了在实体类上关闭懒加载的方式:
@Entity @Table(name = "test_employee") @Proxy(lazy = false) public class Employee {