-->
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: TABLE_PER_CLASS and AUTO_INCREMENT
PostPosted: Thu Mar 29, 2012 12:19 pm 
Newbie

Joined: Fri Mar 23, 2012 1:32 pm
Posts: 6
Dear All,

I'm facing the same issue many had already. I have a TABLE_PER_CLASS inheritance model on two tables with an AUTO_INCREMENT id field on each.

Library versions:
  • Hibernate 3.1.1,
  • MySQL 5.0.

I would like to avoid the use of JOINED inheritance and I DO need the inheritance mapping in hibernate so I can link'em with another entity and run queries.

I thought of having an embedded id with the AUTO_INCREMENT field and a discriminator, but I wouldn't know how to do that. Actually, I don't even know if that would work.

Currently, I have a GeneratorType.TABLE in the abstract superclass and it works, but that makes me have an extra table, thus, an extra insert.

Could anybody help me out?

Thanks.


Top
 Profile  
 
 Post subject: Re: TABLE_PER_CLASS and AUTO_INCREMENT
PostPosted: Fri Mar 30, 2012 2:22 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Currently, I have a GeneratorType.TABLE in the abstract superclass and it works, but that makes me have an extra table, thus, an extra insert.


Which extra table and which extra insert do you mean exactly?


Top
 Profile  
 
 Post subject: Re: TABLE_PER_CLASS and AUTO_INCREMENT
PostPosted: Fri Mar 30, 2012 5:18 am 
Newbie

Joined: Fri Mar 23, 2012 1:32 pm
Posts: 6
pb00067 wrote:
Which extra table and which extra insert do you mean exactly?

The table where the ID for the whole entity hierarchy is got from. And the extra insert is the one happening in that table every time I create and persist a new object in the hierarchy.


Top
 Profile  
 
 Post subject: Re: TABLE_PER_CLASS and AUTO_INCREMENT
PostPosted: Fri Mar 30, 2012 5:46 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
The table where the ID for the whole entity hierarchy is got from.


When using GeneratorType.TABLE this table is named hibernate_sequences, and it owns the columns

sequence_name
sequence_next_hi_value

Is this correct?
Can you please check it in you environment?


Top
 Profile  
 
 Post subject: Re: TABLE_PER_CLASS and AUTO_INCREMENT
PostPosted: Fri Mar 30, 2012 7:51 am 
Newbie

Joined: Fri Mar 23, 2012 1:32 pm
Posts: 6
OK, I was making no sense at all.

I do have a table called SEQUENCES and the table generator is this:

Code:
@TableGenerator(name = "operationGenerator", table = "SEQUENCES",
      pkColumnName = "SEQ_NAME", pkColumnValue = "OPERATION",
      valueColumnName = "SEQ_NUMBER")


There is currently just one record in that table:
Code:
|ID|SEQ_NAME |SEQ_NUMBER|
|2 |OPERATION|18392     |


I said "INSERT", but what I meant was "UPDATE", because I thought Hibernate would actually update the record in the table SEQUENCES... But I don't see that happening.
The record is untouched, no matter how many "Operations" I create and persist. And the weird thing is that it is taking the value for the ID only god knows where... I have rows in the table OPERATION starting from ID 919600...
What does Hibernate do? Does it cache the number, increment it in memory and then use it?
How does it maintain uniqueness among all the servers?
Where does it even get the values from? Do I need to specify the initialValue, and why?

I'm lost here...


Top
 Profile  
 
 Post subject: Re: TABLE_PER_CLASS and AUTO_INCREMENT
PostPosted: Fri Mar 30, 2012 9:42 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
What does Hibernate do? Does it cache the number, increment it in memory and then use it?


yes.
TableGenerator is a hilo generator.
After initializing the HiberanteSessionFactory and doing the first OPERATION insert, hibernate will increment the SEQ_NUMBER by 1
and with this action he allocates allocationSize id numbers (i think allocation size defaults to 32768, anyway you can define it excpilitely)

Code:
@TableGenerator(...
    allocationSize = 1000)


For the next 999 inserts in OPERATION your application will not touch the sequence anymore.

Quote:
How does it maintain uniqueness among all the servers?

Another server will also increment the SEQ_NUMBER by 1 (concurrent access is prevent due pessimistick locking in atomic transactions)
and therefore do inserts in a completely differen number range.


Top
 Profile  
 
 Post subject: Re: TABLE_PER_CLASS and AUTO_INCREMENT
PostPosted: Fri Mar 30, 2012 10:45 am 
Newbie

Joined: Fri Mar 23, 2012 1:32 pm
Posts: 6
Oh, I've just tested it. Ok, it seems the default allocationSize is 50. I think I could live with that...

But, what if I wanted to use the AUTO_NUMERIC of each table instead?
The Id has to be unique, I get that. But what if I had a composite id like, for instance, "(ID, DISCRIMINATOR)"? Wouldn't that do the trick? And how would I achieve that?

I mean, that was the original question, after all.


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.