-->
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.  [ 10 posts ] 
Author Message
 Post subject: Using Object Model with and without Hibernate
PostPosted: Sun Apr 10, 2005 3:18 am 
Newbie

Joined: Sun Apr 10, 2005 2:20 am
Posts: 15
I have a simple object model that can model simple graphs. I would like to be able to use this same object model with AND without Hibernate. The first issue I came accross was the fact that my id's are null when I am not using Hibernate (using hilo generator) since I have code that prints out my object ids and I use them as keys in collections.

Do you have any strategies for creating a default in-memory id generation that also works when using Hibernate? It would be great if these two id generation schemes were orthogonal so that when using Hibernate, the id generation chosen there would take over the in-memory, default one.

thanks,

-- yuri

Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 10, 2005 3:51 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
if you are already capable of generating ids then why not just use the "assigned" id strategy ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 10, 2005 10:25 am 
Newbie

Joined: Sun Apr 10, 2005 2:20 am
Posts: 15
What if, for instance, I want to use the same graph for in memory simulations and I dont want to incur the database overhead since it is not needed? I thought that the POJO idea would help me with exactly this type of scenario.

I have tried to do something like:

Quote:
public class Node
{
Long _id = new Long(hashCode());
....
}


So there would be a default id when Node was created. And I had the following Hibernate id generation strategy:

Quote:
<id name="id" type="long">
<generator class="org.hibernate.id.TableHiLoGenerator">
<param name="table">hilo_table</param>
<param name="column">next_hi</param>
</generator>
</id>


However, I had the following exception:

Quote:
at Main.main(Main.java:383)
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:88)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:74)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:172)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:72)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:67)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:148)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:1967)
at org.hibernate.persister.entity.BasicEntityPersister.updateOrInsert(BasicEntityPersister.java:1923)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:2163)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:75)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:675)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at Main.createGraph(Main.java:454)
at Main.main(Main.java:383)
Exception in thread "main"


I was hoping that Hibernate would ignore the already set id and use the one from the hilo table, but it seems that that is not hapening.

Any ideas?

thanks,

-- yuri


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 10, 2005 10:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
well hibernate checks the id to know wether it is working with a new or not-new object.

If you really want to save then use save - i think you are probably using saveOrUpdate or something similar..

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 10, 2005 10:34 am 
Newbie

Joined: Sun Apr 10, 2005 2:20 am
Posts: 15
max wrote:
if you are already capable of generating ids then why not just use the "assigned" id strategy ?


Sorry, I ended up not answering your question about the assigned id strategy.

If I have to choose an assigned id generation strategy that works in both scenarios I will have to go to something like an UUID, which is definetely an option. But I was trying to avoid UUID's because it would add some overhead (so it seems) to my database if it containst tens or thousands of instances. Imagine every pk and fk containing UUID.

The hilo seems much leaner and why not be able to have these two strageties depending on the way I want to use my graph model?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 10, 2005 10:36 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
sure - but why are you then assigning values to your id's ? that should not be needed.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 10, 2005 10:49 am 
Newbie

Joined: Sun Apr 10, 2005 2:20 am
Posts: 15
max wrote:
sure - but why are you then assigning values to your id's ? that should not be needed.


My point is that I need an in memory id when my objects are created because I lot could be done with these objects before they are even saved to a database, if at all.

If I use an id generated by Hibernate, my object ids will be null all the way from when the objects are created until they are saved for the first time.

I guess the answer to my question is that I will have to use an assigned id generation and something like UUID generated by me to be able to use these objects with or without a database.

thanks,

-- yuri


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 10, 2005 11:36 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
why does your object *need* the synthetic only persistence relevant id ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 10, 2005 7:33 pm 
Newbie

Joined: Sun Apr 10, 2005 2:20 am
Posts: 15
max wrote:
why does your object *need* the synthetic only persistence relevant id ?


Max, I am sorry, but I dont think I follow what you mean by "synthetic only persistent id".

Assuming that I have a simple POJO object model and that I want to use in two different usecases (believe me, in my case these two distinct usecases are important):
(1) Transient POJO. No database persistence, but only inmemory use (simulation, testing, etc).
(2) Persistent POJO. Database persistence using Hibernate (or any other POJO ORM).

Ideally I would like to be able to cover (1) as the default for my POJO model, but I would still like to have the freedom to create my (2) mappings independent of (1). Ideally, I wouldnt want to modify my POJO code to acomodate (2).

My understanding so far tells me that the only way to cover usecase (1) and (2) wihtout code changes, is to assign my own unique id at POJO instantiation time and use the "assigned ids" in my Hibernate mappings. These ids must be unique accross usecase (1) and (2) so the easiest would be to use an UUID.

thanks again!

-- yuri


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 11, 2005 2:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes - but you would only *need* to assign id's in case (1) IFF you need that id for something else than deciding what the id is in the db + to decide wether the object is transient or not.

And in case (1) your objects are ALWAYS transient so their id should logically always be null/0 or whatever other value you use to tell if an object is transient.

_________________
Max
Don't forget to rate


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