我遇到了以下问题。
以下是我的Entity OrderModel。我有另一个名为ItemModel的实体,我想在Map中使用它。我想订单。
对于我为订单选择的每个项目,我想指定这些项目的数量,因此也就是地图中的整数。课程的关键必须是项目对象。
这是OrderModel。
package com.project.pms.model;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
@Entity
public class OrderModel
{
@Id @GeneratedValue
private Long orderId;
private String orderReferenceNumber;
@OneToOne
private ClientModel orderedBy;
@OneToOne
private ClientModel orderedTo;
@OneToMany
@MapKey(name="THE_ITEM")
private Map<ItemModel,Integer> itemList= new HashMap<ItemModel, Integer>();
// the integer above depicts the quantity of the Item
// For each Item I select I need to specify how many of those
private String orderIssuedDate;
private String orderToBeCompletedBy;
private String totalPaymentToBeMade;
private String orderState;
public Long getOrderId()
{
return orderId;
}
public void setOrderId(Long orderId)
{
this.orderId = orderId;
}
public String getOrderReferenceNumber()
{
return orderReferenceNumber;
}
public void setOrderReferenceNumber(String orderReferenceNumber)
{
this.orderReferenceNumber = orderReferenceNumber;
}
public ClientModel getOrderedBy()
{
return orderedBy;
}
public void setOrderedBy(ClientModel orderedBy)
{
this.orderedBy = orderedBy;
}
public ClientModel getOrderedTo()
{
return orderedTo;
}
public void setOrderedTo(ClientModel orderedTo)
{
this.orderedTo = orderedTo;
}
public Map<ItemModel, Integer> getItemList()
{
return itemList;
}
public void setItemList(Map<ItemModel, Integer> itemList)
{
this.itemList = itemList;
}
public String getOrderIssuedDate()
{
return orderIssuedDate;
}
public void setOrderIssuedDate(String orderIssuedDate)
{
this.orderIssuedDate = orderIssuedDate;
}
public String getOrderToBeCompletedBy()
{
return orderToBeCompletedBy;
}
public void setOrderToBeCompletedBy(String orderToBeCompletedBy)
{
this.orderToBeCompletedBy = orderToBeCompletedBy;
}
public String getTotalPaymentToBeMade()
{
return totalPaymentToBeMade;
}
public void setTotalPaymentToBeMade(String totalPaymentToBeMade)
{
this.totalPaymentToBeMade = totalPaymentToBeMade;
}
public String getOrderState()
{
return orderState;
}
public void setOrderState(String orderState)
{
this.orderState = orderState;
}
}
但是我得到以下异常。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.hibernate.SessionFactory com.project.pms.service.ClientService.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/springmvc-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.project.pms.model.OrderModel.itemList[java.lang.Integer]
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1017)
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:746)
org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:716)
org.apache.jsp.index_jsp._jspService(index_jsp.java:67)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.hibernate.SessionFactory com.project.pms.service.ClientService.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/springmvc-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.project.pms.model.OrderModel.itemList[java.lang.Integer]
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
org.springframework
有没有办法可以将numberOfItems Integer与Item类型一起使用。 当然,我在ItemModel本身中不需要这个字段。
也可以有人回答我的QueryByExample问题:) 非常感谢。感谢
答案 0 :(得分:0)
要在JPA2中保留集合,您可以使用@ElementCollection
有关详情,请参阅:http://docs.oracle.com/javaee/6/api/javax/persistence/ElementCollection.html