2011-06-02 51 views
0

我是hibernate的新手,我正在使用hibernate 3.0與MySQL,並且希望使用插入更新和刪除數據庫等簡單的數據庫操作。我獲得了插入成功的結果並刪除但無法更新特定字段。 我有持久類名爲Employee與Fname,Lname,Id和郵件與getters和setter。無法在休眠狀態下更新數據庫

這是我的主要方法: -

public static void main(String[] args) { 
    Session session = null; 
    Random r = new Random(); 
    try { 
     /* 
     * This step will read hibernate.cfg.xml 
     * 
     * and prepare hibernate for use 
     */ 
     SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
     session = sessionFactory.openSession(); 
     Transaction trx = session.beginTransaction(); 
     Contact contact = new Contact(); 
     contact = (Contact) session.get(Contact.class, new Long(1)); 
     // Create new instance of Contact and set 
     // values in it by reading them from form object 
     System.out.println("Inserting Record"); 
     contact.setId(r.nextLong() % 100); 
     contact.setFirstName("123anand"); 
     contact.setLastName("nandurbarkar"); 
     contact.setEmail("[email protected]"); 
     trx.commit(); 
     session.update(contact);  

,這是我的conf文件: -

<hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatetutorial</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.connection.password">root</property> 
    <property name="hibernate.connection.pool_size">1</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="contact.hbm.xml"/> 
</session-factory> 
</hibernate-configuration> 

請幫助如果ü知道如何解決這個問題。 在此先感謝...

回答

0

我認爲你需要你提交會議之前做你的更新

所以各地的交換你的最後兩行應使其發揮作用: -

session.update(contact); 
trx.commit();  
2

你不能更改實體的ID。如果你想看看其他屬性的更新是如何工作的,請試試這個:

public static void main(String[] args) { 
    Session session = null; 
    Random r = new Random(); 
    try { 
     /* 
     * This step will read hibernate.cfg.xml 
     * 
     * and prepare hibernate for use 
     */ 
     SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
     session = sessionFactory.openSession(); 
     Transaction trx = session.beginTransaction(); 
     Contact contact = new Contact(); 

     // Create new instance of Contact and set 
     // values in it by reading them from form object 
     System.out.println("Inserting Record"); 
     contact.setId(r.nextLong() % 100); 
     contact.setFirstName("123anand"); 
     contact.setLastName("nandurbarkar"); 
     contact.setEmail("[email protected]"); 
     // this makes contact persistent. No SQL commands yet 
     session.persist(contact); 
     // sync the session. Here you get a SQL INSERT 
     session.flush(); 

     // change some properties 
     contact.setFirstName("ugo"); 

     // sync again.Here you get a SQL UPDATE 
     session.flush() 

     // Now, let's say you want to update an existing instance/record 
     // with id = 123 
     // retrieve the instance 
     Contact c = session.get(Contact.class, new Long(123)); 
     c.setFirstName("new name"); 

     // sync the session with the db 
     session.flush(); // <- here you get SQL UPDATE 

     trx.commit(); 
+0

Ya同意這一點。但是如果我想更新某個記錄呢?可以說,如果Id = 1,聯繫人名稱應該改爲「abc」。那麼我該如何實現呢?爲此,我不應該使用這個: - contact =(Contact)session.get(Contact.class,new Long(1)); – Ved 2011-06-04 05:14:54

+0

是的,你應該使用session.get()。爲了舉例說明,我在代碼中添加了幾行代碼。其他選項是使用HQL查詢來檢索實例。這是Hibernate的基本用法,你可以在優秀的參考文檔中找到更多的用例和樣例。 – 2011-06-04 08:28:52

相關問題