-->
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: Tricky Schema
PostPosted: Mon Jun 20, 2005 9:50 am 
Regular
Regular

Joined: Thu Sep 23, 2004 11:53 am
Posts: 83
Hibernate version:
3.0
Mapping documents:
na
Code between sessionFactory.openSession() and session.close():
na
Full stack trace of any exception that occurs:
na
Name and version of the database you are using:
Oracle 10g
The generated SQL (show_sql=true):
na
Debug level Hibernate log excerpt:
na

Was wondering if there is a built in way to handle the following in Hibernate. The schema I am working with defines a table with 3 columns; Id, Name and LocationType. The value of LocationType is the table name that describes a particualr type of location. For instance there are several location tables with different schemas, and the LocationType column holds the name of the table for each ids location type. I have never run across a schema like this before and not sure what is the best approach for retrieving the id + location from the database. Currently, I query the first table using the id; then use the location type (table name) and execute a second query. I thought there might be a a way in hibernate to define some sort of association between the LocationType column values (values meaning table names) and the assoaicted table.

Thanks for any help/ideas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 5:39 pm 
Regular
Regular

Joined: Thu Sep 23, 2004 11:53 am
Posts: 83
Thought an example might help...

I have table Building with columns id, name, and location_type. The location_type column contains the location's table name. Currently I am querying the Building table and then creating another query using the value retrieved from the location_type column. I looked into the inheritence "Table per subclass, using a discriminator", but whe I try to create the java classes for the mapping below I get the following exception...


Code:
[hibernatetool] org.hibernate.MappingException: class com.model.Local not found while looking for pr
operty: name

Code:
<hibernate-mapping package="com.model">

   <class name="Building" table="building">
      <id name="id" column="id" type="integer">
         <generator class="assigned" />
      </id>
      <discriminator column="location_type" type="string"/>
      <property name="name" column="name" type="string"
         not-null="true" />
          <subclass name="Local" discriminator-value="local">
              <join table="local">
                 <key column="id"/>
            <property name="name" column="name"/>
            </join>
      </subclass>
          <subclass name="National" discriminator-value="national">
              <join table="national">
                 <key column="id"/>
            <property name="name" column="name"/>
            </join>
      </subclass>
   </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 8:06 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
were you not using hibernate, how would you write that query in one statement? TTBOMK, its not possible to do this in PLSQL:

Code:
  Select * from (select location from refTable where name = 'foo');

You would have to do:

Code:
  select location into locationName from refTable where name = 'foo';
  execute immediate "select * from " || locationName;


Which is what you are doing now.

So if there is no way to do it in oracle native, probably no way to do it with Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 26, 2005 3:59 pm 
Newbie

Joined: Mon May 30, 2005 10:26 am
Posts: 13
Right, there's no faster way to do it than with 2 selects. But check chapter 6.1.22 in the manual for saving a few lines of code (provided that your LocationType has a static range of values).


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 26, 2005 8:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Isn't this just an <any> mapping?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 12:42 am 
Regular
Regular

Joined: Thu Sep 23, 2004 11:53 am
Posts: 83
Thanks for the replies I think the <any> element is what I am looking for. To clear up my explanation I was not using Oralce specific queries, I was executing one query to get the location_type. Then I would retrieve the appropriate query from a XML resource file and execute that to get the data I needed.

Thanks Again
J


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 3:37 pm 
Regular
Regular

Joined: Thu Sep 23, 2004 11:53 am
Posts: 83
Issue with <any>, same schema as above. I am recieving the following exception...

database MySQL (for this simple test I am using MySQL). so we have...

Code:
<any name="loctype" id-type="string" insert="false" update="false">
         <meta-value value="LOCAL" class="Local" />
         <meta-value value="INTERNATIONAL" class="International" />
         <column name="loctype" />
         <column name="id" />
      </any>


When I change the loctype column value (databse value not mapping value) to "com.model.Local" it seems to work. I don't think that this was the intended use of <any> as the database would have to contain the classpath of the entity. Is Hibernate even using the value and class attribute values? In fact I can change the value and class attributes to anything and as long as the value in the database is "com.model.Local" Hibernate does not throw an exception and returns the correct object.

Exception
Code:
This is a snippet of the exception, see below for the entire log print out.

