views:

316

answers:

4

Hello everyone.

I use Hibernate/Spring and a MySQL Database for my data management.

Currently I display a tree-structure in a JTable. A tree can have several branches, in turn a branch can have several branches (up to nine levels) again, or having leaves. Lately I have performanceproblemes, as soon as I want to create new branches on deeper levels.

At this time a branch has a foreign key to its parent. The domainobject has access to its parent by calling getParent(), which returns the parent-branch. The deeper the level, the longer it takes to create a new branch.

Microbenchmark results for creating a new branch are like:

Level 1: 32 ms. Level 3: 80 ms. Level 9: 232 ms.

Obviously the level (which means the number of parents) is responsible for this. So I wanted to ask, if there are any appendages to work around this kind of problem. I don’t understand why Hibernate needs to know about the whole object tree (all parents until the root) while creating a new branch. But as far as I know this can be the only reason for the delay while creating a new branch, because a branch doesn’t have any other relations to any other objects.

I would be very thankful for any workarounds or suggestions.

greets, ymene

+1  A: 

I don't know how Hibernate handles this internally. However, there are different ways to store tree structures in a database. One which is quite efficient for many queries done on the tree is using a "nested set" approach - but this would basically yield the performance issues that you're seeing (e.g. expensive insertion). If you need fast insertion or removal I'd go with what you have, e.g. a simple parent-ID, and try to see what Hibernate is doing all this time.

Lucero
I read about the nested sets as well. Unfortunatly I need the ability to change the structure fast, so I stopped focussing on it, since nested sets like u already said make structure changes expensive, but are perfectly nice for reading commands.Your Idea of using just Ids instead of objects is interesting. It kinda feels wrong to me, but might be worth a try, since hibernate shouldn`t be able to resolve the whole hierarchie then. So thank u for that hind!
ymene
+1  A: 

If you don't need to report on your data in SQL, you could just serialize your JTable to the database instead (perhaps using something like XStream). That way you wouldn't have to worry about expensive database queries that deal with trees.

Ewen Cartwright
This would be expensive for large trees since you would have to serialize the entire thing on every update and get the entire thing even if you wanted just the first level nodes.
tster
unfortunatly I have to report a lot, so I can't use it.
ymene
+2  A: 

Basically you are having some sort of many to one relationships structure right? In hibernate all depends on mapping. Tweak your mapping, Use One-to-many relationship from parent to child using java.util.Set.

Do not use ArrayList becasue List is ordered, so hibernate will add extra column for that ordering only.

Also check your lazy property. If you load parent and you have set lazy="false" on its child set property, then all of its children will be loaded from DB which can affect the performance.

Also check 'inverse' property for children. If inverse is true in child table, that means you can manage the child entity separately. Otherwise you have to do that using the parent only.

google around for inverse, it will sure help you.

thank.

Paarth
thank u for your detailed answer. U are right, relationships are declared One-to-Many and that way using Sets. First of all ill try focusing on my Mapping. So thank you for your hints. Ill check all the properties u where talking about.
ymene
thanks, i hope it should work for you. hibernate is always tricky, and confuses us... :)
Paarth
+1  A: 

One thing you can do is use the XML support in MySQL. This will give you native ability to support hierarchies. I've never used XML support in MySQL, so I don't know if it is as full-featured as other DBMSes (SQL Server and DB2 I know have great support, probably Oracle too I would guess).
Note, that I have never used hibernate, so I don't know if you could interface with that, or if you would have to write your own DB code in this case (my guess is, you're going to be writing your own queries).

tster
I would prefer to stay independend on my database management system, but if I dont see any other chance I will try researching on this point as well. Never heard about it yet, but very interesting hint. Going to have researches on that as well! thank you!
ymene