0

我是hibernate的新手,很难让它在直接表映射场景之外的任何东西上工作。基本上,我有一个用户定义的 Java 类,称为:BookInvoice。此类是非持久性(不是 db 表),并使用先前定义和注释的 db 表中的列。每次我尝试将其标记为 @Entity 时,它都会告诉我我不能,因为它不是现有的数据库表。我如何映射它,以免我得到

未知实体:com.acentia.training.project15.model.BookInvoice

我遇到的错误消息。

我的 sql 查询很好,我可以从数据库中获取信息;但是,它们作为 Object 类返回,我不允许将它们转换为我想要的 BookInvoice 类以便将其发送回调用方法。请在下面找到我迄今为止的工作片段。. .

请注意,我所有符合现有数据库表查询的常规类都可以正常工作,只是那些我遇到问题的非持久类。

PurchaseOrderInvoiceDAO:

        List<BookInvoice> bInvoiceList = null;
        final String bookInvoiceQuery =
                "SELECT Books.ID, PO_Details.QUANTITY, Books.ISBN, Books.TITLE, Books.AUTHOR, Author.Name, Books.PUBLISHED, Books.COVER, Books.SERIES, Books.SERIES_NO,\n" +
                        "          Books.SUBJECT_ID,Books.PRICE\n" +
                        "        FROM Purchase_Order, PO_Details, Books, Author\n" +
                        "        WHERE Purchase_Order.ID=?\n" +
                        "        AND Purchase_Order.ID=PO_Details.PO_ID\n" +
                        "        AND PO_Details.Book_ID=Books.ID\n" +
                        "        AND Books.AUTHOR=Author.ID";

        Query bookInvoicQ = getSession().createSQLQuery(bookInfoQuery).addEntity(BookInvoice.class);
        bookInvoicQ.setInteger(0, id);
        bList = (List<Books>) bookInvoicQ.list();

BookInvoice类:

        public class BookInvoice {

            Integer id = null;
            Integer quantity = null;
            String isbn = null;
            String title = null;
            Integer authorId = null;
            Date publishedDate = null;
            String cover = null;
            String series = null;
            Integer seriesNo = null;
            Integer subjectId = null;
            Double price = null;

            public BookInvoice(final Integer id, final Integer quantity, final String isbn, final String title,
                   final Integer authorId, final Date publishedDate, final String cover,
                   final String series, final Integer seriesNo, final Integer subjectId, final Double price) {
                   this.id = id;
                   this.quantity = quantity;
                   this.isbn = isbn;
                   this.title = title;
                   this.authorId = authorId;
                   this.publishedDate = publishedDate;
                   this.cover = cover;
                   this.series = series;
                   this.seriesNo = seriesNo;
                   this.subjectId = subjectId;
                   this.price = price;
           }

           public BookInvoice(){}

           public Integer getId() {
               return id;
           }

           etc. . . .

堆栈跟踪片段:

           Struts Problem Report

           Struts has detected an unhandled exception:

           Messages:    
           Unknown entity: com.acentia.training.project15.model.BookInvoice
           File:    org/hibernate/impl/SessionFactoryImpl.java
           Line number:     693
           Stacktraces

org.hibernate.MappingException:未知实体:
com.acentia.training.project15.model.BookInvoice
org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693)

org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.getSQLLoadable(SQLQueryReturnProcessor.java:335) org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processRootReturn(SQLQueryReturnProcessor.java:376) org.hibernate.loader.custom.sql。 SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:355) org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:171) org.hibernate.loader.custom.sql.SQLCustomQuery.(SQLCustomQuery.java:87) org .hibernate.engine.query.NativeSQLQueryPlan.(NativeSQLQueryPlan.java:67) org.hibernate.engine.query.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:166) org.hibernate.impl.AbstractSessionImpl.getNativeSQLQueryPlan(AbstractSessionImpl.java:160) org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165) 组织。hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:157) com.acentia.training.project15.bo.PurchaseOrderInvoiceBO$PurchaseOrderInvoiceDAO.getById(PurchaseOrderInvoiceBO.java:196)

...

4

1 回答 1

0

好的!终于崩溃了,和我的主管谈了这件事。他解释说,我在这方面做了很多额外的工作。

基本上,如果我正确设置了类/数据库映射,那么他们将从直接对应于数据库表的类的映射中@Entity获得所有正确的信息(即等)。@OneToMany基本上,Hibernate 将下降到尽可能多的级别(PO_Details ->Payments->Books 等),它们会为我提供我需要的所有其他信息,并且我不需要创建自己的自定义类。

于 2013-02-01T20:59:24.613 回答