Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
John Landon
Ranch Hand
Posts: 228
  • 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!
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate 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: 228
  • 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
Eclipse IDE Hibernate 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: 228
  • 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
Eclipse IDE Hibernate 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: 228
  • 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
Eclipse IDE Hibernate 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic