Hibernate Config....

Hi' is that possible to change hibernate configuration from java source? or using API from hibernate itself?

i have hibernate configuration file, called hibernate.cfg.xml, what i want is, can i change/update the configuration inside hibernate.cfg.xml from my java source? if it's possible, can anyone explain to me how....

thanks...

[353 byte] By [hudoqa] at [2007-11-27 6:28:44]
# 1

hi' it there anyone can help me? i'm really confused about this.

i already use this code too :

Configuration config = new Configuration().configure("hibernate.cfg.xml");

config.setProperty("hibernate.connection.username", "myname");

config.setProperty("hibernate.connection.password", "mypass");

But hibernate.cfg.xml still not change, whats wrong?

Thanks...

hudoqa at 2007-7-12 17:52:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Well, one straight question how is Hibernate related to JSP / JSTL ?

anyways

> Hi' is that possible to change hibernate

> configuration from java source? or using API from

> hibernate itself?

Yes it could be done...

> i have hibernate configuration file, called

> hibernate.cfg.xml, what i want is, can i

> change/update the configuration inside

> hibernate.cfg.xml from my java source? if it's

> possible, can anyone explain to me how....

there would be an Object Configuration which i can say to be mother of Hibernate ORM.You may have configure that object inorder to build a SessionFactory Object.

just as an example check below snippet hope that helps

Configuration hibernateConfig = new Configuration();

/** Setting Properties**/

// where props is a properties Object which contains core proerties which you configure in Hibernate-cfg.xml

//hibernateConfig.setProperties(props);

// manually add properties in the below way

hibernateConfig.setProperty("hibernate.connection.driver_class ", "org.postgresql.Driver");

hibernateConfig.setProperty("hibernate.connection.url", "jdbc:postgresql://localhost/mydatabase");

hibernateConfig.setProperty("hibernate.connection.username", "myuser");

--

--

/** Setting up resources or adding *.hbm.xml files**/

// We can place all mapping files with *.hbm.xml extension in a jar file and the add it to the configuration Object

//hibernateConfig.addJar(new File("filename.jar"));

//We can place all mapping files with *.hbm.xml extension in a directory and then add it to the configuration Object

//hibernateConfig.addDirectory(new File("directoryPath"));

// Manually add each mapping file in the below way

hibernateConfig.addFile("Item.hbm.xml");

hibernateConfig.addFile("article.hbm.xml");

hibernateConfig.addFile("Items.hbm.xml");

SessionFactory sessionFactory = hibernateConfig.buildSessionFactory();

Session hibernateSession = sessionFactory.openSession();

REF : http://www.hibernate.org/hib_docs/v3/api/org/hibernate/cfg/Configuration.html

http://www.hibernate.org/hib_docs/reference/en/html/session-configuration.html

Hope that could have given you some idea,If that doesn't i'd recommend you to go with the documentation provided under hibernate.org and JavaDocs for Hibernate API.

and a small advice Hibernate Does have their own Technical forums.

Hope that might help :)

REGARDS,

RaHuL

RahulSharnaa at 2007-7-12 17:52:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

>> just as an example check below snippet hope that helps.

hi i already modify my code like yours, but it's still same, hibernate.cfg.xml still not changing. this is my code :

Configuration config = new Configuration().configure().addFile("hibernate.cfg.xml");

config.setProperty("hibernate.connection.username", "myuser");

config.setProperty("hibernate.connection.password", "mypass");

SessionFactory sessionFactory = config.buildSessionFactory();

sessionFactory.openSession();

i don't have any more idea except those code. this is really nuts..can u give more helps?

thanks before...

hudoqa at 2007-7-12 17:52:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

my friend one straight ground rule if you are a java programmer.

alwys refer to api documentation.

Configuration config = new Configuration().configure().addFile("hibernate.cfg.xml");

you are messing everything here

configure() method by default assumes tht you have placed a file called hibernate.cfg.xml in current path it has configurations of all the core properties & resource mapping files.

and addFile() method is used to add a mapping resource file to the configuration (*.hbm.xml") not hibernate configurations file like hibernate.cfg.xml.

inorder to achieve that

Configuration config = new Configuration();

//config = config.configure("hibernate.cfg.xml");

// or use config.addResource() method

config = config.addFile("dbtable.hbm.xml");

-

-

-

config.setProperty("hibernate.connection.username", "myuser");

config.setProperty("hibernate.connection.password", "mypass");

-

-

-

SessionFactory sessionFactory = config.buildSessionFactory();

sessionFactory.openSession();

and i'd advice u to go through api javadocs 1st.

http://www.hibernate.org/hib_docs/v3/api/org/hibernate/cfg/Configuration.html

RahulSharnaa at 2007-7-12 17:52:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
hi' really sorry about that, now i already read the API, but why i can't update the content of hibernate.cfg.xml, there're no error or exception occured. can u explain it?thanks a lot before...
hudoqa at 2007-7-12 17:52:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...