Я создал таблицу: < /p>
Код: Выделить всё
CREATE TABLE supplier_ingredient (
supplier_id BIGINT,
ingredient_id BIGINT
)
ALTER TABLE supplier_ingredient ADD CONSTRAINT supplier_ingredient_pkey
PRIMARY KEY(supplier_id, ingredient_id);
ALTER TABLE supplier_ingredient ADD CONSTRAINT
fk_supplier_ingredient_ingredient_id FOREIGN KEY (ingredient_id)
REFERENCES ingredient(id);
ALTER TABLE supplier_ingredient ADD CONSTRAINT
fk_supplier_ingredient_supplier_id FOREIGN KEY (supplier_id) REFERENCES
supplier(id);
Код: Выделить всё
.....
.....
@ManyToMany(mappedBy = "ingredients")
@OrderBy("created DESC")
@BatchSize(size = 1000)
private List suppliers = new ArrayList();
....
....
Код: Выделить всё
....
@ManyToMany
@JoinTable( name = "supplier_ingredient ",
joinColumns = @JoinColumn(name = "supplier_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "ingredient_id", referencedColumnName = "id"),
foreignKey = @ForeignKey(name = "fk_supplier_ingredient_supplier_id"))
@OrderBy("created DESC")
@Cascade(CascadeType.SAVE_UPDATE)
@BatchSize(size = 1000)
private List ingredients = new ArrayList();
....
Код: Выделить всё
@RequestMapping(value = "/{supplierId:[0-9]+}", method = RequestMethod.GET)
@ResponseStatus(value = HttpStatus.OK)
@ResponseBody
public SupplierObject get(@PathVariable Long supplierId) {
Supplier supplier = supplierService.get(supplierId);
SupplierObject supplierObject = new SupplierObject (supplier);
return SupplierObject;
}
Код: Выделить всё
....
public Supplier get(Long supplierId) {
Supplier supplier = supplierDao.getById(supplierId); (it does entityManager.find(entityClass, id))
if (supplier == null) throw new ResourceNotFound("supplier", supplierId);
return supplier;
}
....
Код: Выделить всё
@JsonIgnoreProperties(ignoreUnknown = true)
public class SupplierObject extends IdAbstractObject {
public String email;
public String phoneNumber;
public String address;
public String responsible;
public String companyName;
public String vat;
public List ingredients = new ArrayList();
public SupplierObject () {
}
public SupplierObject (Supplier supplier) {
id = supplier.getId();
email = supplier.getEmail();
responsible = supplier.getResponsible();
companyName = supplier.getCompanyName();
phoneNumber = supplier.getPhone_number();
ingredients = supplier.getIngredients();
vat = supplier.getVat();
address = supplier.getAddress();
}
}
Код: Выделить всё
public abstract class IdAbstractObject{
public Long id;
}
< /code>
Моя проблема в том, когда я вызываю конечную точку: < /p>
http://localhost:8080/supplier/1
< /code>
Я получил ошибку: < /p>
"Не удалось написать json: не удалось инициализировать коллекцию роли
: mypackage.ingredient.ingredient.suppliers, не удалось инициализировать
proxy - без сессии; /> comm.fasterxml.jackson.databind.jsonmappingexception: не удалось лениво
инициализировать сбор роли:
mypackage.ingredient.ingredient.suppliers, не может инициализировать прокси < /p>
. mypackage.supplier.supplierobject ["ingridients"]-> org.hibernate.collection.internal.persistentbag [0]-> mypackage.ingredient.ingredient ["Поставщики"]) "< /li>
< /ul>
< /blockquote>
< /ul> < /pr /> < /pr /> Сериализация Джексона на не извлеченных ленивых объектах < /p>
Теперь я не ошибся, но в возвращении JSON поле ингредиентов равна нулю: < /p>
{
"id": 1,
"email": "mail@gmail.com",
"phoneNumber": null,
"address": null,
"responsible": null,
"companyName": "Company name",
"vat": "vat number",
"ingredients": null
}
Подробнее здесь: https://stackoverflow.com/questions/481 ... on-of-role