0

以下は私のコードです..私はSpringフレームワークn hibernate JPAを適用しました.それを保存する1つの関数を除いてすべてのコードが機能します..同様の他のコードでは、Makananエンティティのサービスはうまく機能します..どこが間違っているのかわかりません.. .

3 つのエンティティがあり、そのうちの 1 つは 2 つの多対 1 関係を持っています。

SalesDetail は、Sales および Makanan と関係があります... Makanan のすべてのコードは正常に機能しますが、Sales および SalesDetail では機能しません。

Salesを保存したいとき、このようなエラーになります

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;

ありがと....

4

1 に答える 1

1

Sales以下の例のように、およびSalesDetailクラスを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>
于 2013-05-25T23:15:23.437 に答える