JsonMappingException-无法映射有效对象



我需要在@Test method:中使用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.

ProductPropertyProduct之间的关系是OneToMany,关联的所有者是Product,因此当您尝试获取ProductProperty元素时,如果获取策略不是EAGER ,则不会加载Product

现在在这行代码中:

productProperty = crService.findProductProperty(1);

您正试图从服务类crService中获取ProductProperty元素。这意味着获取逻辑存在于拥有Hibernate的Session的DAO层中此外,集合元素mapped_fk_product_property将被设置为代理对象,因为在这种情况下获取策略是LAZY

现在在这行代码中:

Hibernate.initialize(productProperty.getMapped_fk_product_property());

您正试图使用Hibernate.initialize(Object)初始化集合元素,这行代码的问题是您正试图在DAO层本身关闭会话后调用initialize。所以例外情况是:

could not initialize proxy - no Session

要解决这个问题,请在DAO层中添加这行代码,您正试图在其中获取ProductProperty:

Hibernate.initialize(productProperty.getMapped_fk_product_property());

此外,根据Java编码指南,不建议在变量声明中使用下划线"_",因此在代码中尽量避免使用下划线。

最新更新