Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Use of sequence without creating an entity

 
Ranch Hand
Posts: 563
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Celinio Fernandes
Ranch Hand
Posts: 563
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 563
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 will suppress my every urge. But not this shameless plug:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic