下面是我的控制器
@PostMapping(value="/shoppingcart")
ResponseEntity<ResponseDTO<ShoppingCartItem>>
insertItems(@RequestParam("cartId")Long cartId, @RequestParam ("itemsKey")
Long itemkey) {
ResponseDTO<ShoppingCartItem> responseDTO = new ResponseDTO<>();
Meta meta = new Meta();
ShoppingCartItem cartItem = null;
ShoppingCartItem saveItem = new ShoppingCartItem();
ShoppingCart cart = new ShoppingCart();
cart.setId(cartId);
Items item = new Items();
item.setId(itemkey);
saveItem.setCart(cart);
saveItem.setItem(item);
cartItem = shoppingCartItemService.save(saveItem);
return new ResponseEntity<ResponseDTO<ShoppingCartItem>>(responseDTO, HttpStatus.OK);
}
这是我的实体类:
@Entity
@Table(name = "tb_shoppingcart_items")
public class ShoppingCartItem implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long cartItemId;
@ManyToOne
@MapsId("shoppingcartId")
private ShoppingCart cart;
@ManyToOne
@MapsId("itemsId")
private Items item;
@Column(name = "quantity")
private boolean quantity;
public Long getCartItemId() {
return cartItemId;
}
public void setCartItemId(Long cartItemId) {
this.cartItemId = cartItemId;
}
public ShoppingCart getCart() {
return cart;
}
public void setCart(ShoppingCart cart) {
this.cart = cart;
}
public Items getItem() {
return item;
}
public void setItem(Items item) {
this.item = item;
}
public boolean isQuantity() {
return quantity;
}
public void setQuantity(boolean quantity) {
this.quantity = quantity;
}
}
@Entity
@Table(name = "tb_shoppingcart")
public class ShoppingCart implements Serializable {
/**
*
*/
private static final long serialVersionUID = -2114870327410506836L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column(name = "dateCreated")
private Date datecreated;
/*
* @JsonIgnore
*
* @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) List<Items> items
* = new ArrayList<>();
*/
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getDatecreated() {
return datecreated;
}
public void setDatecreated(Date datecreated) {
this.datecreated = datecreated;
}
/*
* public List<Items> getItems() { return items; }
*
* public void setItems(List<Items> items) { this.items = items; }
*/
}
将我的shoppingcartItem保存在数据库中时出现以下异常:
错误[http-nio-8090-exec-2] org.apache.juli.logging.DirectJDKLog:路径为[]的Servlet [dispatcherServlet]的Servlet.service()引发异常[请求处理失败;嵌套的异常是org.springframework.beans.NotReadablePropertyException:Bean类[com.epam.item.model.ShoppingCartItem]的无效属性'id':根本原因是在后备访问期间找不到属性字段! org.springframework.beans.NotReadablePropertyException:Bean类[com.epam.item.model.ShoppingCartItem]的无效属性'id':在后备访问期间找不到属性字段! 在org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper.getPropertyValue(DirectFieldAccessFallbackBeanWrapper.java:58) 在org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.getId(JpaMetamodelEntityInformation.java:154) 在org.springframework.data.repository.core.support.AbstractEntityInformation.isNew(AbstractEntityInformation.java:42) 在org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.isNew(JpaMetamodelEntityInformation.java:233) 在org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:488) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498) 在org.springframework.data.repository.core.support.RepositoryComposition $ RepositoryFragments.invoke(RepositoryComposition.java:359) 在org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) 在org.springframework.data.repository.core.support.RepositoryFactorySupport $ ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) 在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608) 在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.lambda $ invoke $ 3(RepositoryFactorySupport.java:595) 在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) 在org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) 在org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) 在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) 在org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) 在org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor $ CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135)处 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) 在org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) 在org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) 在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) 在com.sun.proxy。$ Proxy105.save(未知来源) 在com.epam.item.service.impl.ShoppingCartItemServiceImpl.save(ShoppingCartItemServiceImpl.java:19) 在com.epam.item.controller.ItemController.insertItems(ItemController.java:180) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498) 在org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) 在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) 在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) 在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) 在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) 在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) 在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) 在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:660) 在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:741) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) 在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) 在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) 在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 在org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:791) 在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1417) 在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624) 在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在java.lang.Thread.run(Thread.java:748)