-->
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.  [ 3 posts ] 
Author Message
 Post subject: Create a new view
PostPosted: Fri Nov 04, 2005 2:30 pm 
Newbie

Joined: Fri Nov 04, 2005 2:05 pm
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0.5

Name and version of the database you are using:
Oracle 8.1.7.0.0

Is there any way to do something like what the (incorrect) HQL below does to create a "view"?

Code:
select new org.mytest.MyView(first,second)
  from FirstTable as first
  left join SecondTable as second
  where first.property=second.property


MyView class must be like that:
Code:
class MyView {
  FirstTable first;
  SecondTable second;
  ...
}

- not just contain some properties from 'first' and 'second' like after <subselect>.

Thanks,
mp


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 04, 2005 8:14 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
try with native sql (chapter 16 in doc)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 11:28 am 
Newbie

Joined: Fri Nov 04, 2005 2:05 pm
Posts: 2
Let me ask another way: is it possible to modify a HelperView's mapping and have a working ViewTest?

Thanks,
mp

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hqltest" schema="SCH">
   <class name="FirstTbl" table="FIRST">
      <id name="id" type="string" column="ID">
         <generator class="uuid.hex"/>
      </id>
       <property name="dat" type="string" column="DAT"/>
   </class>

   <class name="SecondTbl" table="SECOND">
      <id name="id" type="string" column="ID">
         <generator class="uuid.hex"/>
      </id>
       <property name="dat" type="string" column="DAT"/>
   </class>

   <class name="HelperView" mutable="false">
      <!--
      <subselect>
      select first,second
      from FirstTbl as first, SecondTbl as second
      </subselect>
      -->
      <synchronize table="FirstTbl"/>
      <synchronize table="SecondTbl"/>
      <id name="id" type="string" column="ID">
         <generator class="uuid.hex"/>
      </id>
       <property name="first" type="hqltest.FirstTbl"/>
       <property name="second" type="hqltest.SecondTbl"/>
       
       <loader query-ref="helper"/>
   </class>
   
   <sql-query name="helper">
      <return alias="hv" class="HelperView"/>
      <return-join alias="fst" property="hv.first"/>
      <return-join alias="snd" property="hv.second"/>
      SELECT {fst.*}, {snd.*}
      FROM FIRST as fst, SECOND as snd
   </sql-query>
</hibernate-mapping>


FirstTbl, SecondTbl an HelperView are generated from the HBM.XML file above.

Code:
// View.java
package hqltest;

public class View implements java.io.Serializable {
   private FirstTbl first;
   private SecondTbl second;
   
   public View() {}

   public View(FirstTbl first, SecondTbl second) {
      this.first = first;
      this.second = second;
   }

   public FirstTbl getFirst() {
      return first;
   }

   public void setFirst(FirstTbl first) {
      this.first = first;
   }

   public SecondTbl getSecond() {
      return second;
   }

   public void setSecond(SecondTbl second) {
      this.second = second;
   }
}


Code:
package hqltest;

import java.util.*;
import junit.framework.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.apache.log4j.Logger;

public class ViewTest extends TestCase {   
    protected static Logger log = Logger.getLogger(ViewTest.class);

    public ViewTest(String str) {
       super(str);
    }
   protected void setUp() throws Exception {
      super.setUp();
   }
   protected void tearDown() throws Exception {
      super.tearDown();
   }
   public static TestSuite suite() {
      TestSuite suite = new TestSuite();
      suite.addTest(new ViewTest("testView"));
      return suite;
   }
   public static void main(String[] args) {
      junit.textui.TestRunner.run(suite());
   }


   public void testView() {
      Configuration configuration = new Configuration();
                configuration.configure();
                Session s = configuration.buildSessionFactory().openSession();
      Transaction t = s.beginTransaction();
      String hql =
         //"select new hqltest.View(f,s)"
         //+" from FirstTbl as f, SecondTbl as s"
         //+" where f.dat=s.dat"
                        // -- works

         "select new hqltest.View(f,s)"
         +" from HelperView hrv"
         +" join hrv.first as f"
         +" left outer join hrv.second as s"
         // a lot of hql should be inserted here
         ;
      List list = s.createQuery(hql).list();
      log.warn("-- "+list.size());
      t.commit();
      s.close();
   }
}


