-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Newbie - Oracle primary key triggers
PostPosted: Tue Nov 22, 2005 12:55 pm 
Newbie

Joined: Tue Nov 22, 2005 12:50 pm
Posts: 7
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0.5
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:
Oracle 10g
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Hi,

I've seen a few posts on this topic, but am new to this and trying to
get my head round it.

I have an Oracle table that has a primary key generated by a trigger
running before insert. How do I make use of this within hibernate?
I suppose the more basic question is - can I make use of this?

Any help will be very appreciated.

Thanks.
Joseph.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 5:40 pm 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
You can just use
[code]
<generator class="select">
[/]
within your id tag.

See http://www.hibernate.org/hib_docs/v3/re ... -generator for more information on id's :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 7:44 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
you work with assigned key generator and flush session and reread row after save


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 7:53 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
You should just call the getter for the property that is mapped to this column.

If this does not work, pass the business object to Session.refresh() before calling the property ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 6:05 am 
Newbie

Joined: Tue Nov 22, 2005 12:50 pm
Posts: 7
Hi everyone,

Thanks for the replies, but I'm confused.

If I try:
<id name="id" type="integer">
<column name="ID" precision="6" scale="0"/>
<generator class="select">
<param name="key">id</param>
</generator>
</id>

Then I get this error...
org.hibernate.PropertyNotFoundException: Unable to resolve property [name=id] to corresponding index
My Hibernate object only has an "id" to represent the primary key.

If I try to use an assigned key generator, then I get an error , on the
initial save - that I haven't set the id - so I can't event get to flush the
session

And.. apologies... but I don't understand the suggestion...

"You just call the getter for the property that is mapped to this column.

If this does not work, pass the business object to Session.refresh() before calling the property ;)"


Again, I appreciate your help and suggestions,

Regards,
Joseph.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 6:20 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
Try changing it to:

Code:
<id name="id" type="integer" unsaved-value="any">
  <column name="ID" precision="6" scale="0"/>
  <generator class="assigned"/>
</id>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 6:35 am 
Newbie

Joined: Tue Nov 22, 2005 12:50 pm
Posts: 7
Hi,

Thanks for the suggestion, but I still get:

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():

The problem is that the PK is set by the database trigger before insert.
So, I want hibernate to:
not need me to provide a PK before a save, but to be able to get the id
back from the saved object.

Cheers,
Joseph.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 6:44 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
I think I got it now...

When u want to use a trigger, you should ideed use generator="select"

But as a param, you should use a unique other attribute of the objecte as a temporary id value.

so:

Code:
<id name="id" type="integer">
   <column name="ID" precision="6" scale="0"/>
   <generator class="select">
      <param name="key">otherObjectAttributeName</param>
   </generator>
</id>


Try that :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 7:18 am 
Newbie

Joined: Tue Nov 22, 2005 12:50 pm
Posts: 7
Hi again!

I'd tried that before actually, and created a property in the
class, but get
org.hibernate.MappingException: unknown property: tempId

where tempId is the name of the property.
It seems that Hibernate isn't happy about the fact that tempId isn't
in the mapping file somewhere.


I then tried to create another property - also called id
and have this in my mapping file...

Code:
<id name="id" type="integer">
   <column name="ID" precision="6" scale="0"/>
   <generator class="select">
      <param name="key">id</param>
   </generator>
</id>
   
<property name="id" update="false" insert="false">          
</property>


But this generated the following error....
org.hibernate.id.IdentifierGenerationException: the inserted row could not be located by the unique key: id

Seem to be close, but not quite there :)

Cheers,
Joseph.
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 7:43 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
The value of <param name="key">id</param> should be the name of a unique property that exists in you mapping, other than the id itself :)

So yes, setting it to id doesn't work and setting it to a non existing property also doesn't


Don't you have any other property in your mapping that is also 'unique'?
Like a string i.e.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 8:27 am 
Newbie

Joined: Tue Nov 22, 2005 12:50 pm
Posts: 7
Hi,

That's the problem, I think.
The ID that I'm generating is a PK - but there isn't another column
that's unique. It's the generated ID that is providing uniqueness.

I suppose that a whole load of other columns could be made into
a composite key, but I'm looking for something simpler - or so
I thought :)

Also, depending on who you speak to or read, it's regarded as
a good idea to have a PK that is not dependant on the data.

Cheers.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 4:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
once 3.1 is released, you will also be able to the 'select' generator but then enable the use of 'getGeneratedKeys' processing at the jdbc level. This requires a JDBC3-compatible driver (I've tested with 10g drivers).

For example, have a look at the testsuite tests for this:
http://cvs.sourceforge.net/viewcvs.py/hibernate/Hibernate3/test/org/hibernate/test/generatedkeys/oracle/


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

Joined: Tue Nov 22, 2005 12:50 pm
Posts: 7
Hi,

Thanks, but I'm now confused!

When I've tried the format used in the test cases - which is
Code:
<id name="id" type="integer">
   <column name="ID" precision="6" scale="0"/>
   <generator class="select">
   </generator>
</id>


I get an exception sayign that there is a missing parameter.
It seems to want another field to write the generated id into - or have
I misunderstood the select generator?

Cheers,
Joseph.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 6:00 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
guess I'll just quote myself again ;)

Quote:
once 3.1 is released, you will also be able to ...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 6:09 pm 
Newbie

Joined: Tue Nov 22, 2005 12:50 pm
Posts: 7
Hi,

OK point taken ;)

So, to summarise... with 3.0.x, what I've been trying to do is not
possible - is that correct? (I'm asking the dumb questions ;) )

Next... Is this available in 3.1 rc3?

Cheers,
Joseph


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.