Hi!
I'm new to Hibernate. I've followed all steps from hibernate.org tutorial but using mysql (all steps but ant's since I'm using eclipse) and I got an error.
I checked mysql query browser and found hibernate didn't create the database I specified in hibernate.cfg.xml (hbn1).
Does hibernate create the db and tables??? I though yes since the tutorial doesn't say anything about creating a db manually and since other persistent-layer projects such as torque does create db and tables.
Does any have any idea what do I'm missing??? Here is the error and code I got.
Thx!
error message-----------------------------------------------------------------------------
Code:
00:10:48,156 INFO Environment:509 - Hibernate 3.2.3
00:10:48,171 INFO Environment:542 - hibernate.properties not found
00:10:48,171 INFO Environment:676 - Bytecode provider name : cglib
00:10:48,171 INFO Environment:593 - using JDK 1.4 java.sql.Timestamp handling
00:10:48,265 INFO Configuration:1426 - configuring from resource: /hibernate.cfg.xml
00:10:48,265 INFO Configuration:1403 - Configuration resource: /hibernate.cfg.xml
00:10:48,359 DEBUG DTDEntityResolver:38 - trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd]
00:10:48,359 DEBUG DTDEntityResolver:40 - recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
00:10:48,359 DEBUG DTDEntityResolver:50 - located [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd] in classpath
00:10:48,468 DEBUG Configuration:1387 - connection.driver_class=org.gjt.mm.mysql.Driver
00:10:48,468 DEBUG Configuration:1387 - connection.url=jdbc:mysql://localhost:3306/hbn1
00:10:48,468 DEBUG Configuration:1387 - connection.username=root
00:10:48,468 DEBUG Configuration:1387 - connection.password=rootpswd
00:10:48,468 DEBUG Configuration:1387 - cache.provider_class=org.hibernate.cache.NoCacheProvider
00:10:48,468 DEBUG Configuration:1387 - dialect=org.hibernate.dialect.MySQLDialect
00:10:48,468 DEBUG Configuration:1387 - connection.pool_size=1
00:10:48,468 DEBUG Configuration:1387 - current_session_context_class=thread
00:10:48,468 DEBUG Configuration:1387 - show_sql=true
00:10:48,468 DEBUG Configuration:1387 - hbm2ddl.auto=create
00:10:48,468 DEBUG Configuration:1586 - null<-org.dom4j.tree.DefaultAttribute@158b649 [Attribute: name resource value "hibernate/events/Event.hbm.xml"]
00:10:48,468 INFO Configuration:553 - Reading mappings from resource : hibernate/events/Event.hbm.xml
00:10:48,484 DEBUG DTDEntityResolver:38 - trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd]
00:10:48,484 DEBUG DTDEntityResolver:40 - recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
00:10:48,484 DEBUG DTDEntityResolver:50 - located [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd] in classpath
00:10:48,656 INFO HbmBinder:300 - Mapping class: hibernate.events.Event -> events
00:10:48,671 DEBUG HbmBinder:1270 - Mapped property: id -> EVENT_ID
00:10:48,687 DEBUG HbmBinder:1270 - Mapped property: date -> EVENT_DATE
00:10:48,687 DEBUG HbmBinder:1270 - Mapped property: title -> EVENT_TITLE
00:10:48,687 DEBUG HbmBinder:1270 - Mapped property: duration -> EVENT_DURATION
00:10:48,687 INFO Configuration:1541 - Configured SessionFactory: null
00:10:48,687 DEBUG Configuration:1542 - properties: {java.vendor=Sun Microsystems Inc., show_sql=true, sun.java.launcher=SUN_STANDARD, hibernate.connection.url=jdbc:mysql://localhost:3306/hbn1, sun.management.compiler=HotSpot Client Compiler, hbm2ddl.auto=create, os.name=Windows XP, sun.boot.class.path=C:\Archivos de programa\Java\jre1.6.0_01\lib\resources.jar;C:\Archivos de programa\Java\jre1.6.0_01\lib\rt.jar;C:\Archivos de programa\Java\jre1.6.0_01\lib\jsse.jar;C:\Archivos de programa\Java\jre1.6.0_01\lib\jce.jar;C:\Archivos de programa\Java\jre1.6.0_01\lib\charsets.jar, hibernate.current_session_context_class=thread, sun.desktop=windows, java.vm.specification.vendor=Sun Microsystems Inc., java.runtime.version=1.6.0_01-b06, hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider, user.name=Enyel, connection.driver_class=org.gjt.mm.mysql.Driver, current_session_context_class=thread, user.language=es, sun.boot.library.path=C:\Archivos de programa\Java\jre1.6.0_01\bin, dialect=org.hibernate.dialect.MySQLDialect, java.version=1.6.0_01, user.timezone=America/Mexico_City, sun.arch.data.model=32, java.endorsed.dirs=C:\Archivos de programa\Java\jre1.6.0_01\lib\endorsed, sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86, sun.jnu.encoding=Cp1252, file.encoding.pkg=sun.io, file.separator=\, java.specification.name=Java Platform API Specification, java.class.version=50.0, user.country=MX, connection.url=jdbc:mysql://localhost:3306/hbn1, java.home=C:\Archivos de programa\Java\jre1.6.0_01, java.vm.info=mixed mode, sharing, os.version=5.1, path.separator=;, connection.password=rootpswd, java.vm.version=1.6.0_01-b06, hibernate.connection.password=rootpswd, user.variant=, java.awt.printerjob=sun.awt.windows.WPrinterJob, sun.io.unicode.encoding=UnicodeLittle, awt.toolkit=sun.awt.windows.WToolkit, hibernate.connection.username=root, user.home=C:\Documents and Settings\Enyel, java.specification.vendor=Sun Microsystems Inc., hibernate.hbm2ddl.auto=create, java.library.path=C:\Archivos de programa\Java\jre1.6.0_01\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Archivos de programa\Intel\Wireless\Bin\;C:\Archivos de programa\Archivos comunes\Adobe\AGL;C:\Archivos de programa\QuickTime\QTSystem\;.;C:\Archivos de programa\Java\jdk1.6.0_01\bin;C:\Archivos de programa\Apache Software Foundation\Maven 1.0.2\bin;, java.vendor.url=http://java.sun.com/, hibernate.connection.driver_class=org.gjt.mm.mysql.Driver, connection.username=root, java.vm.vendor=Sun Microsystems Inc., hibernate.dialect=org.hibernate.dialect.MySQLDialect, java.runtime.name=Java(TM) SE Runtime Environment, java.class.path=D:\apache-tomcat-6.0.10\webapps\test\WEB-INF\classes;D:\apache-tomcat-6.0.10\webapps\test\WEB-INF\lib\itext-2.0.1.jar;D:\apache-tomcat-6.0.10\webapps\test\WEB-INF\lib\jstl.jar;D:\apache-tomcat-6.0.10\webapps\test\WEB-INF\lib\standard.jar;D:\apache-tomcat-6.0.10\webapps\test\WEB-INF\lib\mysql-connector-java-5.0.4-bin.jar;D:\Componentes\apache\jakarta\commons\proper\collections\commons-collections-3.2\commons-collections-3.2.jar;D:\Componentes\apache\jakarta\commons\proper\configuration\commons-configuration-1.3\commons-configuration-1.3.jar;D:\Componentes\apache\jakarta\commons\proper\logging\commons-logging-1.1\commons-logging-1.1.jar;D:\Componentes\hibernate\hibernate-3.2\lib\antlr-2.7.6.jar;D:\Componentes\hibernate\hibernate-3.2\lib\asm.jar;D:\Componentes\hibernate\hibernate-3.2\lib\asm-attrs.jar;D:\Componentes\hibernate\hibernate-3.2\lib\cglib-2.1.3.jar;D:\Componentes\hibernate\hibernate-3.2\lib\dom4j-1.6.1.jar;D:\Componentes\hibernate\hibernate-3.2\lib\jta.jar;D:\Componentes\hibernate\hibernate-3.2\hibernate3.jar;D:\Componentes\itext\itext-2.0.1.jar;D:\Componentes\apache\logging\logging-log4j-1.2.14\dist\lib\log4j-1.2.14.jar;D:\apache-tomcat-6.0.10\lib\servlet-api-2.5.jar, hibernate.bytecode.use_reflection_optimizer=false, java.vm.specification.name=Java Virtual Machine Specification, java.vm.specification.version=1.0, sun.cpu.endian=little, sun.os.patch.level=Service Pack 2, connection.pool_size=1, java.io.tmpdir=C:\DOCUME~1\Enyel\CONFIG~1\Temp\, java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi, os.arch=x86, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.ext.dirs=C:\Archivos de programa\Java\jre1.6.0_01\lib\ext;C:\WINDOWS\Sun\Java\lib\ext, user.dir=D:\apache-tomcat-6.0.10\webapps\test, line.separator=
, java.vm.name=Java HotSpot(TM) Client VM, cache.provider_class=org.hibernate.cache.NoCacheProvider, file.encoding=Cp1252, java.specification.version=1.6, hibernate.show_sql=true, hibernate.connection.pool_size=1}
00:10:48,687 DEBUG Configuration:1285 - Preparing to build session factory with filters : {}
00:10:48,687 DEBUG Configuration:1120 - processing extends queue
00:10:48,687 DEBUG Configuration:1124 - processing collection mappings
00:10:48,687 DEBUG Configuration:1135 - processing native query and ResultSetMapping mappings
00:10:48,687 DEBUG Configuration:1143 - processing association property references
00:10:48,687 DEBUG Configuration:1165 - processing foreign key constraints
00:10:48,796 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
00:10:48,796 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 1
00:10:48,796 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
00:10:48,812 INFO DriverManagerConnectionProvider:80 - using driver: org.gjt.mm.mysql.Driver at URL: jdbc:mysql://localhost:3306/hbn1
00:10:48,812 INFO DriverManagerConnectionProvider:83 - connection properties: {user=root, password=rootpswd}
00:10:48,812 DEBUG DriverManagerConnectionProvider:109 - opening new JDBC connection
00:10:49,015 WARN SettingsFactory:117 - Could not obtain connection metadata
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown database 'hbn1'
...
... more stuff here
...
Code:
00:10:49,625 INFO SchemaExport:154 - Running hbm2ddl schema export
00:10:49,625 DEBUG SchemaExport:170 - import file not found: /import.sql
00:10:49,625 INFO SchemaExport:179 - exporting generated schema to database
00:10:49,625 DEBUG DriverManagerConnectionProvider:109 - opening new JDBC connection
00:10:49,640 ERROR SchemaExport:202 - schema export unsuccessful
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown database 'hbn1'
...
more stuff...
...
Code:
00:10:49,765 WARN JDBCExceptionReporter:77 - SQL Error: 1049, SQLState: 42000
00:10:49,765 ERROR JDBCExceptionReporter:78 - Unknown database 'hbn1'
hibernate.cfg.xml--------------------------------------------------------------------------
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<!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="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hbn1</property>
<property name="connection.username">root</property>
<property name="connection.password">rootpswd</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="hibernate/events/Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Event.hbm.xml------------------------------------------------------------------------------
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibernate.events">
<class name="Event" table="events">
<id name="id" column="EVENT_ID" type="long" unsaved-value="null">
<generator class="native"/>
</id>
<property name="date" column="EVENT_DATE" type="date"/>
<property name="title" column="EVENT_TITLE" type="string" length="100"/>
<property name="duration" column="EVENT_DURATION" type="integer" />
</class>
</hibernate-mapping>
EventManager.java------------------------------------------------------------------------
Code:
package hibernate.events;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.Date;
import java.util.List;
import hibernate.util.HibernateUtil;
public class EventManager {
public static void main(String[] args) {
Configuration cfg = new Configuration();
SessionFactory factory = cfg.configure().buildSessionFactory();
Session session = factory.openSession();
Event e = new Event();
e.setDate(new Date());
e.setTitle("Mi prueba");
e.setDuration(10);
session.save(e);
session.flush();
session.close();
factory.close();
}
}