-->
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.  [ 7 posts ] 
Author Message
 Post subject: Repeated column for index problem...
PostPosted: Wed Nov 19, 2003 3:36 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Okay, I know that the <key> and <index> can not both be the same column and you can't map a clumn twice to two attributes, but, dumb question of the day, why can't I use the ID of a many-to-many as the index of the List that holds them?

My tables are like so:
Code:
user_role { role_id int, role_description varchar }
role_functionality_xref { role_id int, functionality_id int }
functionality { functionality_id int, description varchar }


And my incorrect mapping attempt is:
Code:
   <class name="package.UserRole" table="user_role" mutable="false">
      <id name="ID" type="integer" column="role_id">
         <generator class="net.sf.hibernate.id.IdentityGenerator"/>
      </id>
      <property name="description" column="description" type="string" not-null="true"/>
      <list name="functions" lazy="true" table="role_functionality" cascade="none">
         <key column="role_id"/>
         <index column="functionality_id" type="integer"/>
         <many-to-many class="package.Functionality" column="functionality_id"/>
      </list>
   </class>

   <!-- Functionality -->
   <class name="package.Functionality" table="functionality" mutable="false">
      <id name="ID" type="integer" column="functionality_id">
         <generator class="net.sf.hibernate.id.IdentityGenerator"/>
      </id>
      <property name="description" column="description" type="string" not-null="true"/>
   </class>


I just want a List with NULL values where there are no associated records.
What would be the proper way to accomplish this? I don't really want an extra column with duplicate values of the ID.

If I take the column attribute out of the many-to-many tag I get a stack overflow with no messages when I try to run (Hibernate starts, I just can't login to my app because the first request involves the mapping in question which generates the stack overflow).


Top
 Profile  
 
 Post subject: Urgh
PostPosted: Wed Nov 19, 2003 4:42 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Please ignore the stack overflow portion, I changed something else that caused that and thought it was a problem with my mapping...

It works if I take the column out of the many-to-many, sorry to have posted this waste of time.


Top
 Profile  
 
 Post subject: Hmm, maybe it doesn't work...
PostPosted: Mon Nov 24, 2003 9:26 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Okay, I take back the part about it working...
Upon writing some unit tests I discovered that not all is as I desire it to be.

The mapping doesn't generate an error at startup, however, once an attempt is made to access an instance of Functionality (or call .size() on the collection) and Hibernate tries to initialize the collection of functions I get the following Exception:
Code:
Hibernate: select userrole0_.role_id as role_id0_, userrole0_.role_description as role_des2_0_ from user_role userrole0_ where userrole0_.role_id=?
Hibernate: select role_fun0_.elt as elt__, role_fun0_.role_id as role_id__, role_fun0_.functionality_id as function3___, function1_.functionality_id as functionality_id0_, function1_.description as descript2_0_ from role_functionality role_fun0_ inner join functionality function1_ on role_fun0_.elt=function1_.functionality_id where role_fun0_.role_id=?
WARN  JDBCExceptionReporter:38 - SQL Error: 207, SQLState: S0003
ERROR JDBCExceptionReporter:46 - Invalid column name 'elt'.
WARN  JDBCExceptionReporter:38 - SQL Error: 207, SQLState: S0003
ERROR JDBCExceptionReporter:46 - Invalid column name 'elt'.
ERROR JDBCExceptionReporter:75 - could not initialize collection: [com.fgl.ina.security.UserRole.functions#4]
com.jnetdirect.jsql.v: Invalid column name 'elt'.

   at com.jnetdirect.jsql.v.a(Unknown Source)
   at com.jnetdirect.jsql.av.a(Unknown Source)
   at com.jnetdirect.jsql.JSQLConnection.prePrepare(Unknown Source)
   at com.jnetdirect.jsql.JSQLConnection.getPreparedStatementHandle(Unknown Source)
   at com.jnetdirect.jsql.ap.executeQuery(Unknown Source)
   at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:83)
   at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:790)
   at net.sf.hibernate.loader.Loader.doQuery(Loader.java:184)
   at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:128)
   at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:905)
   at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:880)
   at net.sf.hibernate.loader.CollectionLoader.initialize(CollectionLoader.java:69)
   at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:571)
   at net.sf.hibernate.impl.SessionImpl.initialize(SessionImpl.java:3329)
   at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:199)
   at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:69)
   at net.sf.hibernate.collection.List.size(List.java:78)
   at package.UserRole.canRolePerformFunction(UserRole.java:69)


Where does the 'elt' column come from? Is it generated by Hibernate because I didn't explicitly specify a column in the many-to-many tag? If so how can I get around this (or can I?)?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 24, 2003 9:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Recheck your mappings you are missing a column attribute in a <many-to-many> or <element> tag.


Top
 Profile  
 
 Post subject: I was afraid you were going to say that...
PostPosted: Mon Nov 24, 2003 10:00 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Quote:
...you are missing a column attribute in a <many-to-many>...

That's what I was afraid of.
Okay, then, that brings me full circle back to my original question/problem. Is there any way in which I can use the foreign key column which is the "column" in the many-to-many, as the index of the List?
If not, why not?
I know this starts to look a bit like duplicate column mapping but is it really duplicate? After all, isn't the ID or PK by nature a form of index?

I obviously don't want to make a duplicate column just to have an index mirror the ID and although I can fake it through a view that selects the same column twice I would have problems if I needed to insert new instances.

Thank-you for your help (and product :) ).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 24, 2003 10:10 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No, its not supported. A little bit tricky to implement since the generated SQL looks completely different.

To implement it we would require a new kind of net.sf.hibernate.collection.Bag.


But anyway, this doesn't seem to make much sense in the case of surrogate keys. (it makes more sense for a Map)


Top
 Profile  
 
 Post subject: too bad, it would have been handy if I could
PostPosted: Mon Nov 24, 2003 10:31 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Quote:
...this doesn't seem to make much sense in the case of surrogate keys...


Hmm, I was just trying to find an efficient way of determining the existence of an instance without iterating through the list and comparing the ID's one by one. Granted this is more of a one-off than a standard need.

Quote:
(it makes more sense for a Map)

I would be happy to use a Map instead of a List if I could accomplish the same thing. ;)

Okay, thank-you for your help, I guess I'll have to decide if I want to keep it the way it is and make a view or change to a Set and add a bunch of iteration and if (ID == ?) stuff.


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