I am trying to configure hibernate in Eclipse but i am having some problems when i try to generate the Hibernate Code:
org.hibernate.console.HibernateConsoleRuntimeException: Received a
NoClassDefFoundError, probably the console configuration classpath is
incomplete or contains conflicting versions of the same class Received
a NoClassDefFoundError, probably the console configuration classpath
is incomplete or contains conflicting versions of the same class
org.hibernate.console.HibernateConsoleRuntimeException: Received a
NoClassDefFoundError, probably the console configuration classpath is
incomplete or contains conflicting versions of the same class Received
a NoClassDefFoundError, probably the console configuration classpath
is incomplete or contains conflicting versions of the same class
java.lang.NoClassDefFoundError:
org/apache/commons/collections/MultiMap
org/apache/commons/collections/MultiMap
java.lang.ClassNotFoundException:
org.apache.commons.collections.MultiMap cannot be found by
org.jboss.tools.hibernate.runtime.v_5_1_5.0.1.Final-v20160331-1852-B88
org.apache.commons.collections.MultiMap cannot be found by
org.jboss.tools.hibernate.runtime.v_5_1_5.0.1.Final-v20160331-1852-B88
This is how my projects libraries look like
And this is my hibernate.cfg.xml
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="ConexionHibernate">
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">hr</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:1521:xe</property>
<property name="hibernate.connection.username">hr</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
</session-factory>
</hibernate-configuration>
Before i have it with a tons more of jar files, but looking in other topics i tried to delete them. Before i had this jars, just in case i deleted someone i should not.
Old version of my project library
Thank you in advance!
I already solve the problem.
First of all i downloaded again all the jars from the hibernate website http://hibernate.org/orm/downloads/, concretely the 5.1.1 version.
I added to my project all the jars, not just the ones from the required folder, as i did the first time. And finally i downgraded the Hibernate console version to 4.3 and whoala! its working!
Related
When I was using Eclipse as my IDE, I used to use Hibernate Tools to reverse engineer a database to obtain my entities, complete with annotations.
I recently moved to IntelliJ IDEA, which I consider to be an overral better IDE, but unfortunately there isn't a port of Hibernate Tools for it, so I cannot generate my entities the way I used to. I know that IntelliJ IDEA has its own reverse engineer tool (the one accessible via Persistence->Generate Persistence Mapping->By Database Schema), but I found it to be somewhat buggy, sometimes generating entities which are plain wrong.
I know that Hibernate Tools can be also used from Ant. Is there a way to use it from Gradle, too?
I managed to use Hibernate Tools from Gradle, largely thanks to this question.
It turns out (I didn't know it) that Gradle is indeed capable of calling Ant tasks, so it is possible to use the preexisting Hibernate Tools Ant task to reverse engineer a database.
To do so, it is necessary to have a hibernate.cfg.xml file, which contains the configuration needed to tell the Ant ask how to access our database. This is an example:
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/mydb
</property>
<property name="hibernate.connection.username">
username
</property>
<property name="hibernate.connection.password">
password
</property>
</session-factory>
</hibernate-configuration>
(IntelliJ may complain about this file, telling that it cannot find the driver, but this is ok, as they will be provided by Gradle during the execution)
This config file will be used by the Ant task called from Gradle. I put it in a new db folder, created in the project root.
The following needs to be added to the build.gradle file:
configurations {
reverseMap
}
dependencies {
//...your other dependencies...
reverseMap 'org.hibernate:hibernate-core:4.0.1.Final'
reverseMap 'org.hibernate:hibernate-tools:4.0.1.Final'
reverseMap 'org.slf4j:slf4j-simple:1.7.5'
reverseMap 'mysql:mysql-connector-java:5.1.48'
}
project.ext {
hibernateDestDir = file("$projectDir/src/main/java")
}
task reverseMap {
outputs.dir hibernateDestDir
doLast {
hibernateDestDir.exists() || hibernateDestDir.mkdirs()
ant {
taskdef(
name: 'hibernatetool',
classname: 'org.hibernate.tool.ant.HibernateToolTask',
classpath: configurations.reverseMap.asPath
)
hibernatetool(destdir: hibernateDestDir) {
jdbcconfiguration(
configurationfile: "$projectDir/db/hibernate.cfg.xml",
packagename: "com.me.models"
)
hbm2java(
jdk5: true,
ejb3: true
)
}
}
}
}
This code creates a new configuration called reverseMap, which can be used to declare the dependencies needed for the reverseMap task (hibernate-core,hibernate-tools and log4j are needed, while the driver should be the one needed for your DBMS).
The reverseMap code calls the Ant task, basically following the official guide. The part of interest is hbm2java, which is the actual exporter. The rest of the code is basically glue code for the Ant task and configuration.
The Gradle task can be called either from the command line (./gradlew reverseMap) or from IntelliJ.
I am trying to implement a GWT project in IntelliJ Idea 15. I have no problems (at least obvious) with GWT and its superdev mode - I can run an application and play with it. I can do RPC calls.
However, now I am trying to add JPA/Hibernate support to use a database. And here I have troubles. In the project I have a GWT facet (2.6.1), a JPA facet (with hibernate implementation), and a web facet (for web dd). Using Open Module Settings -> Libraries -> New Project Library I have added gwt-servlet.jar, and using maven (its not a maven project, just using the feature of Idea) libraries: c3p0:c3p0:0.9.1.2, org.hibernate:hibernate-entitymanager:5.0.3.Final, org.postgresql:postgresql:9.3-1101-jdbc41 ; that is the configuration.
Using this persistence unit properties (excerpt):
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="300" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="3000" />
In GWT RPC servlet I try to create EntityManager instance:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("ThreatPersistenceUnit");
EntityManager em = emf.createEntityManager();
Query query = em.createQuery("SELECT a FROM Asset a");
...
But when I try to run it in the IDEA using default GWT run configuration (Jetty), I get following exception:
javax.persistence.PersistenceException: Unable to build entity manager factory
caused by
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.postgresql.Driver
Also, before the exception I get following warning:
WARN: HHH000022: c3p0 properties were encountered, but the c3p0 provider class was not found on the classpath; these properties are going to be ignored.
So it seems the server cannot see both c3p0 and postgre jdbc driver.
I tried to google, the closest problem/solution I found is this SO answer. I have put the libraries both to the project and to the artifact. However, I am not sure what the replier meant by the Jetty lib. Where would I find Jetty installation, if I am using just the GWT's default server?
Plus, what seems quite weird to me is that before I had a similar problem (I dont remember if it was exactly the ClassNotFoundException) with GWT RPC that I was able to resolve by adding a gwt-servlet.jar to the libraries - why then adding these other libraries does not help? At least it seems that it does not have any problems with hibernate, since it provides the warnings and so on.
OK, even though it is a stupid mistake, maybe someday in the future someone else will make it, so for future reference this was the issue:
I had set the SDK and sources version of the project as 1.6; the hibernate and other libraries seem to been compiled in the same or lower version. However, the postgre driver was compiled using a newer version - when I have set the source version to 1.7 and set as an SDK the Java 8 SDK, the program crashed at some different exception (but that was due to my programming error - unrelated). I found this out only after I tried (in total desperation) to create a new object of the driver manually in the code (not to delegate the creation to persistence provider) - then I got the major minor version exception and I knew what is the problem.
I'm know how to deploy a app on Heroku but only in Ruby on Rails, I have been trying A LOT deploying this app: GitHub, but I have an issue that I don't know how to solve!
My link: https://twitter-on-java.herokuapp.com/
Caused by: java.lang.NoClassDefFoundError: Could not initialize class br.edu.unisep.hibernate.HibernateSessionFactory
at br.edu.unisep.hibernate.GenericDAO.list(GenericDAO.java:57)
at br.edu.unisep.bean.TweetsBean.list(TweetsBean.java:21)
... 43 more
But if I any change, and run again mvn package, and do the push to heroku.. I have another issue:
org.hibernate.HibernateException: /hibernate.cfg.xml not found
So if you can give me a light?
I'm glad your time!
After I had a EXTREME headaches, I found the solution!!!
First my errors switching I don't know how but the issue is, a error when configuring the hibernate.cfg.xml:
<session-factory>
...
<!-- I used postgres instead of postgresql -->
<property name="connection.url">jdbc:postgresql://foo:5432/bar</property>
...
</session-factory>
This issue generated erros about NoClassDefFoundError, and hibernate.cfg.xml not found.
Now I'm happy :D
Short Version
I've got a Java-project which uses JPA 2.0 with Hibernate 4.3.4. All is fine when I run it inside Eclipse. But when I let Eclipse export a runnable JAR, the trouble begins and the program crashes due to a seemingly missing persistence unit...
javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyDBManager
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
... or seemingly unmapped classes...
3024 Thread-4| FATAL DbManager : DBManager could not load countries from database.
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Country is not mapped [SELECT x FROM Country x]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:331)
Background
Depending on the kind of export (extracting vs. packaging vs. copying libraries), I run into different errors which resist solving. The furthest I get is with the last approach, which is also the one I have to choose for license reasons, so let's focus on that one.
In this case the exported JAR fails to look into its persistence.xml. I will specify that later but first some background information...
Folder Structure
some_folder
myproject_lib
myproject.jar
root of my project's package structure
meta-inf
persistence.xml
File persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="MyDBManager" transaction-type="RESOURCE_LOCAL">
<!-- <exclude-unlisted-classes>false</exclude-unlisted-classes> -->
<!-- <class>isi.eload.core.Country</class> -->
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<!-- Do not define a connection here - this is done by the DbManager according to the command line arguments -->
<property name="hibernate.id.new_generator_mappings" value="true" />
<!-- <property name="hibernate.archive.autodetection" value="class, hbm" /> -->
</properties>
</persistence-unit>
</persistence>
I played around with the commented lines, once I felt that the xml is actually processed (see below) but that didn't help.
JPA/Hibernate JARs
Essentially the ones from the 4.3.4 Final Release:
antlr-2.7.7
dom4j-1.6.1
hibernate-commons-annotations-4.0.4.Final
hibernate-core-4.3.4.Final
hibernate-entitymanager-4.3.4.Final
hibernate-jpa-2.1-api-1.0.0.Final
jandex-1.1.0.Final
javassist-3.18.1-GA
jboss-logging-3.1.3.GA
jboss-logging-annotations-1.2.0.Beta1
jboss-transaction-api_1.2_spec-1.0.0.Final
.
Failing with persistence.xml
Packaged meta-inf
As I hinted at before, the exported JAR fails to properly process the persistence.xml. When I execute it in the above folder structure, the following exception is thrown:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyDBManager
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
This exception is usually thrown when the file was found but the persistence unit name given to Persistence.createEntityManagerFactory does not match any persistence unit declared in the files. But this is definitely not the case here!
I have no good idea, why this exception is thrown.
When I edit the JAR file (though I'm not sure that such tampering is not causing problems on its own) and empty or remove the persistence.xml, the error stays the same.
Another meta-inf
My first response was to copy a meta-inf folder next to the JAR:
myproject_lib
myproject.jar
... unchanged ...
meta-inf
persistence.xml
This seems to work, as an entity manager factory can now be created. But then no Entities are found and I think this is related to the fact that the persistence.xml, which is actually used, is not "on the same class path" as the JAR file.
Is there a link or an idea for how I can fix this? Preferably by forcing the JAR file to use the meta-inf folder which it contains itself.
META-INF needs to be in upper case. If Java is attempting to access the filesystem on Windows (or OS X in that regard) META-INF/persistence.xml will be automatically translated to meta-inf/persistence.xml by the operating system. Once you package it up to a JAR it becomes case sensitive and stops working.
We have a project called web-app1 and has a dependency on another jar file called core-app.jar which is provided by another team as a shared library , yet there is a hibernate.cfg.xml in this core-app.jar (inside of the jar), with content as below.
<hibernate-configuration>
<session-factory>
<property name="dialect">${hibernate.dialect}</property>
<property name="query.substitutions"><![CDATA[false 'N', true 'Y']]></property>
<property name="show_sql">false</property>
<property name="format_sql">false</property>
<property name="use_sql_comments">false</property>
<property name="generate_statistics">true</property>
<property name="hibernate.connection.release_mode">after_transaction</property>
<!-- Search Configurations -->
<property name="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</property>
<property name="hibernate.search.default.indexBase">${lucene.index.home}</property>
<property name="hibernate.search.default.batch.merge_factor">10</property>
<property name="hibernate.search.default.batch.max_buffered_docs">10</property>
</session-factory>
</hibernate-configuration>
As we see in the Search Configurations section, there is a variable ${lucene.index.home} that should be replaced by other projects on different OS platform,
so the question, does maven provide a way to filter a dependency jar file and filter the content? any plugins ? war:war , unzip ? dependencies ? I couldn't figure a fast way to do that. it looks to me , no matter what plugin would be adopted, the plugin needs to do 4 things basically.
1 unpack the jar in
process-resources phase.
2 substitute the ${var} with
value defined in profile.
3 pack it again back into a jar.
4 need to copy it back from the
packing/unpacking workspace back to
the maven process path ??
did anyone run into this similar requirement before.
thanks
I would assume that those values are meant to be set at runtime, likely as VM arguments. It doesn't make sense to provide a jar file that has to be modified to be able to be used.
If you really really REALLY have to do filtering at build time for configuration purposes, those configuration files should be filtered, NOT your dependencies. Then, you should either bundle said file into multiple artifacts (assuming of course you are targeting multiple environments), or be provided outside the built artifact as an externalized resource.