Hibernate Logging while Executing ANT SCRIPT for POJO Class Generation - java

I am using ANT SCRIPT to generate POJO classes from database.
I want to generate log for each class and table while executing the script.
Can I use Log4J for Hibernate? Any documentation or tutorial for this? or any other suggestions?
Please help!!
Thanks in advance.

just add to your hibernate.cfg.xml with following.
<property name="hibernate.show_sql">true</property>

Just write this code in your hibernate.cfg.xml
<property name="hibernate.show_sql">true</property>
This will show hql on catalina.out file. or on console if you are making desktop application.

You can use Log4J as log listener for Ant, then Log4J will log any ant message or call. Use this argument to attach the listener during ant execution.
-listener "org.apache.tools.ant.listener.Log4jListener"
Of course you'll have to specify the logger in the ant classpath.

Related

How to fire liquibase rollback script from spring application

I am using spring with liquibase to update my database. Since know I have not need to user rollback functonality, but the times come where I would like to make it work.
But I cant seems to fire it from my application.
I know that maven has plugin which helps with that, but until know I was not using it and when I add it I need to provide source and credentials to my database.
In this moment liquibase is configured in xml.
<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="p6spyDataSource"/>
<property name="changeLog" value="classpath:db.changelog-master.yaml"/>
.
.
</bean>
And in maven I have only dependency to liquibase-core.
And the place where I set liquibase.shouldRun is in application.properties
DataSource is taken from TomEE configuration server.xml file
So the question is if I can maybe somehow add maven plugin without adding credentials (should be taken from dataSource). Or is there other way to run rollback script from my changelog?
There are several related questions that have been posted previously about using Liquibase rollback with Spring Boot. This one seems the most similar to your post: Perform a liquibase:rollback from the command line when properties are in Spring-boot files (application.properties) and not liquibase.properties
Here is the answer as provided by Robert Kleinschmager:
The property names within springs application.properties and liquidate.properties are not compatible. You have three options
#1 just create a separate liquibase.properties file with the content you need - see liquibase doc as you only need to fix your current setup
#2 give the database parameters via command-line arguments
mvn liquibase:rollback -Dliquibase.rollbackCount=1 -Dliquibase.url=jdbc:postgresql://localhost:5432/comptesfrance -Dliquibase.username
see rollback goal for all arguments
#3 if you need a permanent solution, then you may add the liquibase properties into your application.properties and reuse them in the same file. i.e.
liquibase.url=jdbc:postgresql://localhost:5432/comptesfrance
spring.datasource.url=${liquibase.url}

How do I use templating with Hibernate and Gradle?

I have a server that talks to a database that I need to test. I connect to the database using Hibernate and manage the dependencies using Gradle. I want to use separate tables in MySql for production and testing. So I have currently this line in hibernate.cfg.xml:
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/production_database</property>
But what I really want is for it to be something like:
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/${DATABASE_NAME}</property>
and then when I run gradle test, DATABASE_NAME can be set to "test_database_name", and when I run gradle jettyRun it'll still be "production_database". This seems like something that should be possible, but when I google for "hibernate teplating" I get references this other thing called HibernateTemplate that has nothing to do with what I want as far as I can tell. What's the syntax that'll make this happen for me?
You should move that property out of hibernate.cfg.xml, and into a database.properties file.
And, Then you can use gradle to modify this file depending upon the argument.
Please refer to Gradle Tasks for this.
ant.propertyfile(
file: "database.properties") {
entry( key: "connectionurl", value: "somevalue")
}

Connection Time out error while parsing hbm file

I am facing this weird issue while opening hbm file. I am using hibernate3.jar. I also verified that there is only one hibernate3.jar in classpath and it contains hibernate mapping dtd file.
I tried to put code and exception here but StackOverFlow engine continuously throwing some error.
Code and exception is given at [link] https://forum.hibernate.org/viewtopic.php?f=1&t=1029004.
I also tried by specifying docBuilder.setEntityResolver(new DTDEntityResolver());
I was running it via Apache Ant and behind proxy.
Any pointers will be appreciated.
Thanks,
Setting proxy values as export
ANT_OPTS="-Dhttp.proxyHost=proxy -Dhttp.proxyPort=8080"
didn't work. So, I set the proxy values in Ant's build file as given in ant documentation and it worked.
<property name="proxy.port" value="80"/>
<property name="proxy.user" value=""/>
<property name="proxy.pass" value=""/>

