-->
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.  [ 4 posts ] 
Author Message
 Post subject: Best practice for generated properties?
PostPosted: Fri Jan 29, 2010 10:23 am 
Newbie

Joined: Fri Jan 29, 2010 9:58 am
Posts: 2
I do not want to use the primary key of my user table to reference a row from a user interface.

So I wanted to add a public_id column generated (from an oracle sequence) on create, never updated, unique, non-nullable. However @Generated may only be used on @Id columns (http://opensource.atlassian.com/project ... e/HHH-2907).

I've seen someone offer the workaround of a one-to-one relationship. I can create a public_id to user_id join table with the public_id as @Id,@Generated and put a one-to-one inside the user table. If I then add a non-insert, non-update property while keeping the one-to-one lazy, I can fetch without the overhead of an additional join. However, non-nullability and foreign-key constraints are an issue at creation time. Which comes first, the map table or the user table and how do you enforce data integrity without creating them both at the same time (non-nullability constraint)?

Does anyone know of any way to do this with hibernate?


Top
 Profile  
 
 Post subject: Re: Best practice for generated properties?
PostPosted: Fri Jan 29, 2010 10:41 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Hmmm... the examples at http://docs.jboss.org/hibernate/stable/ ... ml#d0e2322 shows @Generated being used with a String column. I have not used it, so maybe there is an issue with it. The HHH-2907 seems to consider properties that are generated by something else than the database.


Top
 Profile  
 
 Post subject: Re: Best practice for generated properties?
PostPosted: Fri Jan 29, 2010 11:23 am 
Newbie

Joined: Fri Jan 29, 2010 9:58 am
Posts: 2
I have to use SQL for this. Otherwise I'd setup a database-side trigger to insert the the sequence into public_id on insert. If I could do that, I would just turn insert and update to false and use it like a regular property.


Top
 Profile  
 
 Post subject: Re: Best practice for generated properties?
PostPosted: Fri Jan 29, 2010 3:15 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Maybe I am missing something, but doesn't "public_id column generated (from an oracle sequence)" mean that the value is assigned automatically by the database? Or does it only work for primary key columns?

Hmmm... I searched the net and realized that I was missing something. The value is not automatically generated. But... maybe you can map your class using a custom <sql-insert> statement. This could include something like:

Code:
insert into table (..., generated_column, ...) values (..., the_sequence.nextval, ...)


See http://docs.jboss.org/hibernate/stable/ ... erysql-cud for some more examples. The drawback is reduced portability and that you have to make sure that the SQL matches what Hibernate expects.


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