I am attempting to complete this Java Brains tutorial on hibernate, using hibernate3.6.10.Final and Eclipse IDE, and I am encountering an exception that is not covered in the video. Here is the output:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.hibernate.cfg.Configuration.reset(Configuration.java:332)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:298)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:302)
at com.helo478.firsthibernateproject.SimpleTest.setUpHibernate(SimpleTest.java:31)
at com.helo478.firsthibernateproject.SimpleTest.main(SimpleTest.java:19)
Caused by: java.lang.NullPointerException
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:167)
at org.hibernate.cfg.Environment.<clinit>(Environment.java:618)
... 5 more
[EDIT] I thought the first 3 lines of code were irrelevent, because I've seen a working version of this program (in the tutorial) that had that output. However, it seems that the issue was, indeed with the slf4j jars. Thank you #Jayaram Pradhan and # drurenia[/EDIT]
I did some searching on StackOverflow already and found this similar case (actually they were probably doing the same tutorial). Unfortunately, that thread does not, at this time, have a usable solution. The questioner solved their problem by starting from scratch and using a different database. I have done that (using both PostgreSQL and MySQL), and I get the same error on each.
The other response indicated that the configuration file should be in the project root directory and that the Configurations.configure() method takes an optional String filepath. I have tried placing the hibernate.cfg.xml file in both the project root directory, as well as the "src" directory. I have also tried pointing to the file in with an argument on the .configure() method. There is no change in output.
Because every line of my Java code is taken directly from the tutorial, I think it is most likely that there is a problem with my hibernate.cfg.xml file. The file is altered from a template that was packaged with hibernate3. I just changed it to reference the MySQL database. Being new to Hibernate, I suppose I must have made a mistake there. Here is the complete text:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Names the annotated entity class -->
<mapping class="com.helo478.firsthibernateproject.User" />
</session-factory>
</hibernate-configuration>
My question is this:
What, if anything, am I doing wrong in my hibernate.cfg.xml file? If nothing is wrong, what else might explain the failure?
I didn't see the tutorial you are reffering but from your stack trace its look like, your slf4j dependencies are not present.
Reffer to the below links which will help you to include the slf4j dependcies:
Hibernate 3.4 with slf4j and log4j
http://www.slf4j.org/manual.html
Make sure you have :
•slf4j-api-1.7.5.jar
•slf4j-simple-1.6.1.jar
in your lib.
Related
I am trying to learn Hibernate by writing a simple java program. I'm using MySQL as the database, and I get the above error when running the program. I saw a number of solutions for this on the internet and tried everything possible, to no avail. What am I doing wrong?
Configuration file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/test/hibernate/student.hbm.xml" />
</session-factory>
</hibernate-configuration>
Console output:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at com.test.hibernate.SimpleTest.main(SimpleTest.java:23)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/hibernatedb
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
... 5 more
Classpath & lib folder:
Most probably you didn't add the mysql-connector library correctly, because in the snapshot from the Run configuration you posted, that library appears within a project called "hibernate test" which contains a file named "mysql-connector-etc.jar", but that is not the way to set a JAR within a classpath.
Do it this way: It's better that you add the "mysql-connector-etc.jar" right within your project's Java Build Path: Pop up the project's contextual menu, and the Build Path > Configure Build Path > Libraries > Add external jars. Then select the mysql-connector JAR and enter. From then on, Eclipse will include this JAR into any execution of your project you should do (so you won't have to care about it anymore).
I'm doing some EJB with JPA project that maps/persists some entities to mysql database.
I have defined persistence unit in persistence.xml like this:
<persistence-unit name="MyAppPU" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>MyAppDS</jta-data-source>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
<property name="openjpa.jdbc.DBDictionary" value="mysql" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
</properties>
</persistence-unit>
Then, in tomee/conf/tomee.xml file i have defined data source like this:
<Resource id="MyAppDS" type="DataSource">
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://127.0.0.1:3306/MyAppDB
UserName root
Password 123
JtaManaged true
DefaultAutoCommit false
</Resource>
All this works fine, i create MyApp.jar, deploy it to TomEE server, test it and i get mysql tables in database.
My question is "Is there any other place where I could define data source resource?"
Or it has to be in tomee/conf/tomee.xml file?
Can it be defined somewhere inside application structure, in some xml file, and deployed inside apps jar file to server?
That's the whole point of a JNDI data source, to externalize it outside of your application, so you can modify it without recompiling or repackaging. So it is better to leave it this way.
For testing purpose, some EE server such as JBoss (Wildfly) let you define this in your project.
It might be a bit late to answer this, You can in tomee place the resource definition in WEB-INF/resources.xml.
You only have to set the tomee.xml. If you are using Eclipse, you must copy the tomee.xml into the servers configuration/Tomee to get recognize it into the web project, otherwise you will get troubles.
I'm writing a java Application for Tomcat 7.
I have a bean configuration for a class that creates a log file and appends information to it.
now the question is how can I know the tomcat log directory path in bean configuration.
for now I have the following bean:
<bean id="foo_logger" class="com.bar.LoggerBean">
<!-- <property name="logPath" value="/path/DWHEventGenerator.log"/> -->
<property name="logPath" value="/var/lib/tomcat7/logs/mylog.log"/>
<property name="logLevel" value="ALL"/> <!-- ALL, FINE/R/ST, INFO, SEVERE, WARNING, OFF -->
</bean>
what i'd like to do is instead of specify /var/log/tomcat7/log, is to specify some variable that will indicate the actual path of the logs directory of tomcat. is that possible ?
thank you.
The simplest approach would be to use catalina.base from the system properties in the logPath property value
<property name="logPath" value="${catalina.base}/logs/mylog.log"/>
This property will be set by Tomcat's launch script (catalina.sh/catalina.bat) so will be available for use when Spring loads the application context file.
I'm trying to configure a xWiki server on a OpenShift hosting (Tomcat 6 (JBoss EWS 1.0)). I've never configured a Java server before and I have a issue:
I followed THIS tuto on my local Ubuntu and worked. But reproducing the steps in the OpenShift server I can't find the hibernate.cfg.xml. Looking for files in the directory tree with hibernate I've found the folder jbossews-1.0/jbossews-1.0/work/Catalina/localhost/xwiki/WEB-INF/lib/ with the files:
hibernate-c3p0-3.6.9.Final.jar
hibernate-core-3.6.9.Final.jar
hibernate-validator-4.3.0.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-commons-annotations-3.2.0.Final.jar
But nothing similar to hibernate.cfg.xml. How can I fix it?
hibernate.cfg.xml is your configuration file for Hibernate, where you specify the dialect, connection driver, url, username, password, etc. of the database.
Example:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://host/database</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
</session-factory>
</hibernate-configuration>
Place this file in the Java resources classpath.
For detail information look hibernate.cfg.xml for Oracle or example by mkyong
This is (should be, anyway) an xWiki installation issue. If you are using the WAR file, then when you put the WAR into the tomcat webapps directory, Tomcat normally will expand the WAR into a directory with the same name. In that directory, in the WEB-INF directory, should be the hibernate.cfg.xml file. Typically, here:
/var/lib/tomcat5.5/webapps/xwiki/WEB-INF/hibernate.cfg.xml
If you have configured your Tomcat to run the WAR without expanding it, then the file will be in the WAR at:
WEB-INF/hibernate.cfg.xml
But in this instance you will have to edit the XML file and put it back into the WAR to configure the application.
I had the same problem, and by stracing tomcat I see it looks at the following places, the long one being in the git repository in my openstack gear:
/var/lib/openshift/518f381fe0b8cd1de2000181/git/tomcat.git/hibernate.cfg.xml
/usr/share/java/tomcat7/hibernate.cfg.xml
I cannot see any correlation between these locations and the classpath either given in the command line or in catalina.properties. The git repository is the cwd of tomcat. Putting hibernate.cfg.xml there does work for now. But I believe as soon as openstack guys realize how messy is to put the cwd there, they will move it away.
I have a web application using a jar file(lib) to access a Database.
The jar file when used as a standalone application executes correctly but the webapp received the error:
[01-08-12 13:17:05] - 35266 WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 08001
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/myapp
I have read the answers to similar questions but none of them solve my problem.
I am using Maven, where I have added the dependency for the mysql version I am using, 5.1.21. In fact, I have added it for both the Lib and the Webapp.
Before that, I tried to define in Eclipse a Connectivity Driver Definition bound to the same file, that I had copied to WEB-INF/lib in the eclipse project for my Webapp and with the same parameters that I include below for the persistence.xml file
I am not using any java to configure since I do all the configuration in the persistence.xml file(that I have copied to both META-INF folders, the one for the app(lib) and the one for the webapp. I am using Hibernate (4.1.2) through JPA.
The persistence.xml file is like that:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="myappPersistenceUnit"
transaction-type="RESOURCE_LOCAL">
<provider> org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myapp" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
</properties>
</persistence-unit>
</persistence>
the exception that I get is at Runtime, when launching an Http Request to the app, not at the initialization of the webapp, where I haven't seen any errors but this warning appears:
[01-08-12 13:16:39] - 8782 WARN
org.hibernate.engine.jdbc.internal.JdbcServicesImpl - HHH000342:
Could not obtain connection to query metadata : No suitable driver
found for jdbc:mysql://localhost:3306/myapp
I guess this is due to the information being looked up from the database is not necessary at init, and no exception is thrown but the same root cause in both cases.
EDIT:I am using Tomcat 6.0
Any help will be highly appreciated.
It seems that you are missing MySql driver in your project. To solve this you need to add mysql-connector.jar. externally to your project.
Or if you are using a maven project then use the following dependency in your pom.xml file.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.4</version>
</dependency>
You can use a suitable version if needed!!
If you're running your application as a Java Application, Add the JAR file in the Java Build Path, in Eclipse.
Alternatively, I wouldn't put the MSQL jar inside WEB-INF/lib folder of your web project, instead, I would put it in your Application folder library folder, where the Application Server will load your driver and you can access it from the Application Server container.
you should put mysql connector jar with your own code & add jar file with your project.
I also had the same problem some time before, but I solved that issue. There may be different reasons for this exception.
One of them may be that the jar you are adding to your lib folder may be old. Try to find out the latest mysql-connector-jar version and add that to your classpath. It may solve your issue.
I had the same problem with Tomcat 9. Solved by not having the driver jar in both common/lib and WEB-INF/lib (used the former).