今天在hibernate官网上看到一个例子后按照官方的要求一步一步的写下去了到最后测试也是成功了但是我修改了hbm2ddl.auto的配置为create 或create-drop时发现数据库的并没有像官方说的那样自动创建经过研究发现我犯了一个小小的毛病就是没有修改配置中的方言。下面是我测试的代码用例:
1)测试的版本是 hibernate4.2.8
2)用到的jar包
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.8.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
mysql-connector-java-5.1.21-bin.jar
slf4j-api-1.6.1.jar
3)代码
package com.ygc.test.client; import java.util.Date; import org.hibernate.Session; import com.ygc.test.domain.Event; import com.ygc.test.utils.HibernateUtil; public class EventManager { public static void main(String[] args) { EventManager mgr = new EventManager(); mgr.createAndStoreEvent("My Event", new Date()); HibernateUtil.getSessionFactory().close(); } private void createAndStoreEvent(String title, Date theDate) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Event theEvent = new Event(); theEvent.setTitle(title); theEvent.setDate(theDate); session.save(theEvent); session.getTransaction().commit(); } }
package com.ygc.test.domain; import java.util.Date; public class Event { private Long id; private String title; private Date date; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } }
package com.ygc.test.utils; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.ygc.test.domain"> <class name="Event" table="events"> <id name="id" column="id"> <generator class="native" /> </id> <property name="date" type="timestamp" column="date"/> <property name="title"/> </class> </hibernate-mapping>
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/NoteDb</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <mapping resource="com/ygc/test/domain/Event.hbm.xml"/> </session-factory> </hibernate-configuration>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
我的问题主要出现在<property name="dialect">org.hibernate.dialect.MySQLDialect</property> 默认的数据库的方言是:<property name="dialect">org.hibernate.dialect.HSQLDialect</property>按照自己的数据库修改方言就好了。