# Probleme mit meinem 1. Hibernate Beispiel



## Presler (17. Apr 2012)

Seit kurzem beschäftige ich mich mit Hibernate und habe mir hierzu die Doku von Hibernate angesehen. Aber irgendetwas mache ich in meinem ersten Hibernate test falsch und ich komme einfach nicht darauf was es ist.

Hierzu habe ich mir Hibernate hibernate-release-4.1.2.Final.zip 
und Connector-J 3.0.17 (mysql-connector-java-3.0.17-ga.zip)
heruntergeladen.

Eine Java Projekt über Eclipse Indigo und innerhalb des Projekts
folgende libs reinkopiert

mysql-connector-java-3.0.17-ga-bin.jar
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.1.2.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar

In meiner MySQL - Datenbank die Datentabelle "User" erstelle

1 	id 	int(11) 			Nein 	kein(e) 	AUTO_INCREMENT 	
2 	FirstName 	varchar(80) 	latin1_swedish_ci 		Nein 	kein(e) 
3 	MiddleName 	varchar(80) 	latin1_swedish_ci 		Nein 	kein(e) 
4 	LastName 	varchar(80) 	latin1_swedish_ci 		Nein 	kein(e) 
5 	Email 	varchar(255) 	latin1_swedish_ci 		Nein 	kein(e) 	
6 	Loginname 	varchar(40) 	latin1_swedish_ci 		Nein 	kein(e) 
7 	Password 	varchar(20) 	latin1_swedish_ci 		Nein 	kein(e) 
8 	activecode 	varchar(255) 	latin1_swedish_ci 		Ja 	NULL 	
9 	active 	tinyint(1) 			Nein 	0 	

und im SRC-Verzeichnis die hibernate.cfg.xml


```
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//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.url">jdbc:mysql://localhost/hibernate</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password"></property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="show_sql">true</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
```

Die Verbindungsdaten stimmen

Meine Domain sieht wie folgt aus


```
package hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="User")
public class User {
      /** integer primary key */
	  private long   id;
	
       /** string First name */
	  private String FirstName;

      /** string Middlename */
	  private String MiddleName;
	  
      /** string Last Name */
	  private String LastName;

      /** string Registration Email */
	  private String Email;
	  
      /** string Loginname */
	  private String Loginname;

      /** string Password */
	  private String Password;

      /** string activecode */
	  private String activecode;
	  
	  /** boolean active */
      private boolean active;

  	/**
  	 * constructor
  	 */      
     public User(String firstname, String lastname, String email) {
            this.FirstName = firstname;
            this.LastName  = lastname;
            this.Email     = email;
     }

 	@Id
 	@GeneratedValue(generator="increment")
 	@GenericGenerator(name="increment", strategy = "increment")	     
	/**
	 * @return the id
	 */
     public long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * @return the firstName
	 */
	public String getFirstName() {
		return FirstName;
	}

	/**
	 * @param firstName the firstName to set
	 */
	public void setFirstName(String firstName) {
		FirstName = firstName;
	}

	/**
	 * @return the middleName
	 */
	public String getMiddleName() {
		return MiddleName;
	}

	/**
	 * @param middleName the middleName to set
	 */
	public void setMiddleName(String middleName) {
		MiddleName = middleName;
	}

	/**
	 * @return the lastName
	 */
	public String getLastName() {
		return LastName;
	}

	/**
	 * @param lastName the lastName to set
	 */
	public void setLastName(String lastName) {
		LastName = lastName;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return Email;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		Email = email;
	}

	/**
	 * @return the loginname
	 */
	public String getLoginname() {
		return Loginname;
	}

	/**
	 * @param loginname the loginname to set
	 */
	public void setLoginname(String loginname) {
		Loginname = loginname;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return Password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		Password = password;
	}

	/**
	 * @return the activecode
	 */
	public String getActivecode() {
		return activecode;
	}

	/**
	 * @param activecode the activecode to set
	 */
	public void setActivecode(String activecode) {
		this.activecode = activecode;
	}

	/**
	 * @return the active
	 */
	public boolean isActive() {
		return active;
	}

	/**
	 * @param active the active to set
	 */
	public void setActive(boolean active) {
		this.active = active;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "User [id=" + id + ", FirstName=" + FirstName + ", MiddleName="
				+ MiddleName + ", LastName=" + LastName + ", Email=" + Email
				+ ", Loginname=" + Loginname + ", Password=" + Password
				+ ", activecode=" + activecode + ", active=" + active + "]";
	}	  
	  
}
```

