-->
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.  [ 13 posts ] 
Author Message
 Post subject: Why does hibernate do inserts when I use a getter
PostPosted: Tue Nov 29, 2005 9:01 am 
Newbie

Joined: Tue Nov 01, 2005 5:39 pm
Posts: 11
Hibernate version: 2

Name and version of the database you are using: MSSQL 4

When I load an object from the db with lazy=false and I loop over that object to display the properties of the inner objects I am seeing show_sql produce delete and then insert statements. Is there any way to turn that off? I just want the data to be read only with out the additional overhead.

Bryan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 9:34 am 
Senior
Senior

Joined: Mon Aug 22, 2005 5:45 am
Posts: 146
My guess is that this is due to some extra config in your mapping class configs. I'd suggest checking your mappings.

_________________
Please don't forget to give credit, if my posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 10:27 am 
Newbie

Joined: Tue Nov 01, 2005 5:39 pm
Posts: 11
I have a pretty simple config for each object. I only have defined the necessary attributes to describe the class and properties. The delete/insert is happening on a many-to-many relation ship I have defined for a java.util.Set made up of a <composit-id>, in other words it deletes all the entries in my reference table and re-inserts them.

Bryan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 10:59 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 4:27 pm
Posts: 40
Location: canada
does this help?

http://www.hibernate.org/116.html#A14


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 11:20 am 
Newbie

Joined: Tue Nov 01, 2005 5:39 pm
Posts: 11
very much so. I implemented the addAll method of a TreeSet to overcome a problem where my collection was only holding one object and the query was return more than one. I will revisit that problem to see if I can figure another way.

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 10:43 am 
Newbie

Joined: Tue Nov 01, 2005 5:39 pm
Posts: 11
I tried to remedy the situation by extending a TreeSet and overriding the equals method. If the object used my hibernate is a Set of the same size I called them equal. I am still getting the delete/inserts. Here is a peak at my config files, tell me if you see anything odd.

The xref table below is the one that gets deleted and re-created.

Code:
<hibernate-mapping package="com.campuspartners.webexit.dao">

    <class name="Exam" table="exam">
        <id name="examid" column="examid" type="java.lang.Integer">
            <generator class="identity"/>
        </id>

        <property name="testType" column="testtype" type="java.lang.String" />
        <property name="name" column="name" type="java.lang.String" />
       
        <set name="TestQuestions" lazy="false" table="examquestionxref">
            <key column="examid"/>
            <many-to-many
               class="com.campuspartners.webexit.dao.TestQuestion"
               column="questionid"/>
        </set>
    </class>
   
</hibernate-mapping>

<hibernate-mapping package="com.campuspartners.webexit.dao">

    <class name="TestQuestion" table="testquestion">
        <id name="questionid" column="QuestionID" type="java.lang.Integer">
            <generator class="identity"/>
        </id>

        <property name="testtype" column="TestType" type="java.lang.String"/>
        <property name="questiontype" column="QuestionType" type="java.lang.String" />
        <property name="contexttext" column="ContextText" type="java.lang.String" />
        <property name="questiontext" column="QuestionText" type="java.lang.String" />
        <property name="questionsequence" column="QuestionSeq" type="java.lang.Short" />
       
        <set name="QuestionChoices" lazy="false" table="questionchoicexref" >
            <key column="questionid"/>
            <many-to-many
               class="com.campuspartners.webexit.dao.QuestionChoice"
               column="choiceid"/>
        </set>
    </class>
   
</hibernate-mapping>

<hibernate-mapping package="com.campuspartners.webexit.dao">

    <class name="ExamQuestionXref" table="examquestionxref">
        <composite-id name="id" class="ExamQuestionXrefKey">
            <key-property name="examid" column="ExamID" type="java.lang.Integer"/>
            <key-property name="questionid" column="QuestionID" type="java.lang.Integer"/>
        </composite-id>

    </class>
   
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 3:18 pm 
Regular
Regular

Joined: Tue Oct 28, 2003 8:25 am
Posts: 72
Location: Belgium
Playing with equals() and hashcode() with the collections framework is dangerous and changing the behaviour of a collection interface's method is even more. I would recommend you not to use tricks like that.

You should also remember that Hibernate will replace your TreeSet implementation with its own when objects are loaded back from the DB. That's maybe not related to the problem you're seeing here but it could become problematic later on.

What was the problem you tried to overcome in the addAll method of TreeSet ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 5:19 pm 
Newbie

Joined: Tue Nov 01, 2005 5:39 pm
Posts: 11
Only one row of data was being picked up. Once I put addAll in the setter then I got all the rows of data I was expecting.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 5:23 pm 
Newbie

Joined: Tue Nov 01, 2005 5:39 pm
Posts: 11
shoot, I missquoted my previous problem. It was not the number of return rows that was the problem. The query that gets generated when you have a composit-id thinks that any order-by you use should point to a column in the xref table. I could not figure out how to get it to sort by a colum that could not be found in the xref table so I implemented sorting with a comparator outside of hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 6:02 pm 
Regular
Regular

Joined: Tue Oct 28, 2003 8:25 am
Posts: 72
Location: Belgium
Why not using a sorted collection for that ?

http://www.hibernate.org/hib_docs/v3/re ... ons-sorted


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 7:05 pm 
Newbie

Joined: Tue Nov 01, 2005 5:39 pm
Posts: 11
I would say that I did not try sort="natural" but these are test questions and they need to come out in the order of the sequence property. When I tried the order by the resulting query points to the wrong table for the sequence column.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 5:39 am 
Regular
Regular

Joined: Tue Oct 28, 2003 8:25 am
Posts: 72
Location: Belgium
I think I got the idea of what you're trying to do but not yet what you did wrong to get your collection recreated each time you access it.

Here is how I would have implemented that sorting:

I would have configured the QuestionChoices set in the TestQuestion's mapping to use either natural sort or a sorting class.

In case of a natural sort, the class(es) of the entities contained in the set must implement comparable and Hibernate is using that to sort them. This way you can sort using whatever java properties you wish.

The sorting class is simply some other class implementing Comparable and able to perform comparison of the entities contained in the set. You would use that for instance if you cannot have your entities implementing comparable or if you want two different mapped sets to sort differently.

In no case I would have extended any implementation of set nor even do something else than assigning a value to the objet's variable in the setter of the collection.

If you still can't get it working properly then you should post your java entities's code and some snippet of the code that provokes the collection's recreation.


Top
 Profile  
 
 Post subject: use pure getter
PostPosted: Thu Dec 01, 2005 10:43 am 
Newbie

Joined: Thu May 26, 2005 1:04 pm
Posts: 13
I have faced the same thing some time back.

Are you doing any thing else in your getter.
Hibernate uses getters to access the properties/collections

Do not sort collections in the getter let the getter be pure getter.
Use some other method to do your processing of the property or collection.

You might want to give this a try.

-RT

_________________
RT


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