基于主键的一对一映射,抛出空指针异常,求指教
类对象:
/**
*
*/
package cn.entity;
/**
* @author 基于主键的被控端
*
*/
public class Citizen {
private int id;
private String name;
public Citizen(){}
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;
}
}
package cn.entity;
//基于主键的主控端
public class IDCard {
private int id;
private String no;
private Citizen citizen;
public IDCard(){}
public Citizen getCritzen() {
return citizen;
}
public void setCritzen(Citizen citizen) {
this.citizen = citizen;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
}
映射文件是:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.entity.Citizen" table="citizen_tab01">
<id name="id" column="c_id" type="integer">
<generator class="native"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.entity.IDCard" table="ID_tab01">
<id name="id" column="c_id" type="integer">
<generator class="foreign">
<param name="property">citizen</param>
</generator>
</id>
<property name="no"/>
<!-- constrained 告诉当前的主键,你的值是另一个表的主键值
当前主键对于另外的表相当于外键
-->
<one-to-one name="citizen" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
测试代码:
package cn.test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
import cn.entity.Citizen;
import cn.entity.IDCard;
public class TestOTO{
@Test
public void create(){
Configuration cfg = new Configuration().configure();
SchemaExport se = new SchemaExport(cfg);
se.setFormat(true).create(true, true);
}
@Test
public void add(){
Session session=null;
Transaction ts = null;
try{
session = HibernateUtils.getSession();
session.beginTransaction();
Citizen c = new Citizen();
c.setName("wangsi");
session.save(c);
IDCard idc = new IDCard();
idc.setNo("123793478");
idc.setCritzen(c);
session.save(idc);
ts.commit();
}catch(Exception e){
if(ts != null){
System.out.println("kong");
}
}finally{
session.close();
}
}
}
工具类:
package cn.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtils {
public static Session getSession(){
Configuration cfg = new Configuration().configure();
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());
ServiceRegistry service = ssrb.build();
SessionFactory sf = cfg.buildSessionFactory(service);
return sf.openSession();
}
}
可以创建表单成功;只是不能添加记录
显示如下异常:java.lang.NullPointerException
at cn.test.TestOTO.add(TestOTO.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
如果把finally的内容去掉,不会出现异常,但是不能添加记录。