-->
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.  [ 5 posts ] 
Author Message
 Post subject: entity bean based on two tables. is this possible?
PostPosted: Tue Jul 19, 2005 5:07 am 
Beginner
Beginner

Joined: Mon Jun 06, 2005 4:23 am
Posts: 41
Location: Hagenberg, AUSTRIA
hi!

i have to reengineer an old legacy application. its database tables were designed in a very bad way without relations. the logic to save consistent data is completely held in the application's code.

now to my problem:

i have two tables (let's call them t1 and t2) with exactly the same columns. now i want hibernate to merge the tables so i can work with one data-object.

in the hibernate documentation (http://www.hibernate.org/hib_docs/annot ... ping-query) i read about the possibility of mapping queries.

i tried the following example with adjustments to my needs:
Code:
@Entity
@SqlResultSetMapping(name="implicit", entities=@EntityResult(name="org.hibernate.test.annotations.query.SpaceShip"))
@NamedNativeQuery(name="implicitSample", queryString="select * from SpaceShip", resultSetMapping="implicit")
public class SpaceShip {
...
}



adjustments:
Code:
@Entity
@SqlResultSetMapping(name="test", entities=@EntityResult(name="ams.ingres.model.Akten"))
@NamedNativeQuery(name="testSample", queryString="select * from t1, t2", resultSetMapping="test")
public class Akten {
...
}



now my question: is it possible to create an ejb3.0 entity bean based on an sql-query which merges two tables?

i had no success doing this. i'm getting an error when using the @Entity annotation because hibernate tells me, that the data-object which i'm using is not bound to a database-table.

in my case hibernate should act like this:

- merge the two tables
- save it in one data-object (ejb3.0 entity bean)
- when changing a value, hibernate should recognize which table is affected and save the value back to the right table.

best regards
patrik

ps.: my system environment
eclipse 3.1final
jboss4.0.3RC1
jdk1.5update3

Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.1alpha

Mapping documents:
none
Code between sessionFactory.openSession() and session.close():
none
Full stack trace of any exception that occurs:
Code:
08:31:52,148 WARN  [JDBCExceptionReporter] SQL Error: 2117, SQLState: 42500
08:31:52,148 ERROR [JDBCExceptionReporter] Table 'akten' does not exist or is not owned by you.
08:31:52,158 INFO  [STDOUT] org.hibernate.exception.SQLGrammarException: could not execute query
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.loader.Loader.doList(Loader.java:1861)
   at org.hibernate.loader.Loader.list(Loader.java:1842)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:407)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:273)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:850)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
   at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:41)
   at ams.ingres.model.facade.AkteFacade.getAkten(AkteFacade.java:77)
   at ams.ingres.model.facade.AkteFacade.getAllAkten(AkteFacade.java:67)
   at ams.beans.stateless.AktenBean.getAllAkten(AktenBean.java:18)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:99)
   at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:33)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
   at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:66)
   at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
   at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:72)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
   at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:39)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
   at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
   at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:93)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
   at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:148)
   at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:65)
   at $Proxy63.getAllAkten(Unknown Source)
   at ams.web.client.DipasNgHibernateServlet.doPost(DipasNgHibernateServlet.java:58)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
   at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
   at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
   at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
   at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
   at java.lang.Thread.run(Thread.java:595)
Caused by: ca.edbc.util.EdbcEx: Table 'akten' does not exist or is not owned by you.
   at ca.edbc.jdbc.EdbcObj.readResults(EdbcObj.java:782)
   at ca.edbc.jdbc.EdbcPrep.prepare(EdbcPrep.java:668)
   at ca.edbc.jdbc.EdbcPrep.<init>(EdbcPrep.java:310)
   at ca.edbc.jdbc.EdbcConnect.prepareStatement(EdbcConnect.java:1120)
   at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection.prepareStatement(BaseWrapperManagedConnection.java:360)
   at org.jboss.resource.adapter.jdbc.WrappedConnection.prepareStatement(WrappedConnection.java:205)
   at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:395)
   at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:334)
   at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:88)
   at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1426)
   at org.hibernate.loader.Loader.doQuery(Loader.java:637)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:221)
   at org.hibernate.loader.Loader.doList(Loader.java:1858)
   ... 52 more


it's clear for me that this exception occurs, because the table is not in the database. how can i avoid this without creating the database table?

Name and version of the database you are using:
INGRES, version: II 2.6/0207 (int.w32/02)

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 12:43 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you basically want a SQL UNION or a join?
If a join you can use the @SecondaryTable annotation
For a union it's a bit trickier. You need to have a unique id to identifies rows even between the 2 tables. You may try to used the <loader> element.
You cannot do that so far with the annotations

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 21, 2005 3:01 am 
Beginner
Beginner

Joined: Mon Jun 06, 2005 4:23 am
Posts: 41
Location: Hagenberg, AUSTRIA
thanks for you reply.

i want a SQL UNION of two tables and i'd like to work further with one POJO.

when updating/deleting, hibernate should check which table is affected and do the correct operations.

where do i find infos about the <loader> element?

regards
patrik


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 21, 2005 10:54 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 2:46 pm
Posts: 45
Location: Saskatoon SK Canada
MistaMoasn wrote:
i want a SQL UNION of two tables and i'd like to work further with one POJO.

when updating/deleting, hibernate should check which table is affected and do the correct operations.


Is there any way you can do this in the database itself? I don't know anything about Ingres, but in Oracle you would be able to create a view that performs the UNION, and use INSTEAD OF triggers on the view to code the proper insert, update or delete behaviour.

_________________
Maury


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 21, 2005 5:59 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
MistaMoasn wrote:
when updating/deleting, hibernate should check which table is affected and do the correct operations.

I didn't spend much time on this, but I don't see any good way to do that in 1 POJO, unless you actially use a POJO superclass and 2 subclasses (1 for each table), then map the 2 subclasses as union subclasses, Since Hibernate has polymorphism capabilities, you can work on the superclass transparently using queries.

MistaMoasn wrote:
where do i find infos about the <loader> element?

In the doc

I like the idea of a view in the DB though.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.