-->
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: Populate a collection by only setting element IDs
PostPosted: Wed Sep 17, 2008 4:57 pm 
Newbie

Joined: Wed Sep 17, 2008 4:37 pm
Posts: 2
My object A has a Set of object B. All my Bs are created first, and as I create each object A, I populate the set with the appropriate Bs (there's usually only one). I already know the ID of the B object I want to add to the set, so is there anyway to avoid loading the B from the DB?

I'm imagining something like this:

Code:
B myB = new B();
myB.setId(knownId);
A myA = new A();
myA.addB(myB);


When I do that, however, hibernate clears all of the columns of B, except for the ID. The cascade settings on A.getBs() is unset (cascade = none), so I thought it would populate the collection table without actually modifying any of the Bs.

There are millions of these objects, so even cutting out the single read to load each B would improve the performance quite a bit. Is there any way to get this to work, or is there something else I can try?

Thanks,
Steve


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2008 6:22 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Steve,

Form your example, it is unclear what you want to avoid, exactly.
A and B are separate, independent entities, so one database action will be needed to create an "a", another to create a "b", and a third to indicate the relationship between "a" and "b".
What else is Hibernate doing that you don't like?
Please include your classes and mapping file(s).

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2008 6:44 pm 
Newbie

Joined: Wed Sep 17, 2008 4:37 pm
Posts: 2
Assume I have already saved 1,000,000+ instances of B to the database, each with an ID and other fields set. When I want to create an instance of A, I don't want to load myB from the database, but I want to add it to newA.setOfB.

At the SQL level, all hibernate is going to do is issue a statement like:
insert into a_b_table (a_id, b_id) values (id_of_newA, id_of_myB);

Since I already know the ID of myB, I'm trying to get hibernate to issue that statement without first loading myB from the DB.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2008 9:58 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
A possibility would be declaring all properties of B as lazy.

http://www.redhat.com/docs/en-US/JBoss_ ... ching.html

This doesn't seem worth it, though.

A solution I like more, and I have used in the past, is to give the relation table (A_B) a unique ID and a mapping of its onw. In that way, you can insert "ABBeans" consisting only of the ID of A, the ID of B, and its own (probably autogenerated) ID.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 18, 2008 3:28 am 
Beginner
Beginner

Joined: Tue Sep 09, 2008 5:42 am
Posts: 22
Location: Romania
Ciao

I think the last post of gonzao_diaz is a good suggestion, but if you have a single reference of object B in object A and you don't want to create another link table, then you can simply map in class A only the id from the class B, like a property (not the entire object) and when you will need to get the object B from class A you can use Get<ID> method from your session. This way when you will SAVE your object A Hibernate won't load the B object.

Still my suggestion works only if you have a single reference of object B in class A.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 18, 2008 10:32 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
That won't work, tomaandtoma, because when Hibernate reads the mapping file it would complain that the "property" A_ID is repeated.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 8:41 am 
Beginner
Beginner

Joined: Tue Sep 09, 2008 5:42 am
Posts: 22
Location: Romania
My suggestion was to map only the ID of B like a property inside the A class. This works for shure in NHiberbate because I did that in my projects.

If you are talking about a doulble mapping of the same property inside the same mapping file that works too if you clearly specify that the flags INSERT and UPDATE to be false on the second time you are mapping the same property.

In order to see, that I use that and it works, I give you an example of my mapping from one of my project:

Code:
<class>
    <composite-id name="PK_S_Produttore" class="E_ProduttoreKey" unsaved-value="any" access="property">
      <key-property name="Codice" type="System.Int32" column="CodProduttore" />
      <key-many-to-one name="Cliente" class="Cliente">
        <column name="CodCliente" />
      </key-many-to-one>
    </composite-id>
    <property name="CodProduttore" type="System.Int32" column="CodProduttore" not-null="true" insert="false" update="false"/>
    <property name="CodCliente" type="System.Int16" column="CodCliente" not-null="true" insert="false" update="false" />

.........................................................................................................

    <property name="CodTipoProduttore" type="System.Int16" column="CodTipoProduttore" not-null="false"/>

</class>


So you can see at the end how I map the TipoProduttore that is a reference to another table.

And you can also see how I map CodProduttore and CodCliente twice in the same mapping file.

Maybe you didn't understand my suggestion on how to solve the problem in the first place but what I said works just fine.


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.