-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: How to use projection with inheritance
PostPosted: Fri Nov 20, 2009 11:01 am 
Newbie

Joined: Fri Nov 20, 2009 9:27 am
Posts: 7
Hi,

I have a problem with Projection or hql. Cant handle this problem.
sql looks like this
Code:
select wod.warehouseName, doc.symbol, ir.clientRequestSymbol from WarehouseOutcomeDoc wod join Document doc on wod.id = doc.id left join InternalRequest ir on ir.id = doc.baseDocumentId where wod.id = 750189;


and classes looks like this
Code:
class Document {
  Integer id;
  String symbol;
  Document baseDocument;
}

class WarehouseOutcomeDoc extends Document {
  String warehouseName;
}

class InternalRequest extends Document {
  String clientRequestSymbol;
}

and

baseDocument of WarehouseOutcomeDoc is InternalRequest

i tried to use criteria and projection
Code:
CriteriaFactory.createCriteria(WarehouseOutcomeDocEJB.class.getSimpleName());
ProjectionList proList = Projections.projectionList();
proList.add(Projections.property("baseDocument.clientRequestSymbol"));


but hibernate crash on that projection is there a way to solve it ?


Top
 Profile  
 
 Post subject: Re: How to use projection with inheritance
PostPosted: Mon Nov 23, 2009 10:01 am 
Newbie

Joined: Fri Nov 20, 2009 9:27 am
Posts: 7
help plisss... i do realy need solution on this :(
i tried to use hql but there is still the same problem... is there a way to cast classes up i hql ?


Top
 Profile  
 
 Post subject: Re: How to use projection with inheritance
PostPosted: Mon Nov 23, 2009 10:10 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
If you want urgent help, you need to provide more information next time (exception trace, mapping, ...)!

Your classes look weird, is there no association between them? How does your mapping look like?

Why is there a class WarehouseOutcomeDoc and a WarehouseOutcomeDocEJB?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: How to use projection with inheritance
PostPosted: Mon Nov 23, 2009 10:37 am 
Newbie

Joined: Fri Nov 20, 2009 9:27 am
Posts: 7
Hej i didnt wont to spam unnesesery data but i will try to write everything
my hql
Code:
"select " +
   "wod.id, " +
   "wod.symbol, " +
        "wod.warehouseName, "+
   "wod.creationDate, " +
   "docIr.clientRequestSymbol, " +
"from " +
   "WarehouseOutcomeDocEJB wod left join wod.baseDocument as docIr " +
"where " +
   "wod.creationDate >= :startDate and wod.creationDate <= :endDate order by wod.symbol ")


Code:
@Table(name = "WarehouseOutcomeDoc")
@javax.persistence.Entity
@OnDelete( action = OnDeleteAction.CASCADE)
public class WarehouseOutcomeDocEJB extends DocumentEJB implements WarehouseOutcomeDoc {
   private String warehouseName;

  @Column(name="warehouse_name");
  public String getWarehouseName(){
    return this.wareouseName;
  }

  public void setWarehouseName(String warehouseName){
    this.warehouseName = warehouseName;
  }
}

@Table(name = "Document")
@javax.persistence.Entity
@Inheritance( strategy = InheritanceType.JOINED)
public abstract class DocumentEJB extends implements Document {
  private Integer id;
  private String symbol;
  private DocumentEJB baseDocument;

     @ManyToOne(cascade = {}, fetch = FetchType.LAZY)
   @JoinColumn(name = "baseDocumentId", updatable=true)
   public DocumentEJB getBaseDocument()   {
      return baseDocument;
   }
   
   public void setBaseDocument( Document baseDocument)   {
      this.baseDocument = (DocumentEJB) baseDocument;
   }

   @Column(name = "symbol", updatable = true, unique = true)
   public String getSymbol() {
      return symbol;
   }

        public void setSymbol(String symbol){
           this.symbol = symbol;
        }
   @Id
   @GeneratedValue( strategy = GenerationType.AUTO)
   public Integer getId() {
      return this.id;
   }

   public void setId( Integer id) {
      this.id = id;
   }

}

@Table(name = "InternalRequest")
@javax.persistence.Entity
@OnDelete(action = OnDeleteAction.CASCADE)
public class InternalRequestEJB extends DocumentEJB implements InternalRequest {

   private String clientRequestSymbol;
   public String getClientRequestSymbol() {
      return this.clientRequestSymbol;
   }

   public void setClientRequestSymbol(String clientRequestSymbol) {
      this.clientRequestSymbol = clientRequestSymbol;
   }

}



