Hibernate:表中没有填充数据

时间:2014-01-25 11:09:38

标签: hibernate

我编写了一个示例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>

1 个答案:

答案 0 :(得分:0)

我猜你已经创建了这张桌子?表格符合您的要求吗?

我自己正在学习Hibernate,我发现你的代码存在这些问题:

a)@Column(name = "Name")注释应位于Name获取者设置者的顶部。

b)另外,我认为在你的hibernate.cfg.xml中应该说:

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

因为否则,它什么都不做。使用您当前的代码,至少应填充id。 Hibernate文档很好地解释了这一点。

c)此外,如果你看得足够近,你只能用id 2保存用户。

希望这有帮助。