我有一个具有子类InventoryProduct的Product类。它们通过数据库中的upc(通用产品代码)连接。这是Product类。
@Entity
@Table(name = "PRODUCT")
@Inheritance(strategy = InheritanceType.JOINED)
public class Product implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private long id;
private String upc;
private Category category;
private String name;
private String description;
private BigDecimal price;
public Product(){
}
public Product(String upc){
this.upc = upc;
}
public Product(String upc, String name, BigDecimal price){
this.upc = upc;
this.name = name;
this.price = price;
}
public Product(String upc, Category category, String name, String description, BigDecimal price) {
super();
this.upc = upc;
this.category = category;
this.name = name;
this.description = description;
this.price = price;
}
public void setId(long id) {
this.id = id;
}
@Id @GeneratedValue
@Column(name = "id")
public long getId() {
return id;
}
@Column(name = "upc")
public String getUpc() {
return upc;
}
public void setUpc(String upc) {
this.upc = upc;
}
@OneToOne
@JoinColumn(name = "category_id", referencedColumnName = "category_id", insertable = false, updatable = false)
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name = "price")
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
}
这是InventoryProduct类
@Entity
@Table(name = "PRODUCT_INVENTORY")
@Polymorphism(type = PolymorphismType.EXPLICIT)
public class InventoryProduct extends Product {
/**
*
*/
private static final long serialVersionUID = 1L;
private long id;
private Product product;
private int stock;
public InventoryProduct(){
}
public InventoryProduct(Product product) {
super(product.getUpc(), product.getCategory(), product.getName(), product.getDescription(), product.getPrice());
// TODO Auto-generated constructor stub
}
public InventoryProduct(Product product, int stock) {
super(product.getUpc(), product.getCategory(), product.getName(), product.getDescription(), product.getPrice());
this.stock = stock;
}
public InventoryProduct(String upc, int stock){
setUpc(upc);
this.stock = stock;
}
@Id @GeneratedValue
@Column(name = "id")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "stock")
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name="upc", referencedColumnName = "upc", insertable = false, updatable = false)
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}
我正在尝试做的是在这两个对象之间进行内部连接。这是我的疑问:
public static final String HQL_GET_INVENTORY_PRODUCT = "from InventoryProduct as ip inner join ip.product as p where ip.upc = :upc";
这是我使用此查询的地方:
@Override
public InventoryProduct getInventoryProductByUpc(String upc) {
InventoryProduct product = null;
Session session = sessionFactory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Query query = session.createQuery(DAOQuery.HQL_GET_INVENTORY_PRODUCT)
.setParameter("upc", upc);
product = (InventoryProduct) query.getSingleResult();
transaction.commit();
} catch (HibernateException e) {
if (transaction != null)
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
return product;
}
我不知道为什么我会遇到这种类型错误:
Oct 19, 2016 11:06:33 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/spring-machine-problem] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.qbryx.domain.InventoryProduct] with root cause
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.qbryx.domain.InventoryProduct
at com.qbryx.dao.ProductDaoHQLImpl.getInventoryProductByUpc(ProductDaoHQLImpl.java:104)
at com.qbryx.service.CustomerServiceImpl.addProductInCart(CustomerServiceImpl.java:44)
at com.qbryx.controller.CustomerController.productCart(CustomerController.java:91)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.springframework.orm.hibernate5.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:151)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
任何人都可以帮助我吗?谢谢。我已经被困在这里好几个小时了。
修改
这是异常指向的地方:
product = (InventoryProduct) query.getSingleResult();
答案 0 :(得分:0)
getSingleResult
返回对象数组,我想这个arrai的第一个元素就是你的产品。
Object[] arr = (Object[]) query.getSingleResult();
if(arr.length == 0) {
product = null;
} else {
product = (InventoryProduct)arr[0];
}
你可以使用返回列表的方法(据我记得getResult
)它可以为你节省一次演员。