简体   繁体   English

如何映射一个非持久性用户定义的类,以便Hibernate从SQLQuery实例加载bean

[英]How to map a non-persistent user defined class so that Hibernate will load bean with instance from SQLQuery

I am new to hibernate and am having difficulty trying to get it to work for anything other than a direct table mapping scenario. 我是新来的冬眠者,在尝试使其直接工作于表映射方案之外的其他任何方面都遇到困难。 Basically, I have a user defined Java class called: BookInvoice. 基本上,我有一个用户定义的Java类:BookInvoice。 This class is a non-persistent (not a db table) and uses columns from previously defined and Annotated) db tables. 此类是非持久性(不是db表),并且使用先前定义和带注释的db表中的列。 Every time I try to label it as an @Entity it tells me I cant because it is not an existing db table. 每次我尝试将其标记为@Entity时,它都会告诉我不能,因为它不是现有的数据库表。 How do I map it so that I dont get the 我如何映射它,这样我就不会得到

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

error message that I have been experiencing. 我一直遇到的错误消息。

My sql queries are good and I am able to get the info from the db; 我的SQL查询很好,我可以从数据库中获取信息; however, they come back as class Object and I am not permitted to cast them into my desired BookInvoice class in order to send it back to the calling method. 但是,它们作为Object类返回,并且我不允许将它们强制转换为所需的BookInvoice类,以便将其发送回调用方法。 Below pls find snipets of my work thus far . 请在下面找到我到目前为止工作的片段。 . .

Please note all of my regular classes that conform to existing db tables queries work fine, it is just the ones that are non-persistent that I am having issues with. 请注意,我所有符合现有数据库表查询的常规类都可以正常工作,这只是我遇到问题的非持久类。

PurchaseOrderInvoiceDAO: 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 class: 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. . . .

Stack Trace Snippet: 堆栈跟踪代码段:

           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: Unknown entity: org.hibernate.MappingException:未知实体:
com.acentia.training.project15.model.BookInvoice com.acentia.training.project15.model.BookInvoice
org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693) 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) org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:157) com.acentia.training.project15.bo.PurchaseOrderInvoiceBO$PurchaseOrderInvoiceDAO.getById(PurchaseOrderInvoiceBO.java:196) 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)org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:157)com.acentia.training.project15.bo.PurchaseOrderInvoiceBO $ PurchaseOrderInvoiceDAO.getById(PurchaseOrder Java:196)

... ...

Ok! 好! Finally broke down and talked to my supervisor about this. 最终崩溃了,并与我的主管进行了交谈。 He explained that I am doing like waaaaaay to much extra work on this. 他解释说,我正在做很多事情,就像waaaaaay。

Basically, if I set the @Entity class/DB mappings up correctly then they will get all of the right information (ie. @OneToMany , etc.) from the mappings of the classes that correspond directly to the DB tables. 基本上,如果我正确设置了@Entity类/ DB映射,则它们将从直接对应于DB表的类的映射中获取所有正确的信息(即@OneToMany等)。 Basically, Hibernate will go down as many levels (PO_Details ->Payments->Books, etc) they would give me all the additional information that I need and I wouldn't need to create my own custom classes. 基本上,Hibernate会下降许多级别(PO_Details->付款-> Books等),它们将为我提供我需要的所有其他信息,而无需创建自己的自定义类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM