public Member memLogin(String loginName, String loginPwd) throws Exception {
Session session = MySessionFactory.getSession();
Transaction tx = null;
Member mem = null;
try{
String hql = "select a from Member as a where a.loginName=:loginName and a.loginPwd=:loginPwd";
Query query = session.createQuery(hql);
query.setString("loginName", loginName);
query.setString("loginPwd", loginPwd);
query.setMaxResults(1);
tx = session.beginTransaction();
mem = (Member)query.uniqueResult();
mem.setLoginTimes(Integer.valueOf(mem.getLoginTimes().intValue()+1));
mem.setLastDate(new Date());
session.update(mem);
tx.commit();
}catch(Exception ex){
if(tx!=null)tx.rollback();
ex.printStackTrace();
}finally{
MySessionFactory.closeSession();
}
return mem;
}Query query = session.createQuery(hql);抛出异常,“java.lang.NullPointerException”
高手指教
Session session = MySessionFactory.getSession();
Transaction tx = null;
Member mem = null;
try{
String hql = "select a from Member as a where a.loginName=:loginName and a.loginPwd=:loginPwd";
Query query = session.createQuery(hql);
query.setString("loginName", loginName);
query.setString("loginPwd", loginPwd);
query.setMaxResults(1);
tx = session.beginTransaction();
mem = (Member)query.uniqueResult();
mem.setLoginTimes(Integer.valueOf(mem.getLoginTimes().intValue()+1));
mem.setLastDate(new Date());
session.update(mem);
tx.commit();
}catch(Exception ex){
if(tx!=null)tx.rollback();
ex.printStackTrace();
}finally{
MySessionFactory.closeSession();
}
return mem;
}Query query = session.createQuery(hql);抛出异常,“java.lang.NullPointerException”
高手指教
解决方案 »
- 求教DisplayTag如何实现总列和分列
- struts获取不到日期控件的值
- spring的aop配置完怎么没反应
- Word文件流的读取和写入
- 有关JFreeChart的问题
- struts打印或导出EXCEL
- 用java来写手机与电脑通信的程序
- struts菜鸟问题,都来帮我看下~!
- CMP中JNDI的问题。
- 比较一下J2EE/J2ME/J2ME/JDK/JSDK/都是什么呀????
- java注解Annotaion继承的问题
- [SOAPException: faultCode=SOAP-ENV:Protocol; msg=Unsupported response content type "text/html;
不知道你是怎么获得Session的
不用Spring的情况下我一般是这样拿的:
Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();或
Session session = factory.getCurrentSession();
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION; static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private MySessionFactory() {
}
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);
sessionFactory = configuration.buildSessionFactory();
} 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) {
MySessionFactory.configFile = configFile;
sessionFactory = null;
} public static Configuration getConfiguration() {
return configuration;
}}
<?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">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=yt
</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<!-- 配置C3P0连接池属性 -->
<property name="hibernate.connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.timeout">50000</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">false</property>
-
- <!-- 注册ORM映射文件
-->
<mapping resource="com/ORM/Memberlevel.hbm.xml" />
<mapping resource="com/ORM/Merchandise.hbm.xml" />
<mapping resource="com/ORM/Admin.hbm.xml" />
<mapping resource="com/ORM/Orders.hbm.xml" />
<mapping resource="com/ORM/Cartselectedmer.hbm.xml" />
<mapping resource="com/ORM/Cart.hbm.xml" />
<mapping resource="com/ORM/Leaveword.hbm.xml" />
<mapping resource="com/ORM/Category.hbm.xml" />
<mapping resource="com/ORM/Member.hbm.xml" />
<mapping resource="com/ORM/QuotedPricer.hbm.xml" /> </session-factory>
</hibernate-configuration>
检查看看hibernate.cfg.xml是不是放对地方了
hibernate.cfg.xml直接放在src下就可以直接configuration.configure();
发现<!-- 注册ORM映射文件
--> 这句话前面的“-”了吗。
我删了后居然好了。谢谢大家了