org.hibernate.MappingException: Unknown entity: LOCAL
   at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:569)
   at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:62)
   at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:655)
   at org.hibernate.type.AnyType.resolveAny(AnyType.java:111)
   at org.hibernate.type.AnyType.resolve(AnyType.java:101)
   at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:105)
   at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:530)
   at org.hibernate.loader.Loader.doQuery(Loader.java:436)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
   at org.hibernate.loader.Loader.doList(Loader.java:1593)
   at org.hibernate.loader.Loader.list(Loader.java:1577)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:395)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:271)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:844)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
   at com.test.Main.main(Main.java:21)




SQL Schemas
Code:
Removed columns not pertaining to problem, but local and international schemas are different.

Table Building (
pk(id);
varchar name;
varchar loctype;
)

Table Local (
pk(id);
varchar name;
)

Table International (
pk(id);
varchar name;
)



hibernate.cfg.xml
Code:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration.                   -->
<hibernate-configuration>
   <session-factory>
      <!-- properties -->
      <property name="connection.username">root</property>
      <property name="connection.url">jdbc:mysql://localhost:3306/acme</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="connection.password">root</property>
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="show_sql">true</property>

      <mapping resource="com/model/Building.hbm.xml"/>
      <mapping resource="com/model/Local.hbm.xml"/>
      <mapping resource="com/model/International.hbm.xml"/>      

   </session-factory>
</hibernate-configuration>


Building.hbm.xml
Code:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.model">

    <class name="Building" table="building">
        <id name="id" column="id" type="integer">
            <generator class="assigned"/>
        </id>

        <property name="name" column="name" type="string"  not-null="true" />
         
      <any name="loctype" id-type="string" insert="false" update="false">
         <meta-value value="LOCAL" class="Local" />
         <meta-value value="INTERNATIONAL" class="International" />
         <column name="loctype" />
         <column name="id" />
      </any>
    </class>
   
</hibernate-mapping>


Local.hbm.xml
Code:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.model">

    <class name="Local" table="local">
        <id name="building" column="id" type="integer">
            <generator class="assigned"/>
        </id>

        <property name="name" column="name" type="string"  not-null="true" />
    </class>
   
</hibernate-mapping>


