2013-05-25 125 views
0

下面是我的代碼..我應用了Spring框架n休眠JPA,除了一個保存它的函數之外,我的所有代碼都可以工作..在其他類似的代碼中,Service for Makanan實體工作好..我不知道哪裏是錯誤的...org.hibernate.MappingException:未知實體SessionFactoryImpl.getEntityPersister

有3個實體(S),其有兩個多對一關係的..

的salesdetail有它的銷售和Makanan關係.. Makanan的所有代碼都很好用,但不適用於Sales and 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 
} 

basedaohibernate.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; 

感謝ü....

回答

1

因此,有必要對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> 
相關問題