hibernate多对一提示空指针的问题

创建了product和factory两个实体类,字段什么的和数据库的都是一样的,配置文件如下:product.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">















factory.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">










操作查询:
try {
Session session = Hibernateutils.getSession();
session.beginTransaction();
Product product = (Product)session.get(Product.class, new Integer(1));
if(product == null)
{
System.out.println("111");
}
else {
System.out.println(product.getName());
session.getTransaction().commit();
}
} catch (Exception e) {
// TODO 自动生成的 catch 块
System.out.println("错误");
e.printStackTrace();
}
之后控制台就输出:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate: select product0_.id as id0_0_, product0_.name as name0_0_, product0_.price as price0_0_, product0_.factoryid as factoryid0_0_ from product product0_ where product0_.id=?
111

查看全部
u011382188
小珊珊爱吃鱼
2015/09/09 10:41
  • java
  • hibernate
  • 数据库
  • 点赞
  • 收藏
  • 回答
    私信

2个回复