azalut
azalut

Reputation: 4234

Huge JSON while saving and returning entity using Hibernate

I am using Hibernate to save my entities to database. For simple example I have two entities:

Car:

@Entity
@Table(name = "car")
public class Car implements Serializable {
    @Id
    @GeneratedValue
    private long id;
    private String name;
    @OneToMany(cascade = CascadeType.PERSIST)
    private List<Wheel> wheelList;

    public Car() {
    }

    public Car(String name, List<Wheel> wheelList) {
        this.name = name;
        this.wheelList = wheelList;
    }

and Wheel:

@Entity
@Table(name = "wheel")
public class Wheel implements Serializable {
    @Id
    @GeneratedValue
    private long id;
    private int size;
    @ManyToOne(cascade = CascadeType.PERSIST)
    private Car car;

    public Wheel() {
    }

    public Wheel(int size, Car car) {
        this.size = size;
        this.car = car;
    }

I am using Spring Framework (MVC) to create REST client, from which I return JSON. And now: I created simple @Repository and @Service.

I wanted to try if saving works, so I created simple Car object with its wheel list etc. and then, in @Service layer I have persisted it and returned car object into the controller.

Finally, when i run the url from the controller, I recieve HUGE json, because every Car object has wheel list, and then, every wheel has its car, which again has list, and in list are wheels which has cars and so on.. thats why my JSON is so big.

{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":{"id":1,"name":"SomeName","wheelList":[{"id":1,"size":1,"car":...........

What do i do wrong? How should i solve this problem?

Rest of the code if you need it:(made just for tests, I know about not-well-coded-style here) Repository:

@Repository
public class TestImpl {
    @PersistenceContext
    private EntityManager entityManager;

    public Car saveCar(Car car){
        entityManager.persist(car);
        return car;
    }
}

Service

@Service
@Transactional
public class TestService {
    @Autowired
    private TestImpl test;

    public Car save(){
        Car car = new Car();
        car.setName("SomeName");
        List<Wheel> list = new ArrayList<Wheel>();
        list.add(new Wheel(1, car));
        car.setWheelList(list);
        test.saveCar(car);
        return car;
    }
}

Controller:

@Controller
public class TestController {
    @Autowired
    private TestService testService;

    @RequestMapping(value = "/los")
    @ResponseBody
    public Car los(){
        return testService.save();
    }
}

Upvotes: 1

Views: 496

Answers (1)

dimoniy
dimoniy

Reputation: 5995

You can add @JsonIgnore annotation to either wheelList field in your Car object or to car field in your Wheel object. This will stop JSON serializer from showing those fields in the resulting document and from recursively including the field.

Upvotes: 1

Related Questions