• 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
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Hibernate 2: check if table exists and get the name of the table out of the class

 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to check if table exists before writing to it.
So I have 2 questions.
This is the way I want to do it.


1st question:
isn't there a better way then just going though all the tables?
question 2: if the answer to question1 is no then is there a way to get the table name from the name of the class that represents it. I do not want to hardcode.

Thanks a lot!
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such thing as a SessionFactory that is unaware of the objects mapped in it, so why are you trying to use Hibernate to manage something it knows nothing about?
 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we use hibernate for db manipulations. So is your answer helpfull?
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate is designed to manage mapped classes, not tables, so my point is if your table is not part of the Hibernate SessionFactory why use Hibernate to manage it at all? Why not just use JDBC?


question 2: if the answer to question1 is no then is there a way to get the table name from the name of the class that represents it. I do not want to hardcode


You know the table exists because you have mapped it.
 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I do not know if it exists because a different process creates it.
I know the name of the class that corresponds to this table. So I want to check if the table is already there.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


No I do not know if it exists because a different process creates it.


Then shouldn't this other process also fix the SessionFactory? Having classes mapped to nothing sounds dangerous. Or can't you change the code for the other process?

 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes the system is screwed up but that's how it is. so is there an answer to my question?
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fair enough.


1st question:
isn't there a better way then just going though all the tables?


No, not without using database specific queries.


question 2: if the answer to question1 is no then is there a way to get the table name from the name of the class that represents it.


No, at least not in Hibernate 2. Hibernate 3 has this stuff in its Configuration class, however its pointless to use this since all it will give you is what you configured, not what may really exist.

 
Everybody's invited. Even this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!