Das eigentliche Programm sieht bei mir wie folgt aus


```
package hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateTest {	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		   SessionFactory sessionFactory;
		   
		   sessionFactory = new Configuration()
                          .configure() // configures settings from hibernate.cfg.xml
                          .buildSessionFactory();
		
	       // A SessionFactory is set up once for an application	    
		   Session session = sessionFactory.openSession();
		   session.beginTransaction();
		   session.save( new User( "myname", "mylastname", "myname@localhost") );
		   session.save( new User( "myname2", "mylastname2", "myname2@localhost") );
		   session.getTransaction().commit();
		   session.close();	
	}

}
```

Aber irgendwas stimmt nicht

Apr 17, 2012 11:20:20 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Apr 17, 2012 11:20:21 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.2.Final}
Apr 17, 2012 11:20:21 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Apr 17, 2012 11:20:21 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Apr 17, 2012 11:20:21 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Apr 17, 2012 11:20:21 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Apr 17, 2012 11:20:21 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace Hibernate - JBoss Community. Use namespace Index of /dtd/hibernate instead. Refer to Hibernate 3.6 Migration Guide!
Apr 17, 2012 11:20:21 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Apr 17, 2012 11:20:21 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Apr 17, 2012 11:20:21 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 10
Apr 17, 2012 11:20:21 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
Apr 17, 2012 11:20:21 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/hibernate]
Apr 17, 2012 11:20:21 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
Apr 17, 2012 11:20:21 AM org.hibernate.engine.jdbc.internal.JdbcServicesImpl configure
WARN: HHH000342: Could not obtain connection to query metadata : No suitable driver found for jdbc:mysql://localhost/hibernate
Apr 17, 2012 11:20:21 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Apr 17, 2012 11:20:21 AM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Apr 17, 2012 11:20:21 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Apr 17, 2012 11:20:21 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Apr 17, 2012 11:20:22 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Apr 17, 2012 11:20:22 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
Apr 17, 2012 11:20:22 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000319: Could not get database metadata
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/hibernate
	at java.sql.DriverManager.getConnection(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192)
	at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
	at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:194)
	at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:178)
	at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:495)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1741)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1779)
	at hibernate.HibernateTest.main(HibernateTest.java:16)

Apr 17, 2012 11:20:22 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000299: Could not complete schema update
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/hibernate
	at java.sql.DriverManager.getConnection(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192)
	at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
	at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:194)
	at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:178)
	at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:495)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1741)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1779)
	at hibernate.HibernateTest.main(HibernateTest.java:16)

Apr 17, 2012 11:20:22 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 08001
Apr 17, 2012 11:20:22 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: No suitable driver found for jdbc:mysql://localhost/hibernate
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Could not open connection
	at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:131)
	at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
	at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
	at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
	at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:304)
	at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
	at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
	at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
	at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1363)
	at hibernate.HibernateTest.main(HibernateTest.java:20)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/hibernate
	at java.sql.DriverManager.getConnection(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192)
	at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:278)
	at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
	... 5 more

Ich suche jetzt schon seit einiger Zeit nach dem Fehler, allerdings finde ich den Fehler absolut nicht. Es liegt vor allen dingen auch daran das ich die Fehlerausgabe nicht so ganz verstehe. Ich hoffe mir kann einer weiterhelfen.


----------



## SlaterB (17. Apr 2012)

hilft zufällig das Vorgehen aus
https://forum.hibernate.org/viewtopic.php?f=1&t=1015157
?

Suche war 'hibernate No suitable driver found for jdbc:mysql://localhost'
vielleicht gibts noch mehr


----------



## Final_Striker (17. Apr 2012)

> und Connector-J 3.0.17 (mysql-connector-java-3.0.17-ga.zip)
> heruntergeladen.



Wo hast du die Version den ausgegraben? Nimm mal lieber die aktuelle 5.x Version.


----------



## Presler (17. Apr 2012)

Herzlichen Dank an euch beiden. Es hat funktioniert, nachdem ich Hibernate 4.1 und mysql-connector-java-5.1.19-bin.jar verwende. Ich hatte wohl die Falsche Version von MySQL Portal runtergeladen.


----------

