我寫了一個示例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>
這至少會創建一個id = 2且沒有名字的學生? – JCalcines
我做了更改並保存了id = 1和id = 2的兩個對象 – Kris27