For hibernate project what is recommended to use hibernate.properties file or hibernate.cfg.xml file.
What to use??
And please can u also specify why to use properties instead configuration and vice versa
which use is best hibernate.properties file OR hibernate.cfg.xml file
The hibernate.properties is the old way to represent the connection information, for newest versions (2.0+), it has been replaced by the XML-based configuration file, since it is more flexible and has more features.
However, there's a thread explaining how to combine them both, you should have a look at this thread: How to use hibernate.properties file instead of hibernate.cfg.xml.
You should also have a look to the Hibernate documentation http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html, chapter 3.7. XML configuration file.
Related
I like to migrate from an older 1.2.x version of log4j, according to the docs I can use the bridge (log4j-1.2-api-2.15.0.jar), but one of the requirements is:
"They must not configure by calling the classes DOMConfigurator or PropertyConfigurator"
Unfortunately we're using the DOMConfigurator to set our logging configuration.
If I remove this - is the bridge looking for an log4j.xml now and can it read the old config format? Or must I convert the config file to the new format and name it log4j2.xml?
Would removing the DOMConfigurator code and place and log4j.xml or log4j2.xml in the classpath work?
Thanks
Klaus
Okay, this works.
I can disable the DOMConfigurator and use an log4j2.xml in the classpath - just have to transfer the config settings to the new format.
I am writing Java code that tests a Java library. The library includes its own Log4j2 configuration as part of the distribution.
I would like to use Log4j2 in my test code without modifying the library's configuration.
Is there a way to have a separate Log4j2 configuration for my test code?
This is all running as command-line Java, no servers or web involvement at all.
EDIT
What I want is to be able to configure loggers, appenders, etc for the test code to use, and at the same time have the library code use its own separate configuration file for its logging.
The idea is to use Log4j2 in my test code, but without having to change the library's configuration file. Since the library configuration file is part of the library's distribution, I don't want to change it for testing.
This may be helpful:
Log4j2 will first look for log4j2-test.xml in the classpath
if that file is not found, it will look for log4j2.xml in the classpath
So one option is to copy the library's configuration (log4j2.xml) to log4j2-test.xml and add your own configuration to log4j2-test.xml.
Furthermore, Log4j2 supports XInclude in XML configuration, so you could use that feature to avoid duplicating the library's configuration in your log4j2-test.xml.
Log4j2 supports "Composite Configuration" which exactly matches your requirement. All you need to do is provide path to multiple files in log4j.configurationFile property. This can be passed from command line or added to log4j2.component.properties file in your application.
References
https://logging.apache.org/log4j/2.x/manual/configuration.html#CompositeConfiguration
https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties
There are two step you can try to solve for your issue
Create your own configuration file with your custom name(eg: xyz.properties/.xml)
You must add the following line to your java runtime command
cmd> java -Dlog4j.configuration=location/xyz.properties
If you use diffent name for configuration rather log4j.properties/.xml file you need to configure that file at runtime by above command for more info have a look here..
Correct format for using an alternate XML file to log4j2.xml:
java -Dlog4j.configurationFile=./location/log4j2-custom.xml
Assuming ./location/log4j2-custom.xml exists and is the new XML to replace log4j2.xml in this run
See:
https://github.com/kamalcph/Log4j2Examples/blob/master/src/main/java/in/co/nmsworks/log4j2/examples/CompositeConfigurationExample.java
Referencing https://logging.apache.org/log4j/2.x/manual/configuration.html states that you can add multiple comma separated files under log4j2.configurationFile property.
To use multiple configuration files, depending on the environment you must set the.
for example:
if (env.equals("DEV")) {
setConfigFile("log4j2-dev.xml");
}
public static void setConfigFile(String logConfigFile) {
File file = new File(logConfigFile);
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
context.setConfigLocation(file.toURI());
}
first configure application.yaml file
spring:
profiles:
active: dev
---
spring:
message: running in the dev profile //just to output the message in the log
profiles: dev
logging.config: classpath:log4j2-dev.xml
---
spring:
profiles: prod
logging.config: classpath:log4j2-prod.xml
and create these similar files in your classpath
* log4j2-dev.xml
* log4j2-prod.xml
I am struggling to build the JPA annotated classes diff changesets against my database, using liquibase.
Still, I am very confused about few things.
I use the following :
liquibase.properties
#liquibase.properties
driver: org.postgresql.Driver
classpath: real_path/.m2/repository/org/postgresql/postgresql/9.2-1002-jdbc4/postgresql-9.2-1002-jdbc4.jar
url: jdbc:postgresql://localhost:5432/diquiz
username: postgres
password: postgres
referenceUrl: hibernate:ejb3:diQuiz
referenceUsername: postgres
referencePassword: postgres
changeLogFile: changelog-master.xml
and
java -jar real_path\liquibase-core-3.0.6.jar diffChangeLog
and a normal persistence.xml file with standard JPA configuration.
I get an error which says : Liquibase diffChangeLog Failed: java.lang.RuntimeException: Cannot find database driver: Driver class was not specified and could not be determined from the url (hibernate:ejb3:unit)
I am confused because the below answer says that we need a hibernate.cfg.xml file (even I have persistence.xml instead), but then he says that we can use some url's which are defined on wiki page.
Hibernate using JPA (annotated Entities) and liquibase
Wiki page says that if we need to use JPA, we can choose between three types of URL's.
hibernate:ejb3:myPersistenceUnit
hibernate:ejb3:com.example.MyConfigFactory
hibernate:ejb3:myPersistenceUnit?hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
So, I choose the first one, and I've set in the liquidbase.properties this as referenceUrl.
Also, on the wiki page it is mentioned : (https://github.com/liquibase/liquibase-hibernate/wiki)
If you are using the command line version of Liquibase, you simply
have to add the liquibase-hibernate[3|4].jar file to the
LIQUIBASE_HOME/lib directory.
I did this too.
Still, doesnt work. Can someone explain me somewhow what am I missing ?
Thanks a lot!
The problem is that you are calling liquibase with "java -jar". The Class-path line in the jar can't pick up additional jars from the lib directory and so the liquibase-hibernate.jar is not being included in the classpath.
Run liquibase using the sh/bat file included in the liquibase jar: real_path/liquibase[.bat] diffChangeLog
I had the following working organization
src/main/resources/log4j.xml
src/main/resources/hibernate.properties
I wanted to reorganize my webapp as follow:
src/main/resources/log/log4j.xml
src/main/resources/orm/hibernate.properties
The Logger.info("foobar") still logs well (after having set the log4jConfigLocation context parameter), and the app still has a working database connection.
The problem is that Hibernate doesn't log anything anymore, even if hibernate.show_sql is set to true. Any idea why? Should specify the new path to the log4j.xml file to Hibernate? If yes, how?
You could run your server with this VM argument:-
-Dlog4j.configuration=/path/to/log4j.xml
I usually tend to place the log4j.xml at the recommended default location unless there's a need to do otherwise... "convention over configuration" is important especially if other peers may be working on the same project in the future.
Log4j first looks for its configuration by looking at the system property "log4j.configuration". If that system property is not set, it looks for a log4j.properties or a log4j.xml file on the classpath.
So, if you really want the log4j.xml at src/main/resources/log/log4j.xml, you will have to set the log4j configuration system property. This is basically what limc does by supplying it as a vm argument.
Also like limc says, you should probably just keep the log4j.xml at the default location.
I have a problem regarding the use of hibernate.properties and c3p0.properties. I am dependent upon an API. The API uses Hibernate. As such the API has it's own hibernate.properties and c3p0.properties file included within its delivery jar. I need to ensure I ignore these properties completely when instantiating my Hibernate SessionFactory. I do need these files to remain available on the CLASSPATH so that the API I depend upon can be configured. I simply need a way to instantiate my org.hibernate.SessionFactory so that it doesn't look at these files in any way. I don't mind using an xml configuration or setting my Hibernate properties manually. I just need to ensure that the properties in hibernate.properties and c3p0.properties are not read.
In order to do this you will have to use an XML hibernate config file. If both the hibernate.properties and hibernate.cfg.xml are present, the XML file will override the properties file.
You can find more information here:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-xmlconfig
Note: your c3p0 properties can be defined in the same XML file.