Person.java和PersonId.java是myeclipse6.0自动生成的俩文件就是getter和setter方法们(Person.java啥用哦)
自己编写的PersonOperator类
package com.abing.hp.hibernate;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;public class PersonOperator {
Session session = null;
public void PersonOperator()
{
Configuration config = new Configuration().configure();
SessionFactory factory = config.buildSessionFactory();
this.session = factory.openSession();
}
public void insert(Person p)
{
Transaction tran = this.session.beginTransaction();
this.session.save(p);
tran.commit();
this.session.close();
}
}
自己编写的测试类testHibernate.java
package com.abing.hp.hibernate;public class TestHibernate {
public static void main(String[] args) {
PersonId pid = new PersonId();
Person p = new Person();
pid.setId("hello");
pid.setName("world");
pid.setPassword("123");
pid.setEmail("[email protected]");
p.setId(pid);
PersonOperator po = new PersonOperator();
po.insert(p); }}
Person.hbm.xml
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.abing.hp.hibernate.Person" table="person" catalog="test">
<composite-id name="id" class="com.abing.hp.hibernate.PersonId">
<key-property name="id" type="string">
<column name="id" length="32" />
</key-property>
<key-property name="name" type="string">
<column name="name" length="20" />
</key-property>
<key-property name="password" type="string">
<column name="password" length="30" />
</key-property>
<key-property name="email" type="string">
<column name="email" length="50" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>
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"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<property name="myeclipse.connection.profile">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="com/abing/hp/hibernate/Person.hbm.xml" /> </session-factory></hibernate-configuration>
异常
Exception in thread "main" java.lang.NullPointerException
at com.abing.hp.hibernate.PersonOperator.insert(PersonOperator.java:18)
at com.abing.hp.hibernate.TestHibernate.main(TestHibernate.java:15)
麻烦各位!
自己编写的PersonOperator类
package com.abing.hp.hibernate;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;public class PersonOperator {
Session session = null;
public void PersonOperator()
{
Configuration config = new Configuration().configure();
SessionFactory factory = config.buildSessionFactory();
this.session = factory.openSession();
}
public void insert(Person p)
{
Transaction tran = this.session.beginTransaction();
this.session.save(p);
tran.commit();
this.session.close();
}
}
自己编写的测试类testHibernate.java
package com.abing.hp.hibernate;public class TestHibernate {
public static void main(String[] args) {
PersonId pid = new PersonId();
Person p = new Person();
pid.setId("hello");
pid.setName("world");
pid.setPassword("123");
pid.setEmail("[email protected]");
p.setId(pid);
PersonOperator po = new PersonOperator();
po.insert(p); }}
Person.hbm.xml
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.abing.hp.hibernate.Person" table="person" catalog="test">
<composite-id name="id" class="com.abing.hp.hibernate.PersonId">
<key-property name="id" type="string">
<column name="id" length="32" />
</key-property>
<key-property name="name" type="string">
<column name="name" length="20" />
</key-property>
<key-property name="password" type="string">
<column name="password" length="30" />
</key-property>
<key-property name="email" type="string">
<column name="email" length="50" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>
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"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<property name="myeclipse.connection.profile">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="com/abing/hp/hibernate/Person.hbm.xml" /> </session-factory></hibernate-configuration>
异常
Exception in thread "main" java.lang.NullPointerException
at com.abing.hp.hibernate.PersonOperator.insert(PersonOperator.java:18)
at com.abing.hp.hibernate.TestHibernate.main(TestHibernate.java:15)
麻烦各位!
解决方案 »
- urlrewrite 中文乱码问题
- Servlet中设置的alert窗口失败
- 新手提问getBytes(1)时什么
- 有谁考系统分析师的,可以在这里交流交流吗?你们认为看什么资料好呢
- 高分请教jsp生成验证码的问题!谢谢
- 【错误】The requested resource (/addressbook/logon.jsp) is not available?
- 一个excel转xml的程序错误,哪位给帮忙解决一下阿?
- 帮忙看看这个小程序(99表)为什么不会换行
- PreparedStatement和Statement有什么区别
- 哪有tomcat下载啊?????
- 关于JAVA接口中的疑问
- 关于页面的一些问题(不知道是跳转出问题,还是其他出问题)
p.setId(pid);
PersonOperator po = new PersonOperator();
po.insert(p);
你在this.session.save(p); 这里只把pid保存了 不知道是不是这的问题```你这样写 Person p = new Person();
p.setId("hello");
p.setName("world");
p.setPassword("123");
p.setEmail("[email protected]");
PersonOperator po = new PersonOperator();
po.insert(p); 你试一下 看能不能行```
id,name,password,email都得有值,你只给p.setId(pid); 其它的没有给值.
这个类只有id属性