JsonMappingException - 无法映射有效对象

时间:2014-09-22 10:31:59

标签: java spring hibernate jackson

我需要在@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.

1 个答案:

答案 0 :(得分:1)

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

,则不会加载EAGER

现在在这行代码中:

productProperty = crService.findProductProperty(1);

您正试图从服务类ProductProperty获取crService个元素。这意味着获取逻辑存在于DAO层中,您正在使用Hibernate&#39; Session 此外,集合元素mapped_fk_product_property将设置为代理对象,因为在这种情况下,提取策略为LAZY

现在在这行代码中:

Hibernate.initialize(productProperty.getMapped_fk_product_property());

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

could not initialize proxy - no Session

要解决此问题,请在您尝试获取ProductProperty的DAO图层中添加以下代码行:

Hibernate.initialize(productProperty.getMapped_fk_product_property());

另外,根据Java编码指南,不建议使用下划线&#34; _&#34;对于变量声明,所以尽量避免在代码中使用它们。