.10:07:57,031 INFO Environment: Hibernate 3.0.5
10:07:57,031 INFO Environment: hibernate.properties not found
10:07:57,046 INFO Environment: using CGLIB reflection optimizer
10:07:57,046 INFO Environment: using JDK 1.4 java.sql.Timestamp handling
10:07:57,109 INFO Configuration: configuring from resource: /hibernate.cfg.xml
10:07:57,109 INFO Configuration: Configuration resource: /hibernate.cfg.xml
10:07:57,437 INFO Configuration: Mapping resource: hqltest/All.hbm.xml
10:07:57,562 INFO HbmBinder: Mapping class: hqltest.FirstTbl -> FIRST
10:07:57,578 INFO HbmBinder: Mapping class: hqltest.SecondTbl -> SECOND
10:07:57,578 INFO HbmBinder: Mapping class: hqltest.HelperView -> HelperView
10:07:57,578 INFO Configuration: Configured SessionFactory: null
10:07:57,593 INFO Configuration: processing extends queue
10:07:57,593 INFO Configuration: processing collection mappings
10:07:57,593 INFO Configuration: processing association property references
10:07:57,593 INFO Configuration: processing foreign key constraints
10:07:57,671 INFO DriverManagerConnectionProvider: Using Hibernate built-in connection pool (not for production use!)
10:07:57,671 INFO DriverManagerConnectionProvider: Hibernate connection pool size: 1
10:07:57,671 INFO DriverManagerConnectionProvider: autocommit mode: false
10:07:57,671 INFO DriverManagerConnectionProvider: using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:file:/eclipse31hib/ep/imstest/db/sch
10:07:57,671 INFO DriverManagerConnectionProvider: connection properties: {user=sa}
10:07:58,093 INFO SettingsFactory: RDBMS: HSQL Database Engine, version: 1.8.0
10:07:58,093 INFO SettingsFactory: JDBC driver: HSQL Database Engine Driver, version: 1.8.0
10:07:58,125 INFO Dialect: Using dialect: org.hibernate.dialect.HSQLDialect
10:07:58,125 INFO TransactionFactoryFactory: Using default transaction strategy (direct JDBC transactions)
10:07:58,125 INFO TransactionManagerLookupFactory: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
10:07:58,125 INFO SettingsFactory: Automatic flush during beforeCompletion(): disabled
10:07:58,125 INFO SettingsFactory: Automatic session close at end of transaction: disabled
10:07:58,125 INFO SettingsFactory: JDBC batch size: 15
10:07:58,125 INFO SettingsFactory: JDBC batch updates for versioned data: disabled
10:07:58,125 INFO SettingsFactory: Scrollable result sets: enabled
10:07:58,125 INFO SettingsFactory: JDBC3 getGeneratedKeys(): disabled
10:07:58,125 INFO SettingsFactory: Connection release mode: null
10:07:58,125 INFO SettingsFactory: Default batch fetch size: 1
10:07:58,125 INFO SettingsFactory: Generate SQL with comments: disabled
10:07:58,125 INFO SettingsFactory: Order SQL updates by primary key: disabled
10:07:58,125 INFO SettingsFactory: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
10:07:58,140 INFO ASTQueryTranslatorFactory: Using ASTQueryTranslatorFactory
10:07:58,140 INFO SettingsFactory: Query language substitutions: {}
10:07:58,140 INFO SettingsFactory: Second-level cache: enabled
10:07:58,140 INFO SettingsFactory: Query cache: disabled
10:07:58,140 INFO SettingsFactory: Cache provider: org.hibernate.cache.EhCacheProvider
10:07:58,140 INFO SettingsFactory: Optimize cache for minimal puts: disabled
10:07:58,140 INFO SettingsFactory: Structured second-level cache entries: disabled
10:07:58,140 INFO SettingsFactory: Statistics: disabled
10:07:58,140 INFO SettingsFactory: Deleted entity synthetic identifier rollback: disabled
10:07:58,140 INFO SettingsFactory: Default entity-mode: pojo
10:07:58,265 INFO SessionFactoryImpl: building session factory
10:07:58,281 WARN Configurator: No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/javautil/hibernate-3.0/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
10:07:58,640 INFO SessionFactoryObjectFactory: Not binding factory to JNDI, no JNDI name configured
10:07:58,640 INFO SessionFactoryImpl: Checking 0 named queries
E
Time: 1.844
There was 1 error:
1) testView(hqltest.ViewTest)java.lang.NullPointerException
at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:264)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3022)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:2841)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2719)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:513)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:371)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:201)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:151)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:834)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at hqltest.ViewTest.testView(ViewTest.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at hqltest.ViewTest.main(ViewTest.java:29)

FAILURES!!!
Tests run: 1, Failures: 0, Errors: 1


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