我試圖開始使用Hibernate,但由於某種原因無法插入數據。它似乎工作正常,因爲沒有給出錯誤,但是當我檢查數據庫時表是空的。我不認爲它與數據庫本身的連接失敗,因爲當我在映射xml中將表名稱更改爲一個不感興趣的表名時,Hibernate會即時創建它(但正如我所說的,沒有插入任何數據) 。有誰知道是什麼問題?數據沒有使用hibernate插入,但沒有錯誤?
這裏是我的代碼:
的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:3306/intex</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>
<!-- Mapping files -->
<mapping resource="intex.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我的XML映射:
<hibernate-mapping>
<class name="com.intex.uni.courses.Course" table="course">
<id name="id" column="id" >
<generator class="increment"/>
</id>
<property name="name" column="name" />
<property name="description" column="description" />
<property name="url" column="url" />
<property name="code" column="code" />
</class>
</hibernate-mapping>
我的測試客戶端:
public class HibernateTest {
public static void main(String[] args) {
Session session = null;
try {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
Course course = new Course();
course.setDescription("Description");
course.setName("NAME");
course.setUrl("http://www.url.com");
session.save(course);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.flush();
session.close();
}
}
}
我真的希望有人能幫助我!在此先感謝:)
你還可以發佈Hibernate生成的SQL嗎? – 2010-12-13 16:43:49