-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Programmatic Configuration and Dynamic JDBC URL
PostPosted: Mon Aug 24, 2009 8:49 pm 
Newbie

Joined: Mon Aug 24, 2009 8:26 pm
Posts: 2
Howdy,

I'm using annotation's but I need to dynamically override/set hibernate.cfg.xml's /hibernate-configuration/session-factory/property[@name='connection.url'] during initialization.

I've tried a few strategies:

Load hibernate.cfg.xml And Override the Property:
Code:
Configuration config = new Configuration();
config.configure();
config.setProperty("hibernate.connection.url", "jdbc:sqlite:" + sqliteFilePath);
session = config.buildSessionFactory().openSession();

But that results in, org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.myproject.MyEntity"/>

Programmatically Load a Configuration:
Code:
Configuration config = new Configuration();
config.setProperty("hibernate.connection.driver_class", JDBC.class.getName());//   JDBC driver class
config.setProperty("hibernate.connection.url", "jdbc:sqlite:" + sqliteFilePath);//   JDBC URL
//more properties...
config.addClass(MyEntity.class);
session = config.buildSessionFactory().openSession();

But that'll try and load MyEntity.hbm.xml which does not exist (because I am using annotations).

I'm not sure what the magic mix is here, I'm sure it's possible... advice would be great!
--Thanks for reading :)

p.s. the documentation for Programmatic Configuration is HERE.


Top
 Profile  
 
 Post subject: Re: Programmatic Configuration and Dynamic JDBC URL
PostPosted: Mon Aug 24, 2009 9:47 pm 
Newbie

Joined: Mon Aug 24, 2009 8:26 pm
Posts: 2
Thanks for anyone who read this. I have a solution now...

Don't set a url in your hibernate.cfg.xml. Use AnnotationConfiguration, and set the hibernate.connection.url programmatically before you invoke .configure().
Code:
        AnnotationConfiguration annotationConfig = new AnnotationConfiguration();
        annotationConfig.setProperty("hibernate.connection.url", "jdbc:sqlite:" + sqliteFilePath);
        session = annotationConfig.configure().buildSessionFactory().getCurrentSession();


Top
 Profile  
 
 Post subject: Re: Programmatic Configuration and Dynamic JDBC URL
PostPosted: Tue Feb 09, 2010 5:53 pm 
Newbie

Joined: Tue Feb 09, 2010 5:42 pm
Posts: 1
I got the same problem with the mapping , how did you solve this issue?


Top
 Profile  
 
 Post subject: Re: Programmatic Configuration and Dynamic JDBC URL
PostPosted: Fri Jun 04, 2010 1:16 pm 
Newbie

Joined: Fri Jun 04, 2010 1:09 pm
Posts: 1
AnnotationConfiguration annotationConfig=new AnnotationConfiguration();
annotationConfig.setProperty("hibernate.connection.datasource","java:comp/env/jdbc/YOURJNDINAME");
annotationConfig.setProperty("hibernate.show_sql", "false"); //add other properties if needed
annotationConfig.addAnnotatedClass(YOURANNOTATEDBEANCLASS); //add all classes JPA annotated
org.hibernate.SessionFactory sessionFactory=annotationConfig.buildSessionFactory();
//open session and good luck! :D


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.