当前位置: 代码迷 >> J2SE >> hiberante报空错误
  详细解决方案

hiberante报空错误

热度:246   发布时间:2016-04-23 20:30:30.0
hiberante报空异常

电影
package cn.demo.entity;

import java.io.Serializable;

import cn.demo.entity.MovieType;

public class Movie implements Serializable {

private int Id;
private String Name;
private MovieType TypeId;
private String Actor;
private String Director;
private int Price;

public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}

public String getActor() {
return Actor;
}
public void setActor(String actor) {
Actor = actor;
}
public String getDirector() {
return Director;
}
public void setDirector(String director) {
Director = director;
}
public int getPrice() {
return Price;
}
public void setPrice(int price) {
Price = price;
}
public MovieType getTypeId() {
return TypeId;
}
public void setTypeId(MovieType typeId) {
TypeId = typeId;
}

}

映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0 //EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  <hibernate-mapping>
  
   <class name="cn.demo.entity.Movie" table="movie" dynamic-update="true">
  
   <id name="Id" type="java.lang.Integer" column="id">
   <generator class="increment"></generator>
   </id>
  
   <property name="Name" type="java.lang.String" column="name"/>
   <property name="Actor" type="java.lang.String" column="actor"/>
   <property name="Director" type="java.lang.String" column="director"/>
   <property name="Price" type="java.lang.Integer" column="price"/>
   <many-to-one name="TypeId"
   column="type_id"
   class="cn.demo.entity.MovieType"
   />
   </class>
  </hibernate-mapping>

电影类型
package cn.demo.entity;

public class MovieType {
private int Type_Id;
private String Name;

public int getType_Id() {
return Type_Id;
}
public void setType_Id(int type_Id) {
Type_Id = type_Id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}

}

映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0 //EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  <hibernate-mapping>
  
   <class name="cn.demo.entity.MovieType" table="movie_type" dynamic-update="true">
  
   <id name="Type_Id" type="java.lang.Integer" column="type_id">
   <generator class="increment"></generator>
   </id>
  
   <property name="Name" type="java.lang.String" column="name"/>
  
   </class>
  </hibernate-mapping>

测试类
package cn.demo.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import cn.demo.entity.Movie;
import cn.demo.entity.MovieType;

public class MovieDao {

Configuration conf=null;
SessionFactory sf=null;
Session session=null;
Transaction tx=null;
public void add(){
try{
conf=new Configuration().configure();
sf=conf.buildSessionFactory();
session=sf.openSession();
MovieType mt=(MovieType)session.load(MovieType.class, 1);
Movie m=new Movie();
m.setId(4);
m.setName("逃学威龙");
m.setPrice(60);
m.setDirector("于晟");
m.setActor("周星驰");
m.setTypeId(mt);
session.save(m);
tx.commit();
System.out.println("OK");
}catch(Exception e){
e.printStackTrace();
tx.rollback();
System.out.println("No");
}
}
}

运行后异常


问下怎么解决 

------解决方案--------------------
tx对象是null。
在你的方法里写tx=session.beginTransaction();