hibernate.cfg.xml of Hibernate

2019-03-04 08:36发布

问题:

In my Java Project, hibernate.cfg.xml is sitting in the source folder-- \src. So, to be precise, it is a file in the source folder, sitting right next to the source codes.

However, Eclipse can't see it. Please see for Hibernate -- object creation error for the background of this Q.

My jar files are/should be in right places. And the header of hibernate.cfg.xml is the exact copy of one of the config files out of the installation package.

I am seeing the same error log with and without hibernate.cfg.xml in the project-- that's how I know it's not being processed on.

Where should hibernate.cfg.xml be ???

回答1:

By default hibernate will look at hibernate.cfg.xml on your classpath root. If you're packaging a jar, this is typically right at the top level. For war, typically on WEB-INF/classes.

If you eclipse is set with src folder as a source directory then it should be fine, just double check the generated war/jar has this file at correct location

Try refreshing / cleaning your eclipse project as often the resource is out of sync with file system.

Also have a look at Configuration class, it has configure(String) method to specify a custom location



回答2:

Exact same code-- both the calling class and DTOs worked in another project i set up from scratch. The setup & placement of libraries and the codes are the same, so are the other environment setting (all default Eclipse).

The only difference is the DBs behind and the related config file settings. I used postgreSQL in the former one and set the related tags in config according to that. The DB in the second, the one that worked is MySQL. Outside of these related tags, the hibernate.cfg.xml file is the same in the two projects-- the exact same set of tags, and values of non-postgreSQL/non-MySQL properties.

From what I know, Configure is a class reading & processing on the config file. and since everything else is the exact same, must be a mistake in postgreSQL setting in the former project.