类文件
package cn.jmi.model;
public class Person {
private int id;
private String name;
private float height;
void person()
{}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
}
映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.jmi.model">
<class name="Person" table="Person">
<id name="id">
<generator class="assigned" />
</id>
<property name="name"/>
<property name="height"/>
</class>
</hibernate-mapping>
配置文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost;database=user</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.password">111111</property>
<property name="hbm2ddl.auto">validate</property>
<mapping resource="cn/jmi/model/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
测试类
package test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.jmi.model.Person;
public class Test extends HibernateDaoSupport{
public List<Person> test()
{
List<Person> l = (List<Person>)getHibernateTemplate().find("from Person");
//select * from dept
return l;
}
public void test1()
{
Person person = (Person)getHibernateTemplate().get(Person.class, 1);
System.out.println(person.getName());
}
public static void main(String[] args) {
/*List<Person> l= new Test().test();
for(Person p:l)
{
System.out.println(p.getId()+","+p.getName());
}*/
//new Test().test1();
//System.out.print(new Test().test().get(0).getName()) ;
Configuration config = new Configuration();
config.configure();
SessionFactory sf = config.buildSessionFactory();
new Test().test1();
Session s = sf.openSession();
// System.out.println(new Test().test().get(0).getHeight());
s.close();
System.out.println("end");
}
}
错误
Exception in thread "main" java.lang.NullPointerException
at test.Test.test1(Test.java:21)
at test.Test.main(Test.java:35)