Hibernate自联接查询不会解析为json

时间:2017-07-10 03:49:37

标签: java json hibernate jpa gson

我正在使用Hibernate将数据保存到HSQLDB文件中 我创建了一个类似于测试人员的方法,并且#34;填充"和"查询"我存储的数据。

但是当我查询数据时,在调试模式下,它就像一个无限数组:

产品(产品实例1)
    - > ParentProduct(产品的实例2)
       - > ChildProduct(这个人与实例1相同)
当我尝试将其作为Json检索时,它会出现一个巨大的错误:

com.google.gson.stream.JsonWriter.beforeName(JsonWriter.java:618)
    com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:401)
    com.google.gson.stream.JsonWriter.value(JsonWriter.java:527)
    com.google.gson.internal.bind.TypeAdapters$11.write(TypeAdapters.java:310)
    com.google.gson.internal.bind.TypeAdapters$11.write(TypeAdapters.java:295)
    com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
    com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:976)

以下是来源:

package com.thalesgomes.ws.rest.classes;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

@Entity
@Table(name="PRODUCT")
public class Product {

    @Id
    @GeneratedValue
    @Column(name="product_id")
    private Long id;

    @ManyToOne(cascade= {CascadeType.ALL})
    @JoinColumn(name="parent_id")
    private Product parent;

    private String name;

    private String description;

    @OneToMany(mappedBy="parent",cascade={CascadeType.ALL})
    private List<Product> children;

    @OneToMany(mappedBy="product",cascade={CascadeType.ALL})
    private List<Image> images;



    //Getters and Setters

}

在这里:

package com.thalesgomes.ws.rest.JPA;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.google.gson.Gson;
import com.thalesgomes.ws.rest.classes.Image;
import com.thalesgomes.ws.rest.classes.Product;

public class ProductJPA {

    public static void main(String[] args) {
        populateDatabase();
        getProducts();

    }

    private static void getProducts() {
        // TODO Auto-generated method stub
        EntityManager em = getManager();
        em.getTransaction().begin();
        List<Product> products  = em.createQuery("from Product", Product.class).getResultList();
        Gson gson = new Gson();
        String json = gson.toJson(products);
        System.out.println(json);
    }

    public static EntityManager getManager() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("Persistence");
        return factory.createEntityManager();
    }

    public static void populateDatabase() {

        List<Image> images = new ArrayList<Image>();
        Image img1 = new Image();
        Image img2 = new Image();

        Product parent = new Product();
        parent.setDescription("descriptionParent");
        parent.setName("nameParent");
        parent.setParent(null);

        Product child1 = new Product();


        img1.setProduct(parent);
        img1.setType("jpeg");
        images.add(img1);

        child1.setName("name1");
        child1.setDescription("description1");
        child1.setParent(parent);
        child1.setImages(images);

        Product child2 = new Product();
        img2.setProduct(child2);
        img2.setType("jpeg");
        images.add(img2);
        child2.setName("name2");
        child2.setDescription("description2");
        child2.setParent(parent);
        child2.setImages(images);





        EntityManager manager = getManager();
        manager.getTransaction().begin();
        try {
            manager.persist(img1);
            manager.persist(img2);
            manager.persist(parent);
            manager.persist(child1);
            manager.persist(child2);

        } catch (Exception e) {
            manager.getTransaction().rollback();
        }
        manager.getTransaction().commit();
        System.out.println("Commit done.");
        manager.close();

    }

}

1 个答案:

答案 0 :(得分:0)

我使用Google的Gson lib解决了我的问题。 我使用了以下注释:

@Exposed  

在ChildProduct实体中,使ChildProduct看不到ParentProduct,但ParentProdcut可以从ChildProduct看到。

同样适用于Image实体,其中Image无法看到与自身相关的Product,但保存Image的Product可以看到Image。