Mysql, NHibernate and visual studio 2008 configuration - mysql

I am a beginer with visual studio 2008 (C#), mysql and NHibernate.
I have problems with mysql connector as I don't know how to add reference to it.
In my project I have:
/lib/mysql-connector-net-6.1.4-src/
(downloaded from http://dev.mysql.com/downloads/mirror.php?id=387799#mirrors)
Now I right click on my project -> addReference -> browse -> What now?
Should I also edit my hibernate configuration?
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">Server=localhost;database=Pets;Database=pets,User ID=root,Password=;</property>
<property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
<property name="proxyfactory.factory_class"> NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu </property>
<property name="show_sql">false</property>
</session-factory>
</hibernate-configuration>
Thanks

You need to add a reference to the MySQL connector DLL that corresponds to the version of .NET you are targeting.
From the link provided, it seems like you downloaded the source code for the MySQL connector, so you'll either need to build this code and take the desired DLL out of the /bin folder afterwards or download a full installer (which will include the binaries) from the second link here:
http://dev.mysql.com/downloads/connector/net/6.1.html
Once you add this DLL to your project, you'll need to edit that NHibernate configuration to update the "connection.connection_string" property with the connection string for your database.
Hope this helps. There is some more documentation available here:
http://dev.mysql.com/doc/refman/5.1/en/connector-net.html

Related

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 !

Creating library jar with other libraries

I am creating jar via eclipse, but not runnable jar, it is just a library. After importing that i'm creating new project and adding this jar file. But when run application it throws exception about not finding libraries which are inside in my jar. But these libraries are included in created jar. Where is my mistake ? How can I do that ?
You cannot, by default, include dependency JAR files inside your JAR file, you need a plugin, as specified in Classpath including JAR within a JAR (suggested by Artur Malinowski). If, however, you wish to create a JAR which simply works with your other dependencies, you can easily do this via Eclipse by right clicking on the project, going into properties, Java Build Path, Libraries and then you can add JARs, etc. by using the buttons along the right side of the menu (as of Kepler).
I hope this helps.
P.S. You'll have to cart round the dependency JAR files wherever you put the main JAR, however, this is the easiest and most straightforward approach to getting it working in the first place, and then refine later, using build plugins.
I found the solution. I write some ant script for creating jar and it was solved my problem.
<target name="dist" depends="build-jar">
<property name="store.jar.name" value="MYJARNAME"/>
<property name="store.dir" value="store"/>
<property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
<echo message="Packaging project into a single JAR at ${store.jar}"/>
<mkdir dir="dist"/>
<delete dir="${store.dir}"/>
<mkdir dir="${store.dir}"/>
<mkdir dir="dist/lib"/>
<copy todir="dist/lib/" overwrite="false" granularity="9223372036854">
<fileset dir="lib/"/>
</copy>
<jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
<zipgroupfileset dir="dist" includes="*.jar"/>
<zipgroupfileset dir="dist/lib" includes="*.jar"/>
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</jar>
<zip destfile="${store.jar}">
<zipfileset src="${store.dir}/temp_final.jar"
excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
</zip>
<delete file="${store.dir}/temp_final.jar"/>
</target>

Mysql connection URL openshift

How do I see MySql connection URL from openshift? I need it to use it in hibernate.cfg.xml
When I log onto my account online I only see the username and password.
On open shift the database is already available in JNDI. It's name is jdbc/MySQLDS.
To use this 'datasource' in hibernate, your hibernate.cfg.xml should look something like this:
<!-- Database connection settings -->
<property name="hibernate.connection.datasource">java:comp/env/jdbc/MySQLDB</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Mapped annotated entity-->
<mapping class="org.hibernate.tutorial.domain.Event"/>
</session-factory>
To help you even mote please tell us if you are using JbossEWS 1.0, 2.0, JBoss AS6, JBoss EAP. Is your app scale, etcetera.

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.