简体   繁体   中英

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. This class is a non-persistent (not a db table) and uses columns from previously defined and Annotated) db tables. Every time I try to label it as an @Entity it tells me I cant because it is not an existing db table. How do I map it so that I dont get the

Unknown entity: 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; 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. 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:

        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:

        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:
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) org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:157) com.acentia.training.project15.bo.PurchaseOrderInvoiceBO$PurchaseOrderInvoiceDAO.getById(PurchaseOrderInvoiceBO.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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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