Hibernate not working Intellij driver not found - mysql

I have a web application and I´m trying to get Hibernate working.
As soon as I call my webservice, which uses Hibernate, I´m getting the error
org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver could not be loaded
I´ve read some posts on SO, where the solution was to add the driver to the classpath.
What I first did is, I´ve added the dependency to my pom.xml, but afterwards it did not download any libs, so I downloaded it manually with maven.
Now I´ve "mysql-connector-java-5.1.31.jar" in my libs and added the stuff to my project.
But it had no effect.
Afterwards I´ve copied the lib to my TOMCAT/libs folder which also had no effect.
Why can´t the driver be loaded and how can I fix it?
My hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/myproject</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">hibernate</property>
<property name="connection.password">hibernate</property>
<mapping class="database.DevicetableEntity"/>
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>

Related

Wildfly 10 persistence MySQL table not found

I have a REST service which access a MySQL database. I'm using Wildfly 10 and MySQL 5.7.12. I am trying to get the EntityManager as an injection and I get the following error when executing the find method for my Entity mapping the table content.
org.h2.jdbc.JdbcSQLException: Table "MYTABLE" not found; SQL statement:
In the RESTService class I have
#PersistenceContext(unitName="myUnit")
protected EntityManager entityManager;
and my persistence.xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="myUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mytable" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="pass" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
</properties>
</persistence-unit>
</persistence>
The issue is that if instead of using injection I retrieve the entity manager using the manual way everything works smothly.
EntityManagerFactory emFactory;
emFactory = Persistence.createEntityManagerFactory("myUnit");
EntityManager em = emFactory.createEntityManager();
Could you give me some hints on how to use the PersistenceContext? the code is somehow cleaner and I prefer to use it.
It looks like you 're getting the default datasource injected in your persistence Unit so I guess this depends on how the EntityManager is 'built'. One way to fix this is to create a datasource in WidFly and use it (through) its JNDI name in your persistence unit.
Feel free to report a bug http://issues.jboss.org/
You are setting up a RESOURCE_LOCAL persistence unit. You should configure it as such:
<persistence-unit transaction-type="RESOURCE_LOCAL">
In order to use a resource local persistence unit you cannot inject EntityManager, only EntityManagerFactory. You'll end up with a lot less plumbing if you switch to JTA datasource and let the server manage it.
If you absolutely don't want to edit standalone.xml, in WF8 anyway, you can drop yourdatasource-ds.xml file into your WEB-INF folder, or into the deployments directory alongside your .war file. There was talk of removing this from WF though so I don't know if it works in 10.x.

Connecting database with hibernate

I know thats a known problem and i made my research but still couldnt find a proper answer and what to do, so my last hope is here. The problem is, when i use phpAdmin i can connect and create my tables in m database. But when i run hibernates connection wizard, after entering the connection properties, if i press the test connection it gives me this error :
"Timeout expired. IDE unable to establish connection. Check your proxy settings or try again later. The server may be unavailable at the moment.   You may also want to make sure that your firewall is not blocking network traffic."
My hibernate.cfg.xml looks like this
<?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="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://127.11.83.2:3306</property>
<property name="hibernate.connection.username">admin1s6nbcM</property>
<property name="hibernate.connection.password">*******</property>
</session-factory>
</hibernate-configuration>
I m suspecting that there is something wrong with MySQL Java Connector library ;
dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
because i get this warning when i build my application:
Some problems were encountered while building the effective model for cardsystelefonbuch:cardsystelefonbuch:war:1.0
'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: mysql:mysql-connector-java:jar -> version 5.1.25 vs 5.1.30 # line 83, column 13
It is highly recommended to fix these problems because they threaten the stability of your build.
For this reason, future Maven versions might no longer support building such malformed projects.
Any ideas would be greatly appreciated !

Could not load AnnotationConfiguration hibernate ,when internet is not connected

i am using hibernate 4.0 and mysql 5.6 with eclipse luna(v4.4.1),
i am using hibernate annotations in my project.
When i make changes to any table of DB, I always generate hibernate annotations automatically via eclipse hibernate configurations.
my question is that:
when i do not connect internet and want to generate hibernate annotations i get this ERROR : [Classpath]: Could not load AnnotationConfiguration (screen shot also attached) and when i connect to internet i do not face this error.
is this bug or feature ?? if this is feature then how can i generate hibernate annotations offline?
thanks!
hibernate.cfg.xml
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">myroot</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.72.128:3306/hesco</property>
<property name="hibernate.connection.username">myroot</property>
</session-factory>
</hibernate-configuration>
You could try putting all the dtds needed in local.
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd
In fact your url in the hibernate confil file should be the cause :
192.168.72.128
Try localhost if it's your ip adress.
thanks to Xavier Bouclet who guide me about dtd problem, I did google it and find my solution from this link
i put below DOCTYPE to my hibernate.cfg.xml file and now i can work offline
<!DOCTYPE hibernate-configuration SYSTEM
"classpath://org/hibernate/hibernate-configuration-3.0.dtd">
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"classpath://org/hibernate/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">myroot</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.72.128:3306/hesco</property>
<property name="hibernate.connection.username">myroot</property>
</session-factory>
</hibernate-configuration>

NetBeans, GlassFish and Hibernate JPA

On work, im using Eclipse, Jboss and Hibernate JPA. For a smaller, private project I like to use Netbeans, GlassFish and Hibernate JPA.
Problem: I want hibernate to generate the tables automagically - but it won't do that for me.
What i did:
First, i installed - obvious is obvious - Netbeans, Glassfish and a local MySQL-DB.
I created a JDBC-Connection for Glassfish:
url: jdbc:mysql://localhost:3306/myDatabase?zeroDateTimeBehavior=convertToNull
name: myDatabaseJDBC
Driver: com.mysql.jdbc.Driver
The connection seems fine, "testing" it resolved to a successful connection.
Now i created the persistance.xml like this:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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">
<persistence-unit name="primary" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>myDatabaseJDBC</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
Then i added the required depoendencies to my Project (using maven) and the hibernate Plugin to Glassfish
What works: When ive created a table, in Netbeans i can Select New -> Other -> Persistance -> Entity Class from Database. The connection shows the tables, i select one, click okay, and i got the entity.
However i usually work the other way round and let hibernate generate my tables from the created entities... That whoever won't work. (It even looks like Hibernate is not even invoked, when building the project)
Did I miss any configuration step?
update: -------------
I wanted to test if hibernate is "active" in any way. So i created a simple entity, a controller and deployed the application with a single button.
public void doSth() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("primary");
EntityManager em = emf.createEntityManager();
CEntity c = new CEntity();
c.setName("Test");
em.persist(c);
}
first, i received an exception:
Caused by: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:376)
Overhere: hibernate, mysql, glassfish v3, and JTA datasource i found the solution to add
// For GlassFish:
hibernate.transaction.manager_lookup_class=org.hibernate.transaction.SunONETransactionManagerLookup
to the persistance.xml. The exception is now gone, but i received another one: Unknown Entity: CEntity.
I figured out, that hibernate can NOT find my entities. (Yes, i used javax.persistance.Entity and not the one from the hibernate namespace). However "adding" the entity manually to the persistance.xml solves the issue and also the automatic table-creation is invoked.
However, now im looking for the correct configuration, so adding every Entity to persistance.xml is NOT required.
I set <exclude-unlisted-classes>false</exclude-unlisted-classes> but hibernate seems to ignore that...
The key was to add:
<property name="hibernate.archive.autodetection" value="class"/>
to persistence.xml's properties-collection. After all:
<persistence version="2.0" 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">
<persistence-unit name="primary">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>myDatabaseJDBC</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>
<property name="hibernate.archive.autodetection" value="class"/>
</properties>
</persistence-unit>
</persistence>

Hibernate question

I getting an error when running my program. the error is: org.hibernate.HibernateException: JDBC Driver class not found: com.mysql.jdbc.Driver.
The driver is there...I am not sure what it causing this. Any suggestions?
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/registrar</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="com/ths/entity/Course.hbm.xml"/>
<mapping resource="com/ths/entity/Student.hbm.xml"/>
<mapping resource="com/ths/entity/Enrollment.hbm.xml"/>
</session-factory>
</hibernate-configuration>
The driver is there...I am not sure what it causing this.
The driver is not there. The error is simple: the Java VM couldn't find your driver. So, double-check the classpath. If you are deploying a web-application, make sure your driver is in the server's lib, and not in WEB-INF/lib.
You may have that jar in project but class loader is unable to find that as class loader does not check each and every folder of project. If it is a web application then you need to put jar file in WEB-INF/LIB.
If it is a standalone application then you need to add jar file in build path.
you can check class path during run time to see whether folder (where jar exist) is considered in class path or not.
I am not sure what was causing this. I was trying this on my work computer. I tried it at home and it worked fine. Must just be a setting.