org.hibernate.MappingException:未知实体:domain.ShoppingCart$Proxy$\u$$\ uWeldClientProxy
我遇到了一个奇怪的错误,我无法调试。如果你能详细介绍一下细节并给我一些建议,我将不胜感激,我的朋友们 我正在使用JBossServer7.1 Web应用程序适用于所有模块。但当我在购物车中处理完产品并进行“结帐”时,就会出现异常 我提供了几乎所有需要的信息,如果你还需要什么,请告诉我。我真的很感激你的善意回应。非常感谢查看我的请求 我在项目中包含的库(JBoss自己包含的库除外) HibernateUtil.javaorg.hibernate.MappingException:未知实体:domain.ShoppingCart$Proxy$\u$$\ uWeldClientProxy,hibernate,jakarta-ee,cdi,hibernate-mapping,Hibernate,Jakarta Ee,Cdi,Hibernate Mapping,我遇到了一个奇怪的错误,我无法调试。如果你能详细介绍一下细节并给我一些建议,我将不胜感激,我的朋友们 我正在使用JBossServer7.1 Web应用程序适用于所有模块。但当我在购物车中处理完产品并进行“结帐”时,就会出现异常 我提供了几乎所有需要的信息,如果你还需要什么,请告诉我。我真的很感激你的善意回应。非常感谢查看我的请求 我在项目中包含的库(JBoss自己包含的库除外) HibernateUtil.java public static SessionFactory getSessio
public static SessionFactory getSessionFactory() {
try{
Configuration conf=new Configuration();
conf.configure("/com/webshop/hb/config/mycfg-mysql.xml");
sessionFactory =conf.buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
return sessionFactory;
}
package domain;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import java.io.Serializable;
@Named
@SessionScoped
@Entity
public class ShoppingCart implements Serializable { private static long currentId =1;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="ID")
private long id;
@Column(name="date")
private Date date;
@ManyToOne
@JoinColumn(name="user")
private User user;
@ElementCollection
private Map<Product, Integer> orders = new HashMap<Product, Integer>();
@Column(name="status")
private OrderStatus status; // String
public ShoppingCart() {
this(new Date());
}
public ShoppingCart(Date date) {
super();
this.id = currentId++; // Simple way of getting an unique id. To be deferred to the DBMS later.
this.date = date;
this.status = OrderStatus.EMPTY;
}
public ShoppingCart(Date date, User user) {
this(date);
this.user = user;
}
public ShoppingCart(Date date, User user, OrderStatus status) {
this(date, user);
if (((status == OrderStatus.COMPLETED) || (status == OrderStatus.PAID)) && (this.getUser() == null)) {
throw new IllegalStateException("No user specified for complete or paid orders");
}
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Map<Product, Integer> getOrders() {
return orders;
}
public void setOrders(Map<Product, Integer> orders) {
this.orders = orders;
}
public OrderStatus getStatus() {
return status;
}
public void setCompleted(OrderStatus status) throws IllegalStateTransitionException {
if ((status == OrderStatus.COMPLETED) && (this.getUser() == null)) {
throw new IllegalStateTransitionException(this.getStatus().toString(), OrderStatus.COMPLETED.toString(), "Customer empty");
}
if ((status == OrderStatus.PAID) && (this.getUser() == null)) {
throw new IllegalStateTransitionException(this.getStatus().toString(), OrderStatus.PAID.toString(), "Customer empty");
}
if ((status == OrderStatus.PAID) && (this.getStatus() != OrderStatus.COMPLETED)) {
throw new IllegalStateTransitionException(this.getStatus().toString(), OrderStatus.PAID.toString(), "Can only go to paid for completed orders");
}
this.status = status;
}
public long getId() {
return id;
}
public void addToCart(Product product, int amountSelected) {
if (orders.containsKey(product)) {
int amount = orders.get(product);
amount +=amountSelected;
orders.put(product, amount);
}
else {
orders.put(product, amountSelected);
}
}
public void removeFromCart(Product product) {
if (orders.containsKey(product)) {
int amount = orders.get(product);
amount --;
if (amount <= 0) {
orders.remove(product);
}
else {
orders.put(product, amount);
}
}
}
public int getNumberOfItems() {
Set<Product> uniqueProducts = orders.keySet();
int amount = 0;
for (Product aProduct: uniqueProducts) {
amount += orders.get(aProduct);
}
return amount;
}
@Override
public String toString() {
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Calendar cal = Calendar.getInstance();
return "Order of " + this.user + ", date " + dateFormat.format(cal.getTime());
}
}
ShoppingCart.java
public static SessionFactory getSessionFactory() {
try{
Configuration conf=new Configuration();
conf.configure("/com/webshop/hb/config/mycfg-mysql.xml");
sessionFactory =conf.buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
return sessionFactory;
}
package domain;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import java.io.Serializable;
@Named
@SessionScoped
@Entity
public class ShoppingCart implements Serializable { private static long currentId =1;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="ID")
private long id;
@Column(name="date")
private Date date;
@ManyToOne
@JoinColumn(name="user")
private User user;
@ElementCollection
private Map<Product, Integer> orders = new HashMap<Product, Integer>();
@Column(name="status")
private OrderStatus status; // String
public ShoppingCart() {
this(new Date());
}
public ShoppingCart(Date date) {
super();
this.id = currentId++; // Simple way of getting an unique id. To be deferred to the DBMS later.
this.date = date;
this.status = OrderStatus.EMPTY;
}
public ShoppingCart(Date date, User user) {
this(date);
this.user = user;
}
public ShoppingCart(Date date, User user, OrderStatus status) {
this(date, user);
if (((status == OrderStatus.COMPLETED) || (status == OrderStatus.PAID)) && (this.getUser() == null)) {
throw new IllegalStateException("No user specified for complete or paid orders");
}
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Map<Product, Integer> getOrders() {
return orders;
}
public void setOrders(Map<Product, Integer> orders) {
this.orders = orders;
}
public OrderStatus getStatus() {
return status;
}
public void setCompleted(OrderStatus status) throws IllegalStateTransitionException {
if ((status == OrderStatus.COMPLETED) && (this.getUser() == null)) {
throw new IllegalStateTransitionException(this.getStatus().toString(), OrderStatus.COMPLETED.toString(), "Customer empty");
}
if ((status == OrderStatus.PAID) && (this.getUser() == null)) {
throw new IllegalStateTransitionException(this.getStatus().toString(), OrderStatus.PAID.toString(), "Customer empty");
}
if ((status == OrderStatus.PAID) && (this.getStatus() != OrderStatus.COMPLETED)) {
throw new IllegalStateTransitionException(this.getStatus().toString(), OrderStatus.PAID.toString(), "Can only go to paid for completed orders");
}
this.status = status;
}
public long getId() {
return id;
}
public void addToCart(Product product, int amountSelected) {
if (orders.containsKey(product)) {
int amount = orders.get(product);
amount +=amountSelected;
orders.put(product, amount);
}
else {
orders.put(product, amountSelected);
}
}
public void removeFromCart(Product product) {
if (orders.containsKey(product)) {
int amount = orders.get(product);
amount --;
if (amount <= 0) {
orders.remove(product);
}
else {
orders.put(product, amount);
}
}
}
public int getNumberOfItems() {
Set<Product> uniqueProducts = orders.keySet();
int amount = 0;
for (Product aProduct: uniqueProducts) {
amount += orders.get(aProduct);
}
return amount;
}
@Override
public String toString() {
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Calendar cal = Calendar.getInstance();
return "Order of " + this.user + ", date " + dateFormat.format(cal.getTime());
}
}
对我来说,原因很明显:您的
ShoppingCart
实体也是一个CDI管理的bean。我几乎可以肯定,您正在将shoppingcart
注入方法confirmOrder
上方的某个位置:
@Inject
ShoppingCart shoppingcart;
我说得对吗?如果是,那么这就是原因
通常JPA实体作为CDIBeans。这背后的原因是防止BeanManager执行可能导致JPA提供程序中断的操作(因为这会导致异常)
因此,请尝试以下方法:
package domain;
@Entity
public class ShoppingCart implements Serializable {
// ...
}
然后定义另一个类,该类保存上述实体:
package com.some_package;
@Named
@SessionScoped
public class ShoppingCartWrapper {
ShoppingCart store = new ShoppingCart(); // this is your entity
}
这个类可以被注入到任何你想要的地方。然后保存看起来像:
public String confirmOrder() throws IllegalStateTransitionException {
// ...
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(shoppingCartWrapper.getStore());
session.close();
// ...
}
顺便说一句:个人提示:像这样管理事务-我的意思是“手工”,没有拦截器很容易出错,并且不会使代码变得干净