2014-01-25 174 views
0

我寫了一個示例Hibernate程序,當我運行該程序時,創建了包含列的表格,但數據未被填充到表格中。可以請某人幫助我。休眠:表中沒有填充數據

My model bean is : 

import java.util.HashSet; 
import java.util.Set; 

import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table; 
@Entity 
@Table(name="student") 
public class student { 
private String name; 
private int id; 


public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
@Id 
public int getId() { 
    return id; 
} 
public void setId(int id) { 
    this.id = id; 
} 

} 

類,其中配置和會話寫的是:

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.AnnotationConfiguration; 
import org.hibernate.tool.hbm2ddl.SchemaExport; 
import com.hibernate.data.Employee; 

public class Test { 
    public static void main(String[]args) 
    { 
    AnnotationConfiguration config=new AnnotationConfiguration(); 
    config.addAnnotatedClass(student.class); 
    config.configure("hibernate.cfg.xml"); 
    new SchemaExport(config).create(true, true); 
    SessionFactory factory=config.buildSessionFactory(); 
    Session session=factory.getCurrentSession(); 
    session.beginTransaction(); 
    student s =new student(); 
    s.setId(1); 
    s.setName("vamsi"); 
    s.setId(2); 
    s.setName("krishna"); 
    subjects s1 =new subjects(); 
    session.save(s); 
    session.getTransaction().commit(); 
    } 
} 

我的hibernate.cfg.xml是:

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 

    <session-factory> 

     <!-- Database connection settings --> 
     <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> 
     <property name="connection.url">jdbc:hsqldb:C:\Users\krishna\Desktop\hsqldb-2.2.6</property> 
     <property name="connection.username">sa</property> 
     <property name="connection.password"></property> 


     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">2</property> 

     <!-- SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.HSQLDialect</property> 

     <!-- Enable Hibernate's current session context --> 
     <property name="current_session_context_class">thread</property> 

     <!-- Disable the second-level cache --> 
     <property name="cache.provider_class">org.hibernate.cache.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="org/hibernate/tutorial/domain/Event.hbm.xml"/> 
     <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> 
    --> 


    </session-factory> 

</hibernate-configuration> 
+0

這至少會創建一個id = 2且沒有名字的學生? – JCalcines

+0

我做了更改並保存了id = 1和id = 2的兩個對象 – Kris27

回答

0

我猜你已經創建的表?而且桌子是按照您的要求?

我自己在學習Hibernate的,我覺得這些與您的代碼幾個問題:

一)@Column(name = "Name")註釋應該有你的Name吸氣制定者的頂部。

B)另外,我覺得在你的hibernate.cfg.xml,應該說:

<property name="hbm2ddl.auto">update</property> 

因爲否則,它什麼都不做。用你現在的代碼,它應該至少填充了id。 Hibernate文檔很好地解釋了這一點。 c)除此之外,如果你看起來足夠接近,你只需要保存用戶id 2。

希望這會有所幫助。

+0

我做了更改並保存了id = 1和id = 2的兩個對象,並在hibernate.cfg.xml中添加了條目,但仍未更新數據。 – Kris27

+0

我仍然認爲你的hibernate.cfg.xml有問題。在「connection.url」屬性中,你應該把URL提供給你的數據庫。例如,我的看起來像: ** jdbc:mysql:// localhost:3306/someDB **,其中** someDB **是我的數據庫。你確定你沒有收到錯誤嗎? – ystark

+0

另外,在代碼中加入這一行,並帶有正確的軟件包引用:'config.addAnnotatedClass(com.something.student.class);'在SchemaExport行之前的某處。另外,再次查看代碼後,我意識到hibernate.cfg.xml中的更新不是必需的。 – ystark