【发布时间】:2014-09-22 10:31:59
【问题描述】:
我需要在@Test 方法中使用jackson.map.ObjectMapper 映射新创建的对象:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
@WebAppConfiguration
public class TestProductServiceController implements ApplicationContextAware {
@Autowired
private ProductService cService;
@Autowired
private ProductPropertyService crService;
@Autowired
private SessionFactory sessionFactory;
String json;
ProductProperty productProperty;
Product product;
ObjectMapper mapper = new ObjectMapper();
@Before
public void setUp() throws IOException {
RestAssuredMockMvc.standaloneSetup(new ProductServiceController());
cService = (ProductService) context.getBean("productService");
}
@Test
public void testAddProduct() throws IOException {
productProperty = crService.findProductProperty(1);
product = new Product();
product.setProduct(1);
product.setDateTimeStart(...);
product.setDateTimeEnd(...);
product.setFk_product_property(productProperty);
product.setName("Test 1");
product.setCancelled("F");
Hibernate.initialize(product);
Hibernate.initialize(product.getMapped_product_part_collection());
Hibernate.initialize(productProperty);
Hibernate.initialize(productProperty.getMapped_fk_product_property());
String jsonStr = mapper.writeValueAsString(product);
}
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static ApplicationContext getContext() {
return context;
}
}
错误:
JsonMappingException failed to lazily initialize a collection of role: model.ProductProperty.mapped_fk_product_property,
could not initialize proxy - no Session
(through reference chain: model.Product["fk_product_property"]->model.ProductProperty["mapped_fk_product_property"])
@Table(name = "product")
@Entity
public class Product implements Serializable {
@Id
@Column(name = "product")
private int product;
@ManyToOne
@JoinColumn(name = "fk_product_property", referencedColumnName = "product_property", nullable = false)
private ProductProperty fk_product_property;
}
@Entity
@Table(name = "product_property")
public class ProductProperty implements Serializable {
@Id
@GeneratedValue
@Column(name = "product_property")
private int product_property;
@OneToMany(mappedBy = "fk_product_property", cascade={CascadeType.ALL})
private Collection<Product> mapped_fk_product_property;
}
我认为将fetch = FetchType.EAGER 应用于@OneToMany(mappedBy = "fk_product_property", cascade={CascadeType.ALL})
private Collection<Product> mapped_fk_product_property; 是不正确的。怎么了?当前@TEST方法的解决方案是什么?
更新
我添加到@TEST 但出现错误:
Hibernate.initialize(product);
Hibernate.initialize(product.getMapped_product_part_collection());
Hibernate.initialize(productProperty.getMapped_fk_product_property()); // HibernateException: collection is not associated with any session
更新 2
我在Hibernate.initialize(productProperty.getMapped_fk_product_property()); 的调试器中有以下结构:
-product={...}
-name=...
-....
-fk_product_property = {....}
-name=...
-....
-mapped_fk_product_property={org.hibernate.collection.internal.PersistentBag@2222} unable to evaluate expression Method threw 'org.hibernate.LazyInitializationException' exception.
【问题讨论】:
-
不需要
fetch = FetchType.EAGER,而是在String jsonStr = mapper.writeValueAsString(product);之前尝试使用Hibernate.intialize(product);Hibernate.intialize(product.getmapped_fk_product_property()); -
如果它解决了您的问题,请告诉我
-
它给了
org.hibernate.HibernateException: collection is not associated with any session -
productProperty.getMapped_fk_product_property()无法初始化,因为它位于 DAO,而不是服务中。 -
无论它位于何处,在包装成 json 之前先尝试初始化它
标签: java spring hibernate jackson