發送POSTTransientObjectException在休眠
後捲曲本地主機:8888 /票據/ addbill -H 「內容類型:應用/ JSON」 -X POST -d「{ 「號」: 「111A111」, 「客戶」:「客戶客戶Rrrr」,「電話」:「1 800 5551212」,「經理」:「經理經理」,「日期」:「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:請求處理失敗;嵌套的異常是org.springframework.dao.InvalidDataAccessApiUsageException:org.hibernate.TransientObjectException:對象引用一個未保存的瞬態實例 - 在沖洗前保存瞬態實例:ru.test.practice.model.Payment;嵌套異常是java.lang.IllegalStateException:org.hibernate.TransientObjectException:對象引用一個未保存的瞬態實例 - 在沖洗之前保存瞬態實例:ru.test.practice.model.Payment「,」path「:」/ bills/addbill「 }
我有一個多對多的關係在我的實體「條例」,這裏有田,惹錯誤
比爾實體:
@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);
付款實體:
@ManyToMany(mappedBy = "payments", cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
private List<Bill> bills = new ArrayList<>(0);
這裏是我的BillService文件:
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());
}
}
不知道是怎麼回事,我已經檢查了計算器類似的問題,它不`噸幫助。 謝謝c: