Connecting database with hibernate - mysql

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 !

Related

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>

Hibernate not working Intellij driver not found

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>

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>

Do I need DataSource in JPA Hibernate project?

I am preparing some application with usage of JPA 2.0, Hibernate as provider, MySQL 5 as database, which will be deployed on JBoss AS 7.0.2.I have already configured some basics in persistence.xml and I came into some kind of trouble. I have noticed that some people also defines some specific DataSource on JBoss Management Console level.
My question is. Do I really need to worry about some DataSource or anything like that in Hibernate application?I thought it is important in old JDBC approach.In some books, where examples are shown, there is no such configuration in persistence.xml or hibernate.cfg.xml
Do I have to place mysql connector into JBOSS_HOME/standalone/deployments directory to use MySQL in my application?Here is content of my persistence.xml file:
<?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="SomeApp">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/somedb" />
<property name="hibernate.connection.username" value="" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
</properties>
</persistence-unit>
</persistence>
Well, you can either access the database by:
providing the url/driver/password/etc. information in the persistence.xml using your jpa-provider properties (in your case hibernate.connection.*) or the JPA 2.0 standardised javax.persistence.jdbc.* ones - this basically looks like the example you've posted,
creating a Data Source in the ApplicationServer and just referring to it in the persistence.xml (through it's JNDI name you provide during creation) which might look similar to this (without the XML schema definition for the sake of brevity) :
<persistence>
<persistence-unit name="SomeApp">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/myDB</jta-data-source>
</persistence-unit>
</persistence>
What you're actually doing right now (with these properties) is using the JDBC.
I would definitely go with the creation of the Data Source in the ApplicationServer rather than providing it in the properties in persistence.xml. It allows you to dynamically change the end-database, it's type, credentials, manage connection pools, etc. without even touching your descriptor.
It's also safer, as the credentials are not written in the plain file left on your server.
As a side note, please remember that the javax.persistence.jdbc.* properties are a JPA provider must requirement for the Java SE environment, but it's optional for Java EE.
Hope that helps!
Do I have to place mysql connector into
JBOSS_HOME/standalone/deployments directory to use MySQL in my
application?
Yes you need to put Mysql J/connector for use it as JDBC Driver. Your application server (JBOss, Weblogic, Glassfish, etc) doesn't provide it because depend of the RDBMS that you are using (in this case Mysql) and the version of it.
In the case of JBoss 7 the JDBC driver can be installed into the container in one of two ways: either as a deployment or as a core module. For the pros/cons of both modes an detailed explanatio you can check the following documentation: http://community.jboss.org/wiki/DataSourceConfigurationInAS7

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.