java - "Could not initialize proxy - no Session" -
i'm getting following error message when trying run junit test via spring, jpa , java:
"could not initialize proxy - no session"
the tests domain class (let's call firstdomainclass). firstdomainclass class has number of fields, including 1 linked through domain class in many-to-one relationship:
@manytoone(fetch = fetchtype.eager) @joincolumn(name = "second_domain_class") private seconddomainclass seconddomainclass;
i found had use fetchtype.eager because otherwise seconddomainclass object not populated when retrieved instance of firstdomainclass.
the dao implementation class firstdomainclass has each method prefixed @transactional annotation:
@transactional(propagation = propagation.required, readonly = false) public void store(firstdomainclass firstdomainclass) { entitymanager.persist(firstdomainclass); }
i use spring wire together, including bean definition of dao implementation @autowired junit class. test class creates data in database via @before method , clears out afterwards @after method.
the class prefixed with
@runwith(springjunit4classrunner.class) @transactionconfiguration(defaultrollback = false) @contextconfiguration(locations = { ... list of config files... })
... , test looks this:
@test public void teststore() { seconddomainclass second = seconddomainclassdao.get(... object created in @before method... ); firstdomainclass first = new firstdomainclass(); first.setseconddomainclass(second); ... other setting... firstdomainclassdao.store(first); firstdomainclass newfirst = firstdomainclassdao.get(... criteria retrieve object created store()... ); ... assertions here... }
when test runs part of automated build process, fails message detailed above. stacktrace identifies error occurs when primary key of seconddomainclass object accessed.
versions: java 1.7 / junit 4.10 / jpa 2 / spring 3.2.2 / hibernate 4.1.7
many in advance assistance.
Comments
Post a Comment