include log4j properties file in hibernate to show query with value instead of question mark

I have crearte log4j.properties file like below:
log4j.logger.org.hibernate=INFO, hb
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
log4j.logger.org.hibernate.hql.ast.AST=info
log4j.logger.org.hibernate.tool.hbm2ddl=warn
log4j.logger.org.hibernate.hql=debug
log4j.logger.org.hibernate.cache=info
log4j.logger.org.hibernate.jdbc=debug
log4j.appender.hb=org.apache.log4j.ConsoleAppender
log4j.appender.hb.layout=org.apache.log4j.PatternLayout
log4j.appender.hb.layout.ConversionPattern=HibernateLog --> %d{HH:mm:ss} %-5p %c - %m%n
log4j.appender.hb.Threshold=TRACE
Can someone help me to how to include it in hibernate.cfg.xml file? I am sorry I actually don't know how log4j works. I create this to display my hibernate query with value instead of ? but still it displays ? nothing changes so what I need to proceed further?
I took reference from here Hibernate show real SQL
The 'show_sql' property in your hibernate.cfg.xml causes the queries to be printed directly to the console.
Log4j allows output to be logged anywhere from console to file to network ports to databases.
But the simple configuration that you have is supposed to print on the console as well. So first remove the show_sql property to see if Log4j puts anything on the console at all.
If this doesn't work, it shows that Log4j is not configured correctly. If you're using hibernate > 3.5, it uses the slf4j api, which uses logback by default instead of log4j.
You can easily switch to log4j by removing logback jar(s) from your classpath, and adding slf4j-log4j12.jar and log4j.jar instead.
The Log4j tracing also prints the queries using '?', but it also prints the parameter bindings, i.e. what the '?' will be replaced with by the database driver or server.
Please refer hibernate reference as per your version. Here is 3.3 Link
Edit :-
In order to setup logging you will need slf4j-api.jar in your
classpath together with the jar file for your preferred binding -
slf4j-log4j12.jar in the case of Log4J.
Put log4j.properties file in your classpath. An example properties file is distributed
with Hibernate in the src/ directory.
Enable following log 4j categories.
org.hibernate.SQL Log all SQL DML statements as they are executed
org.hibernate.type Log all JDBC parameters
In most cases it should be sufficient to include log4j.properties in application's classpath.
See How to initialize log4j properly?.
The link in question already suggests that you should look for bound sql param values not in sql text by in separate log statements like this:
TRACE [BasicBinder] binding parameter [1] as [VARCHAR] - john doe.
But I recommend to stick with log4jdbc. It's pretty simple to use. And it's able to inline query param values in printed sql text.
Add this also
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
your problem will be fixed.
No need to add in hibernate.cfg.xml, just place it to classpath and follow the steps mentioned in this link.
refer this link :- http://makecodeeasy.blogspot.in/2013/03/logger-in-spring-web-application.html
You can't print the values inline. Hibernate will always print the ? when logging the queries. The output of org.hibernate.type logging at the DEBUG level shows the values and types that are used to replace those question marks.
i like sql logging with proxy of jdbcdslog much more than hibernate logging.
Log SQL that is communicated with the database
In my case it wasn't hibernate.cfg.xml, it was log4j.xml file where I put the code given by #jdev
< property name="show_sql">true
Shows SQL queries
< property name="format_sql">true
Format the query shown on console
< property name="use_sql_comments">true
comments will be added to the queries
If your project is having 1000s of queries printing to the console, make others false:
< property name="show_sql">true
< property name="format_sql">false
< property name="use_sql_comments">false
OR write an appender to the log4j.xml which I have done in my project, just to write all SQL queries on separate text file which is specified here:
Configuring Hibernate logging using Log4j XML config file?

Hibernate log4j not recognizing log4j.properties

I have a hibernate standard java application (not a webapp). Logging already works by default. I would like to see the parameters passed in the sql queries. I have researched that in order to that I need to enable logging. It seems as if hibernate uses slf4j. I have downloaded that jar and its accompanying slf4j-log4j jar. I have added these jars to the classpath. I have also added a log4j.properties to the root of the eclipse project.
I can't seem to have the project recognize that it needs to use slf4j and its properties file.
Do I need to add a reference in my hibernate.cfg.xml?
Make sure you have the property hibernate.show_sql set to true.
format_sql set to true will make the SQL be formatted a bit more pretty.
You may also need to log on TRACE level.
For more details have a look here or here.

Categories