and every WarehouseDocument has set as baseDocument InternalRequest and when i try to retrive
clientRequestSymbol from baseDocument of WarehouseOutcomeDocEJB i get

Code:
15:36:38,157 ERROR [JDBCExceptionReporter] Unknown column 'documentej2_4_.clientRequestSymbol' in 'field list'
15:36:38,158 INFO  [STDOUT] javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
15:36:38,158 INFO  [STDOUT]     at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:562)
15:36:38,158 INFO  [STDOUT]     at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:56)
15:36:38,158 INFO  [STDOUT]     at pl.wroc.si.baks.session.WarehouseServiceEJB.getWarehouseOutcomeDoc(WarehouseServiceEJB.java:197)
15:36:38,158 INFO  [STDOUT]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
15:36:38,159 INFO  [STDOUT]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
15:36:38,159 INFO  [STDOUT]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
15:36:38,159 INFO  [STDOUT]     at java.lang.reflect.Method.invoke(Method.java:585)
15:36:38,159 INFO  [STDOUT]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:109)
15:36:38,159 INFO  [STDOUT]     at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.ja
va:54)
15:36:38,159 INFO  [STDOUT]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)


whene i replace clientRequestSymbol with symbol i works fine...
Code:
"select " +
   "wod.id, " +
   "wod.symbol, " +
        "wod.warehouseName, "+
   "wod.creationDate, " +
   "docIr.symbol, " +
"from " +
   "WarehouseOutcomeDocEJB wod left join wod.baseDocument as docIr " +
"where " +
   "wod.creationDate >= :startDate and wod.creationDate <= :endDate order by wod.symbol ")


it looks for me that hibernate do not know that baseDocument has clientRequestSymbol field because it map on DocumentEJB so is there a way to cast it on InternalRequest in hql ?


Top
 Profile  
 
 Post subject: Re: How to use projection with inheritance
PostPosted: Mon Nov 23, 2009 10:50 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
In your query you refer to docIr, which is of type DocumentEJB. DocumentEJB has no attribute clientRequestSymbol.

I think your object model is inappropriate, what are you trying to do?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: How to use projection with inheritance
PostPosted: Mon Nov 23, 2009 10:52 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
pionier wrote:
it looks for me that hibernate do not know that baseDocument has clientRequestSymbol field because it map on DocumentEJB so is there a way to cast it on InternalRequest in hql ?


Why don't you set baseDocument to type InternalRequestEJB?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: How to use projection with inheritance
PostPosted: Mon Nov 23, 2009 11:03 am 
Newbie

Joined: Fri Nov 20, 2009 9:27 am
Posts: 7
mmerder wrote:
pionier wrote:
it looks for me that hibernate do not know that baseDocument has clientRequestSymbol field because it map on DocumentEJB so is there a way to cast it on InternalRequest in hql ?


Why don't you set baseDocument to type InternalRequestEJB?


because there is some other classes that inherit from DocumentEJB like Invoice or WarehouseIncome that has other classes as baseDocument.


Top
 Profile  
 
 Post subject: Re: How to use projection with inheritance
PostPosted: Mon Nov 23, 2009 11:09 am 
Newbie

Joined: Fri Nov 20, 2009 9:27 am
Posts: 7
ie
InternalRequest -> base document Null
WarehouseOutcome -> baseDocument InternalRequest
Invoice -> baseDocument WarehouseOutcome
CorrectionInvoice -> baseDocument Invoice

and there r more combination like this
thats why i need to resolve this problem

exelent solution base on Hibernate manual
Quote:
Hibernate uses a powerful query language (HQL) that is similar in appearance to SQL. Compared with SQL, however, HQL is fully object-oriented and understands notions like inheritance, polymorphism and association.


is to cast baseDocument to a class i need in hql. Is there a way to do that? or can i do it in another way?


Top
 Profile  
 
 Post subject: Re: How to use projection with inheritance
PostPosted: Mon Nov 23, 2009 11:15 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Well, then you should consider not having a base document in your rootclass, but the specific subclass as base document in every subclass.

Or your should extract the specific properties of your base document and store these in the relevant subclasses, which need it.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: How to use projection with inheritance
PostPosted: Mon Nov 23, 2009 11:18 am 
Newbie

Joined: Fri Nov 20, 2009 9:27 am
Posts: 7
it is too late to reimplement whole system :) it is working for few yers...
i cant redesign whole database and reassign whole data...
that's whay im asking for help how to resolve this issiu;(


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.