In my Spring app i have Two entity:
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
....
@OneToOne
@Cascade({CascadeType.ALL})
@JoinColumn(name="page_id")
private Page page;
@OneToMany(fetch=FetchType.LAZY, mappedBy="category")
private List<Shop> shop;
..getters and setters
}
And
@Entity
public class Shop {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
....
@OneToOne(mappedBy="shop")
private Settings settings;
@ManyToOne
@Cascade({CascadeType.SAVE_UPDATE})
@JoinColumn(name = "category_id")
private Category category;
@OneToOne
@Cascade({CascadeType.ALL})
@JoinColumn(name = "page_id")
private Page page;
...getters and setters
}
And in my test jUnit i added some category and some shop with this category, when i want to access to list of shops current category i have a error java.lang.NullPointerException ....
My test:
@ContextConfiguration(locations = {
"classpath:security-context.xml",
"classpath:dao-context.xml"
})
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
@Transactional
public class CategoryTest {
Shop shop1 = new Shop(....);
Shop shop1 = new Shop(....);
Category category1 = new Category(....);
Category category2 = new Category(....);
@Test
public void someTest(){
shop1.setCategory(category1);
shop2.setCategory(category2);
shopService.add(shop1);
shopService.add(shop2);
assertEquals(2, shopService.getAllShop().size());
assertEquals(2, categoryService.getAllShop().size());
//IN THIS LINE I HAVE A ERROR
assertEquals(1, categoryService.getCategory(category1.getId()).getShop().size());
}
}
For access to lazy atribute i added to my web.xml filtr: org.springframework.orm.hibernate3.support.OpenSessionInViewFilter This working properly in front app not in jUnit test.
Which point I make an error?