发送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: