1

I am new to hibernate.I am running a query using hibernate . What i want to know is , if there is a way to know what DB and schema it is connecting to ?? is there a way to do this.??

    String query = "select name , id from employee";
    org.Hiberanate.Session hibernateSession =/*getting session from somewhere*/;
    org.Hibernate.Query queryObject = hibernateSession.createQuery(query);
    List<Object[]> objects = (List<Object[]>) queryObject.list();

I looked over web and found that i can get a connection from this Session object and then use MetaData of that connection to know DB . But it does not give schema name and also it just gives Oracle as the DB name , i wanted the specific DB that it is connecting to in Oracle.

1 Answers1

0

Typically in my projects I define this in my database.properties which if you are using spring with hibernate you can tell it to look at for example in my file I point at my 'threeminute' schema like this in that file

jdbc.url=jdbc:mysql://localhost:3306/threeminute

Check out section 3.3 here if this is what you are looking for

gcalex5
  • 1,091
  • 2
  • 13
  • 23
  • actually i am using spring like you said, but the thing is i have 3 differnt schemas and i have written jdbc.url for those 3 schemas and based on some condition i select one of these three url. i just want to verify wheather i am connected to correct schema or not. So if you know some way to print this information in java, that would be helpful – the_samurai Jun 11 '14 at 14:17
  • @the_samurai I'd refer you to this question then would be of more help then anything else I can offer, the answer basically says to use multiple configuration files and then use SessionFactory to map them then I'm sure based off that you can set up a simple if/else to use session factory x or y for your proper query! http://stackoverflow.com/questions/14133541/how-to-use-hibernate-for-two-different-schemas-in-a-single-database – gcalex5 Jun 11 '14 at 14:21