下面是我的代码。我应用了Spring框架Hibernate JPA,除了保存该函数的一个函数外,我所有的代码都可以工作。 。

有3个实体,其中一个实体具有两个多对音关系。

SalesDetail与Sales和Makanan有关系...
Makanan的所有代码都可以正常工作,但Sales和SalesDetail的所有代码都不能工作。

当我要保存销售时,它的运行错误是这样的

Exception in thread "main" org.hibernate.MappingException: Unknown entity: restodeskapp.model.Sales
    at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:550)
    at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1338)
    at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:180)
    at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:512)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:80)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
    at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
    at restodeskapp.dao.BaseDaoHibernate.save(BaseDaoHibernate.java:29)
    at restodeskapp.service.impl.SalesServiceImpl.save(SalesServiceImpl.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy16.save(Unknown Source)
    at restodeskapp.MainSpring.main(MainSpring.java:74)


这是我的销售代码

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Cascade;

@Entity
@Table(name="T_SALES")
public class Sales implements Serializable{

    @Id @GeneratedValue
    @Column(name="ID")
    private Long id;

    @Column(name="NO_TABLE", nullable=false)
    private int noTable;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="SALES_DATE",nullable=false)
    private Date salesDate;

    @OneToMany(mappedBy="sales",cascade=CascadeType.ALL)
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    private List<SalesDetail> salesDetails;

    @Column(name="TOTAL_SALES",precision=18,scale=0,nullable=false)
    private BigDecimal totalSales;

//setter getter
}


这是我的SalesDetail

@Entity
@Table(name="T_SALES_DETAIL")
public class SalesDetail implements Serializable{

    @Id @GeneratedValue
    @Column(name="ID")
    private Long id;

    @ManyToOne
    @JoinColumn(name="MAKANAN_ID",nullable=false)
    private Makanan product;

    @Column(name="QUANTITY",nullable=false)
    private Integer quantity;

    @Column(name="PRICE",nullable=false,precision=18,scale=0)
    private BigDecimal price;

    @Column(name="SUBTOTAL",nullable=false,precision=18,scale=0)
    private BigDecimal subtotal = BigDecimal.ZERO;

    @ManyToOne
    @JoinColumn(name="SALES_ID",nullable=false)
    private Sales sales;
    //setter getter
}


基于aohibernate.java

public class BaseDaoHibernate<T> {
    @SuppressWarnings("unchecked")
    protected Class domainClass;
    @Autowired
    protected SessionFactory sessionFactory;
    @SuppressWarnings("unchecked")
    public BaseDaoHibernate() {
        this.domainClass = (Class) ((ParameterizedType)
        getClass().getGenericSuperclass())
        .getActualTypeArguments()[0];
    }
    public T save(T domain) {
    sessionFactory.getCurrentSession().saveOrUpdate(domain);
        return domain;
    }
    @SuppressWarnings("unchecked")
    public T getById(Long id) {
        return (T) sessionFactory.getCurrentSession().get(domainClass, id);
    }
    public T delete(T domain) {
        sessionFactory.getCurrentSession().delete(domain);
        return domain;
    }
    @SuppressWarnings("unchecked")
    public T getByName(String name) {
        return (T) sessionFactory.getCurrentSession().get(domainClass, name);
    }
    @SuppressWarnings("unchecked")
        public Long count() {
        List list = sessionFactory.getCurrentSession().createQuery(
        "select count(*) from " + domainClass.getName() + " x").list();
        Long count = (Long) list.get(0);
        return count;
    }
    @SuppressWarnings("unchecked")
    public List<T> getAll() {
        return sessionFactory.getCurrentSession().createQuery("from " +
        domainClass.getName())
        .list();
    }
    @SuppressWarnings("unchecked")
    public List<T> getAll(int start, int num) {
        return sessionFactory.getCurrentSession().createQuery("from " +
        domainClass.getName())
        .setFirstResult(start).setMaxResults(num)
        .list();
    }
}


SalesServiceImpl

@Service(value="salesService")
@Transactional(readOnly=true)
public class SalesServiceImpl implements SalesService{
    @Autowired private SalesDao salesDao;
    @Transactional
    public Sales save(Sales sales) {
        //agar yang digunakan adalah Date server bukan date client kalau dijalankan
        //dengan arsitektur three tier
        sales.setSalesDate(new Date());
        return salesDao.save(sales);
    }
    @Transactional
    public Sales delete(Sales sales) {
        return salesDao.delete(sales);
    }
    public Sales getSales(Long id) {
        Sales s = salesDao.getById(id);
        Hibernate.initialize(s.getSalesDetails());
        return s;
    }
    public List<Sales> getSales() {
        return salesDao.getAll();
    }
    public List<Sales> getSales(int start, int num) {
        return salesDao.getAll(start, num);
    }
}


我的数据库架构

create table T_MAKANAN(
id integer auto_increment primary key,
`name` varchar(100) unique not null,
description varchar(777) not null,
price decimal(65) not null,
note varchar(777),
status varchar(200) not null,
type varchar(200) not null,
picture varbinary(2222222222) not null
) engine=InnoDB;

create table T_SALES(
id integer auto_increment primary key,
no_table integer(50) not null,
sales_date date not null,
total_sales decimal(65) not null
) engine=InnoDB;

create table T_SALES_DETAIL(
id integer auto_increment primary key,
quantity integer not null,
price decimal(65) not null,
subtotal decimal(65) not null,
makanan_id integer not null,
sales_id integer not null,
FOREIGN KEY (makanan_id) REFERENCES T_MAKANAN(id),
FOREIGN KEY (sales_id) REFERENCES T_SALES(id)
) engine=InnoDB;


谢谢你....

最佳答案

必须将SalesSalesDetail类添加到persistence.xml文件中,如下例所示:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">
   <persistence-unit name="myapp" transaction-type="RESOURCE_LOCAL">
       <class>package.Sales</class>
       <class>package.SalesDetail</class>
    </persistence-unit>
</persistence>

10-01 12:04