0

I am using spring framework and hibernate as an ORM tool.

My parent class is like:

@Entity

public class Category {
@Id
@GeneratedValue

@NotNull
private int cid;
private String cname;
@OneToMany(cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn(name = "cid")
List<Ad> ads;

//getter and setter, constructor }

And my child class is like:

@Entity
public class Ad {
private int adid;
private String adName;
//getter and setter, constructor
}

My category controller is :

@Controller
public class CategoryController {

@Autowired
SessionFactory sessionFactory;
Session session;
@Transactional
@RequestMapping(value = "categories",method = RequestMethod.GET)
@ResponseBody
public List<Category> getAllCategory()throws SQLException{
    session=sessionFactory.getCurrentSession();
    return session.createCriteria(Category.class).list();
}
}

When i hit url localhost:8080/categories .I get json data like:

{"cid":"1",cname":"category","ads":[{"adid":"1","adName":"ad"}]}

Here I am getting datas of both parent and the related child table.But how can I get only datas of parent table.Here in this example, I need data like:

{"cid":"1",cname":"category"}

How can I do this

pravin
  • 51
  • 6

2 Answers2

1

I saw a nice article which describes exactly your problem.

Json Exclude Property

By configuring Entity using @JsonIgnore and @JsonProperty annotations you can achieve this.

Mihir
  • 75
  • 1
  • 9
0

You can try as below

Infinite Recursion with Jackson JSON and Hibernate JPA issue

Basically have to apply exclusion where you need to break the link

Community
  • 1
  • 1
Chakradhar K
  • 501
  • 13
  • 40