Logging
Code:
13:54:46,343  INFO Environment:464 - Hibernate 3.0.5
13:54:46,343  INFO Environment:477 - hibernate.properties not found
13:54:46,359  INFO Environment:510 - using CGLIB reflection optimizer
13:54:46,359  INFO Environment:540 - using JDK 1.4 java.sql.Timestamp handling
13:54:46,437  INFO Configuration:1110 - configuring from resource: /hibernate.cfg.xml
13:54:46,437  INFO Configuration:1081 - Configuration resource: /hibernate.cfg.xml
13:54:46,765  INFO Configuration:444 - Mapping resource: com/model/Building.hbm.xml
13:54:46,890  INFO HbmBinder:260 - Mapping class: com.model.Building -> building
13:54:46,906  INFO Configuration:444 - Mapping resource: com/model/Local.hbm.xml
13:54:46,937  INFO HbmBinder:260 - Mapping class: com.model.Local -> local
13:54:46,937  INFO Configuration:444 - Mapping resource: com/model/International.hbm.xml
13:54:46,968  INFO HbmBinder:260 - Mapping class: com.model.International -> international
13:54:46,968  INFO Configuration:1222 - Configured SessionFactory: null
13:54:46,968  INFO Configuration:875 - processing extends queue
13:54:46,984  INFO Configuration:879 - processing collection mappings
13:54:46,984  INFO Configuration:888 - processing association property references
13:54:46,984  INFO Configuration:917 - processing foreign key constraints
13:54:47,062  INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
13:54:47,062  INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20
13:54:47,062  INFO DriverManagerConnectionProvider:45 - autocommit mode: false
13:54:47,078  INFO DriverManagerConnectionProvider:80 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/acme
13:54:47,078  INFO DriverManagerConnectionProvider:86 - connection properties: {user=root, password=****}
13:54:47,328  INFO SettingsFactory:77 - RDBMS: MySQL, version: 4.1.10a-nt
13:54:47,328  INFO SettingsFactory:78 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.10 ( $Date: 2005/05/19 15:52:23 $, $Revision: 1.1.2.2 $ )
13:54:47,359  INFO Dialect:92 - Using dialect: org.hibernate.dialect.MySQLDialect
13:54:47,359  INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
13:54:47,375  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
13:54:47,375  INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
13:54:47,375  INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
13:54:47,375  INFO SettingsFactory:136 - JDBC batch size: 15
13:54:47,375  INFO SettingsFactory:139 - JDBC batch updates for versioned data: disabled
13:54:47,375  INFO SettingsFactory:144 - Scrollable result sets: enabled
13:54:47,375  INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): enabled
13:54:47,375  INFO SettingsFactory:160 - Connection release mode: null
13:54:47,375  INFO SettingsFactory:184 - Maximum outer join fetch depth: 2
13:54:47,375  INFO SettingsFactory:187 - Default batch fetch size: 1
13:54:47,375  INFO SettingsFactory:191 - Generate SQL with comments: disabled
13:54:47,375  INFO SettingsFactory:195 - Order SQL updates by primary key: disabled
13:54:47,375  INFO SettingsFactory:334 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
13:54:47,375  INFO ASTQueryTranslatorFactory:21 - Using ASTQueryTranslatorFactory
13:54:47,375  INFO SettingsFactory:203 - Query language substitutions: {}
13:54:47,375  INFO SettingsFactory:209 - Second-level cache: enabled
13:54:47,375  INFO SettingsFactory:213 - Query cache: disabled
13:54:47,375  INFO SettingsFactory:321 - Cache provider: org.hibernate.cache.EhCacheProvider
13:54:47,390  INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled
13:54:47,390  INFO SettingsFactory:237 - Structured second-level cache entries: disabled
13:54:47,390  INFO SettingsFactory:257 - Echoing all SQL to stdout
13:54:47,390  INFO SettingsFactory:261 - Statistics: disabled
13:54:47,390  INFO SettingsFactory:265 - Deleted entity synthetic identifier rollback: disabled
13:54:47,390  INFO SettingsFactory:279 - Default entity-mode: pojo
13:54:47,531  INFO SessionFactoryImpl:152 - building session factory
13:54:47,546  WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/eclipse/workspace/Hibernate/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
13:54:47,921  INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
13:54:47,921  INFO SessionFactoryImpl:379 - Checking 0 named queries
Hibernate: select building0_.id as id, building0_.name as name0_, building0_.loctype as loctype0_, building0_.id as id0_ from building building0_
org.hibernate.MappingException: Unknown entity: LOCAL
   at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:569)
   at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:62)
   at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:655)
   at org.hibernate.type.AnyType.resolveAny(AnyType.java:111)
   at org.hibernate.type.AnyType.resolve(AnyType.java:101)
   at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:105)
   at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:530)
   at org.hibernate.loader.Loader.doQuery(Loader.java:436)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
   at org.hibernate.loader.Loader.doList(Loader.java:1593)
   at org.hibernate.loader.Loader.list(Loader.java:1577)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:395)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:271)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:844)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
   at com.test.Main.main(Main.java:21)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 5:04 am 
Newbie

Joined: Mon May 30, 2005 10:26 am
Posts: 13
Perhaps that's the default behaviour when you don't specify the "meta-type" attribute of <any>.

My proposal goes like this:
Code:
<any (!!!)name="location" id-type="string" (!!!)meta-type="string" insert="false" update="false">
         <meta-value value="LOCAL" class="Local" />
         <meta-value value="INTERNATIONAL" class="International" />
         <column name="loctype" />
         <column name="id" />
      </any>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 10:12 am 
Regular
Regular

Joined: Thu Sep 23, 2004 11:53 am
Posts: 83
The default according to the DTD is Hibernate.STRING, but when I added what acarstoiu mentioned above it worked. This is what the 3.0 DTD states ...

Code:
<!ATTLIST any meta-type CDATA #IMPLIED>             <!--- default: Hibernate.STRING -->


Thanks ... again
J


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 2:57 pm 
Regular
Regular

Joined: Thu Sep 23, 2004 11:53 am
Posts: 83
Now when I mark the <any> element as lazy=false Hibernate does not egarly load the International property values.

Code:
      <any name="loctype" id-type="string" meta-type="string" insert="false" update="false" lazy="false">         
         <meta-value value="INTERNATIONAL" class="International" />
         <meta-value value="LOCAL" class="Local" />
         <column name="loctype" />
         <column name="id" />
      </any>


Thanks for any help
J


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.