2011-06-11 145 views
-1
持續簡單對象

我有以下模型類:問題在休眠

package com.swaranga.model; 

public final class Book 
{ 
    private Long id; 
    private String title; 
    private String isbn; 

    public Book(){} 

    public Book(String title, String isbn) 
    { 
     this.title = title; 
     this.isbn = isbn; 
    } 

    public final Long getId(){ 
     return id; 
    } 

    private final void setId(Long id) 
    { 
     this.id = id; 
    } 


    public final String getTitle() 
    { 
     return title; 
    } 


    public final void setTitle(String title) 
    { 
     this.title = title; 
    } 


    public final String getIsbn() 
    { 
     return isbn; 
    } 


    public final void setIsbn(String isbn) 
    { 
     this.isbn = isbn; 
    } 
    //assume valid equals and hashcode 
} 

的具有以下映射文件Book.hbm.xml:

<hibernate-mapping> 

     <class name="com.swaranga.model.Book" table="book"> 
      <id name="id" type="long"> 
       <generator class="native"/> 
      </id> 

      <property name="title" column="title" type="string"/> 

      <property name="isbn" column="isbn" type="string"/> 
     </class> 

</hibernate-mapping> 

以下數據庫模式:

CREATE TABLE `hibernatetest`.`book` (
     `id` int(10) unsigned NOT NULL auto_increment, 
     `title` varchar(45) NOT NULL, 
     `isbn` varchar(45) NOT NULL, 
     PRIMARY KEY (`id`) 
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

以下休眠配置文件:

<hibernate-configuration> 
    <session-factory> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver </property>   
     <property name="connection.url">jdbc:mysql://localhost:3306/HibernateTest </property> 

     <property name="connection.username"> root </property> 

     <property name="connection.password"> latitude </property> 

     <property name="show_sql">true</property> 

     <property name="dialect">org.hibernate.dialect.MySQLDialect </property> 

     <property name="myeclipse.connection.profile">mysql</property> 

     <mapping resource="com\swaranga\model\Book.hbm.xml" /> 
    </session-factory> 
</hibernate-configuration> 

最後下面的代碼堅持一個Book對象:

public class Main 
{ 
    public static void main(String[] args) 
    { 
     File f = new File("hibernate.cfg.xml");  
     Configuration cfg = new Configuration(); 
     SessionFactory sessionFactory = cfg.configure(f).buildSessionFactory();  
     Session s = sessionFactory.openSession();  
     s.beginTransaction();  
     s.save(new Book("JDBC", "[email protected]#"));  
     s.flush();  
     s.disconnect(); 
    } 
} 

我得到在控制檯下面的輸出:

Hibernate: insert into book (title, isbn) values (?, ?) 

但是,當我檢查我的數據庫中,有沒有條目。

對於這樣一個長期的問題抱歉,但我覺得有必要提供所有的細節。請幫忙。提前致謝。

回答

3

嘗試編輯你的主要方法結束這樣:

Transaction tx = s.beginTransaction();  
s.save(new Book("JDBC", "[email protected]#"));  
tx.commit(); 
s.flush();  
s.disconnect(); 
+0

明白了。我不得不在s.save()之後說s.getTransaction()。commit();''現在它就像一個魅力!謝謝! – 2011-06-11 01:32:38

+0

很高興工作! :) – 2011-06-11 01:33:07

+1

我的第一個Hibernate程序。哇噢! – 2011-06-11 01:34:20

0

「auto」是一個有效的生成器類嗎?

未列出的文檔的O/R映射的一部分,並從它是什麼樣子,你正在做的你是不是在代碼或數據庫生成ID:

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html

我如果你想分配id,或者做一個身份生成器讓MySQL處理它,它會看本地的。

+0

編輯我的問題。 – 2011-06-11 01:01:37

+0

您是否嘗試過該程序? 「 」無法解釋ID生成器策略:自動「 」暗示它仍然認爲您使用自動。 – 2011-06-11 01:04:04

+0

另外,要使用標識,您需要指定id列以在數據庫中自動遞增。 – 2011-06-11 01:05:13

0

你有沒有嘗試ID類型「長」?

+0

我做到了。查看編輯的問題。 – 2011-06-11 01:02:27

0

您尚未將數據庫的標識設置爲自動增量。試試看。應該管用。

+0

我做到了。現在有一個不同的問題。查看編輯的問題。 – 2011-06-11 01:20:46