I have two objects ( Category | product )with a bi-directional ratio @OneToMany.
@Entity
public class Category {
public Category() {
}
public Category(String name,String description) {
this.name=name;
this.description=description;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long cid;
private String name;
private String description;
@OneToMany(mappedBy="category",cascade=CascadeType.ALL, orphanRemoval=true)
private Set<Product> products;
/..getters and setter.../
}
@Entity
public class Product {
public Product() {
}
public Product(long price, String description, String name) {
this.name=name;
this.description=description;
this.price=price;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long pid;
private long price;
private String name;
private String description;
@ManyToOne
private Category category;
/..getters and setters../
}
in my controller I have a function / categorization that adds a new category with one product, it works fine, and in my database I have a foreign category identifier
But when I try to get all categories using responseBody, I got a strange object in a category (I want to have in the product, categories: category identifier, not the object itself)
public @ResponseBody Category create() {
Category c=new Category("LIGA","messi feghouli cristiano");
Product p=new Product(200,"jahd besaf","Real Madrid");
if(c.getProducts()!=null){
c.addProducts(p);
}else{
Set<Product> products=new HashSet<Product>();
products.add(p);
c.setProducts(products);
}
p.setCategory(c);
cDao.save(c); pDao.save(p);
return c;
}
@RequestMapping(value="/categories",method = RequestMethod.GET)
public @ResponseBody List<Category> categories() {
return cDao.findAll();
}
this is the strat object i got:
{"cid":1,"name":"LIGA","description":"messi feghouli cristiano","products":[{"price":200,"name":"Real Madrid","description":"jahd besaf","category":{"cid":1,"name":"LIGA","description":"messi feghouli cristiano","products":[{"price":200,"name":"Real Madrid","description":"jahd besaf","category":{"cid":1,"name":"LIGA","description":"messi feghouli cristiano","products":[{"price":200,"name":"Real Madrid","description":"jahd besaf","category":{"cid":1,"name":"LIGA","description":"messi feghouli cristiano","products":