我在maven项目中使用hibernate 4.3.11和struts2 2.3.33,我试图用mysql测试我的hibernate,然后弹出Exception,似乎我的xml文件格式正确,并且也没有其他地方有我可以找到的重复资源映射,所以我真的需要一些帮助,感谢任何建议。
这是我的目录 http://imgur.com/PfO1mj5
的hibernate.cfg.xml
<session-factory>
<property name="show_sql">true</property>
<property name="myeclipse.connection.profile">bookshop</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/Bookshop?useSSL=false
</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/hibtest2/entity/Users.hbm.xml" />
</session-factory>
<?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.hibtest2.entity.Users" table="users" catalog="bookshop">
<id name="id" type="java.lang.Integer">
<column name="Id" />
<generator class="native"></generator>
</id>
<property name="loginName" type="java.lang.String">
<column name="LoginName" length="50" />
</property>
<property name="loginPwd" type="java.lang.String">
<column name="LoginPwd" length="16" />
</property>
<property name="name" type="java.lang.String">
<column name="Name" length="16" />
</property>
<property name="address" type="java.lang.String">
<column name="Address" length="16" />
</property>
<property name="phone" type="java.lang.String">
<column name="Phone" length="16" />
</property>
<property name="mail" type="java.lang.String">
<column name="Mail" length="16" />
</property>
<property name="userRoleId" type="java.lang.Integer">
<column name="UserRoleId" length="4" />
</property>
<property name="userStateId" type="java.lang.Integer">
<column name="UserStateId" length="4" />
</property>
</class>
</hibernate-mapping>
这是我的测试课 Test.java
package com.hibtest2;
import com.hibtest2.dao.UserDAO;
import com.hibtest2.dao.UserDAOImpl;
import com.hibtest2.entity.Users;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Test test = new Test();
// 依次測試testAdd、testDelete和testUpdate方法
// test.testAdd();
// test.testDelete(new Integer(9));
test.testUpdate();
}
// 加入資料
public void testAdd() {
Users users = new Users();
users.setLoginName("New York");
users.setLoginPwd("123456");
UserDAO userDao = new UserDAOImpl();
userDao.add(users);
}
// 移除資料
public void testDelete(Integer id) {
UserDAO userDao = new UserDAOImpl();
Users users = userDao.load(id);
userDao.delete(users);
}
// 修改資料
public void testUpdate() {
UserDAO userDao = new UserDAOImpl();
Users users = userDao.load(new Integer(2));
users.setLoginPwd("210000");
userDao.update(users);
}
}
UserDAOImpl.java
package com.hibtest2.dao;
import java.util.List;
import com.hibtest2.entity.Users;
public class UserDAOImpl extends BaseHibernateDAO implements UserDAO {
//加入資料
public void add(Users users) {
super.add(users);
}
//移除資料
public void delete(Users users) {
super.delete(users);
}
//載入資料
public Users load(Integer id) {
return (Users)super.get(Users.class, id);
}
//修改資料
public void update(Users users) {
super.update(users);
}
//登入驗證
public boolean validate(String loginName, String loginPwd) {
// TODO Auto-generated method stub
boolean flag=false;
//封裝查詢條件
Users condition=new Users();
condition.setLoginName(loginName);
condition.setLoginPwd(loginPwd);
//呼叫BaseHibernateDAO類別中的search方法
List list=super.search(Users.class, condition);
if(list.size()>0){
flag=true;
}
return flag;
}
}
BaseHibernateDAO.java
package com.hibtest2.dao;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import com.hibtest2.HibernateSessionFactory;
import com.sun.org.apache.regexp.internal.recompile;
public abstract class BaseHibernateDAO {
protected void add(Object object) {
Transaction tran = null;
// 取得session
Session session = HibernateSessionFactory.getSession();
try {
// 開始交易
tran = session.beginTransaction();
// 持久化動作
session.save(object);
// 傳送交易
tran.commit();
} catch (Exception e) {
if (tran != null) {
// 交易返回
tran.rollback();
}
e.printStackTrace();
} finally {
// 關閉session
session.close();
}
}
protected Object get(Class cla, Serializable id) {
Object object = null;
Session session = HibernateSessionFactory.getSession();
try {
object = session.get(cla, id);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return object;
}
protected void delete(Object object) {
Transaction tran = null;
Session session = HibernateSessionFactory.getSession();
try {
tran = session.beginTransaction();
session.delete(object);
tran.commit();
} catch (Exception e) {
if (tran != null) {
tran.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
}
protected void update(Object object) {
Transaction tran = null;
Session session = HibernateSessionFactory.getSession();
try {
tran = session.beginTransaction();
session.update(object);
tran.commit();
} catch (Exception e) {
if (tran != null) {
tran.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
}
protected List search(Class cla, Object condition) {
Session session = null;
List list = null;
try {
session = HibernateSessionFactory.getSession();
list = session.createCriteria(cla).add(Example.create(condition)).list();
} catch (Exception e) {
// TODO: handle exception
} finally {
session.close();
}
return list;
}
}
和最后一个HibernateSessionFactory.java
package com.hibtest2;
import org.hibernate.HibernateException;
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 HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession() : null;
threadLocal.set(session);
}
return session;
}
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
public static Configuration getConfiguration() {
return configuration;
}
}
答案 0 :(得分:0)
InvalidMappingException
是NPE
的结果,可能是sessionFactory
永远不会在rebuildSessionFactory
HibernateSessionFactory
中分配的sessionFactory = configuration.buildSessionFactory(serviceRegistry); // local scope SessionFactory removed
造成的。
/usr/local/nodejs-binary-6.3.0/bin/