-->
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.  [ 11 posts ] 
Author Message
 Post subject: Mapping Many-to-many to a read only view
PostPosted: Wed Dec 31, 2003 9:57 pm 
Newbie

Joined: Wed Dec 31, 2003 9:45 pm
Posts: 19
I have objects A, B, and C. Currently in the database, A is linked to B with a many-to-many relationship using table ABLINK. B is linked to C with a many-to-many relationship using table BCLINK. I tried to create a read only view in the database, ACVIEW, which JOINs the ABLINK and BCLINK tables to dynamically create the view. I put ACVIEW in a many-to-many definition in Hibernate like:

<class name="A" table="A">
<id name="id" type="string" unsaved-value="null">
<column name="id" sql-type="varchar(20)" not-null="true"/>
<generator class="assigned"/>
</id>
<set name="bvalues" table="ABLINK" cascade="save-update">
<key column="id"/>
<many-to-many column="bid" class="B"/>
</set>

<set name="cvalues" table="ACVIEW" cascade="none">
<key column="id"/>
<many-to-many column="cid" class="C"/>
</set>

When I get to the set bvalues, it seems to work fine, but on cvalues, I get the error:

net.sf.hibernate.JDBCException: could not initialize collection: A.values#1 ]

How can I tell it not to initialize the collection? I saw an update=false parameter for other relationships, but not many-to-many.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 01, 2004 8:10 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Can you please show some more detailed log extract, and more details about what exactly you are doing in your code? Best would be the part between session open() and close().


Top
 Profile  
 
 Post subject: Code sample
PostPosted: Thu Jan 01, 2004 9:52 pm 
Newbie

Joined: Wed Dec 31, 2003 9:45 pm
Posts: 19
I have a small framework to facade the Hibernate calls (and exceptions) from the rest of my code. The exception is thrown in the following code after the first call to getAll(new A());. No other Hibernate calls have been done at this point except for the creation of the session, and I ran fine before I added the View to the equation (at least there wasn't an exception thrown). I'm still absorbing why Hibernate wants to write to the database on a request for data anyway, but my current question is just how to make the link between A and C be read only so Hibernate never tries to write to it.

public ArrayList getAll(Object vo) {
Transaction tx = null;
ArrayList returnList = null;
logger.debug("getAll");
try {
tx = session.beginTransaction();
Criteria crit = session.createCriteria(vo.getClass());
returnList = (ArrayList)crit.list();
}
catch (HibernateException e){
logger.debug("Hibernate exception doing getAll:"+e);
}
catch (Exception e){
logger.debug("Unknown exception found doing getAll: "+e);
}
finally {
try {
tx.commit();
}
catch (HibernateException e){
logger.debug("Error closing Hibernate transaction in getAll: "+e);
}
}
return returnList;
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 01, 2004 10:00 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Hm, I am pretty sure hibernate does not do any insert or update operations unless you change the collection. Can you really find generated INSERT or UPDATE statements in hibernates SQL output? Please enable logging and post the relevant log excerpts, I am pretty sure this is an other error of some kind.


Top
 Profile  
 
 Post subject: Updates
PostPosted: Fri Jan 02, 2004 12:24 am 
Newbie

Joined: Wed Dec 31, 2003 9:45 pm
Posts: 19
Its getting late for me tonight, so I'll get some better log excerpts to you tomorrow, but I thought I read somewhere that it needs to reach out to the database for something for collections. I noticed that even when things otherwise appear to work fine for non-View tables, it says its deleting all the related collections for the first record of whatever table I try to read. It says something in the Hibernate method name about 'Unreachable', but I don't know how these could be unreachable, and it only happens on one element of each table.


Top
 Profile  
 
 Post subject: Read only
PostPosted: Fri Jan 02, 2004 1:32 am 
Newbie

Joined: Wed Dec 31, 2003 9:45 pm
Posts: 19
Actually, I don't want you to waste time debugging why I'm writing when I shouldn't so much as I want to know how to tell Hibernate to make a given many-to-many relationship totally read only. I want to tell Hibernate to pull data from the relationship, but never to update it - the database will do that itself based on other relationships. I think I see how to do this for other relationships, but I don't see a way to say <update>false</update> on a many-to-many relationship. Any thoughts?

Again the problem is:

Given classes/tables A,B,C:
A is M-M with B and B is M-M with C in the database. I want to create a read-only view for the A to C M-M link, and tell Hibernate not to manage this link, but only to read from it to populate the M-M Set in A and C.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2004 9:49 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
What you want to do is not a "classical" mapping. Try to set the many-to-many cascade="none" and inverse="true"

I would solve that by a HQL rather than breaking th"beauty" of my OO model.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2004 9:51 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hava a look at http://www.hibernate.org/Documentation/InsideExplanationOfInverseTrue

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Inverse="False"
PostPosted: Fri Jan 02, 2004 7:13 pm 
Newbie

Joined: Wed Dec 31, 2003 9:45 pm
Posts: 19
I tried inverse=false on both ends of the association, but I still got an error writing to the read only view, but I need to test a little more before posting the details here. Let me look again at that.

I agree that this breaks the ideal model. I think my scenario is kind of a special case. I have small user load and total data, but quite complex business rules. By including just a couple of these views, I can eliminate all SQL and all HQL from the application. This reduces the learning my business and presentation tier people have to do with databases, at the cost of reducing technical elegance a bit. You'd never want to do this with a large amount of data, but here, I'm willing to take a small performance hit.


Top
 Profile  
 
 Post subject: Re: Inverse="False"
PostPosted: Fri Jan 02, 2004 7:15 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
keegan wrote:
I tried inverse=false on both ends of the association, but I still got an error writing to the read only view, but I need to test a little more before posting the details here. Let me look again at that.

That's why I told inverse="true" since this means the other side manage the association link.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: inverse="true"
PostPosted: Fri Jan 02, 2004 9:13 pm 
Newbie

Joined: Wed Dec 31, 2003 9:45 pm
Posts: 19
Yes, sorry, I believe I tried that, but let me do more testing before reposting my question.


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