Reputation: 187
After sending a POST
curl localhost:8888/bills/addbill -H "Content-Type: application/json" -X POST -d '{"number":"111A111", "customer":"Customer Cuustomer Rrrr", "phone":"1 800 5551212", "manager":"Manager Manager Manager", "date":"2012-09-17", "curId":{"id":"1"}, "payments":[{"id":"1", "name":"pomyit stekla","price":"1000.0"}]}'
I get this:
{"timestamp":1503954247880,"status":500,"error":"Internal Server Error","exception":"org.springframework.dao.InvalidDataAccessApiUsageException","message":"org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: ru.test.practice.model.Payment; nested exception is java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: ru.test.practice.model.Payment","path":"/bills/addbill"}
I have a many to many relationship in my entity "Bill", here are the fields, that provoke the error.
Bill entity:
@ManyToMany
@JoinTable(name="BILL_PAYMENTS",
joinColumns=
@JoinColumn(name="payments_id", referencedColumnName="id"),
inverseJoinColumns=
@JoinColumn(name="bills_id", referencedColumnName="id")
)
private List<Payment> payments = new ArrayList<>(0);
Payment entity:
@ManyToMany(mappedBy = "payments", cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
private List<Bill> bills = new ArrayList<>(0);
Here is my BillService file:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.test.practice.dao.BillDao;
import ru.test.practice.model.Bill;
import ru.test.practice.service.BillService;
import ru.test.practice.view.BillView;
import ru.test.practice.model.Payment;
import ru.test.practice.view.PaymentsView;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
public class BillServiceImpl implements BillService {
private final Logger log = LoggerFactory.getLogger(BillServiceImpl.class);
private final BillDao dao;
@Autowired
public BillServiceImpl(BillDao dao) {
this.dao = dao;
}
public List<Payment> convertToPayment(List<PaymentsView> paymentsView){
List<Payment> payments = new ArrayList<>();
for(PaymentsView pv : paymentsView) {
Payment payment = new Payment(pv.id, pv.name, pv.price);
payments.add(payment);
}
return payments;
}
public List<PaymentsView> convertToPaymentView(List<Payment> payments){
List<PaymentsView> paymentsView = new ArrayList<>();
for(Payment p : payments){
PaymentsView paymentView = new PaymentsView(p.getId(), p.getName(), p.getPrice());
paymentsView.add(paymentView);
}
return paymentsView;
}
@Override
@Transactional
public void add(BillView view) {
List<Payment> payments = convertToPayment(view.payments);
Bill bill = new Bill(view.id, view.number, view.customer, view.phone, view.manager, view.date, view.curId, payments);
dao.save(bill);
}
@Override
@Transactional(readOnly = true)
public List<BillView> bills() {
List<Bill> all = dao.all();
Function<Bill, BillView> mapBill = b -> {
BillView view = new BillView();
view.id = b.getId();
view.number = b.getNumber();
view.customer = b.getCustomer();
view.phone = b.getPhone();
view.manager = b.getManager();
view.date = b.getDate();
view.curId = b.getCurId();
view.payments = convertToPaymentView(b.getPayments());
log.debug(view.toString());
return view;
};
return all.stream()
.map(mapBill)
.collect(Collectors.toList());
}
}
Have no idea what is going on, I have already checked similar questions on stackoverflow and it doesn`t help. thanks c:
Upvotes: 3
Views: 5314
Reputation: 6574
you are trying to persist an object which has relation to a transient object, your payments are not managed so when you try to save your bill you will get this exception.
Solution1: you need either to save each one of the payments and add the managed object ( the one returned from the save method) .
Solution2: you can just cascade the payments so when the bill is persisted the payments will be persisted as well
@ManyToMany( cascade = {CascadeType.ALL})
@JoinTable(name="BILL_PAYMENTS",
joinColumns=
@JoinColumn(name="payments_id", referencedColumnName="id"),
inverseJoinColumns=
@JoinColumn(name="bills_id", referencedColumnName="id")
Upvotes: 3