This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes   Use of sequence without creating an entity Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "  Use of sequence without creating an entity" Watch "  Use of sequence without creating an entity" New topic
Author

Use of sequence without creating an entity

Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 546

hi,
I already have created a sequence in the database.
I want to use it to fill an ID field (auto-incremented then) of a table.
I did not create an entity for that table (i have to use a specific API to insert data into that table).

How can i get that sequence in Hibernate and use it to set the value of that ID field ?
Without using an entity that maps to that table.

thanks a lot for your help.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

So you are trying to get Hibernate to update an entity it doesn't know about? The only possibility then is to use a native SQL query.

If you did have a mapped object the sequence could be used by using the sequence generation strategy. Any reason you can't map this entity?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 546

well, i guess i could map it, but then i would use the entity only to get the next value of the sequence so i can use it to insert it into the table.

We are using product that ships with some API that we are required to use to modify/create/delete objects
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


well, i guess i could map it, but then i would use the entity only to get the next value of the sequence so i can use it to insert it into the table.

I'm not sure I understand. If your sequence exists to provide a primary key for a specific table you would map that table to a class. Hibernate would take care of getting/setting the key based on the next value from the sequence when you persisted a new instance of that class if you used the sequence key generation strategy in your mapping.


We are using product that ships with some API that we are required to use to modify/create/delete objects

So you are not using Hibernate to do this?
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 546

1) This field is not a primary key
2) cant map the table to an entity for the reasons i mentioned before

Anyways, I wrote a basic method that takes a sequence as a parameter, and i get an EntityManager and run a native query :


Problem solved, thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of sequence without creating an entity
 
Similar Threads
Question about Entity Bean @Id
ejb3 primary key generation does not work without automatic schema generation
insert expression as primary key, taking advantage of generator class mappings?
ejbCreate, ejbPostCreate and ejbStore called but no rows are inserted
Creating an JPA in eclipse