81,092
社区成员
发帖
与我相关
我的任务
分享
public class UserDaoImpl extends BaseHibernateDAO implements IUserDao {
@Override
public boolean check(User user) {
boolean flag = false;
String queryString = "from User";
Query queryObject = getSession().createQuery(queryString);
List<User> users = queryObject.list();
if(users.size() >0){
flag=true;
}
return flag;
}
}
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="erp.domain.User" table="User" schema="dbo" catalog="ERP2017_Basic">
<id name="userId" type="java.lang.Integer">
<column name="userId" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<property name="password" type="java.lang.String">
<column name="password" />
</property>
<property name="logInUserTime" type="java.lang.Integer">
<column name="logInUserTime" not-null="true" />
</property>
</class>
</hibernate-mapping>
HTTP Status 500 - 接近關鍵字 'User' 之處的語法不正確。
type Exception report
message 接近關鍵字 'User' 之處的語法不正確。
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.hibernate.exception.SQLGrammarException: 接近關鍵字 'User' 之處的語法不正確。
org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.url">
jdbc:sqlserver://192.168.100.112:1433;databaseName=ERP2017_Basic
</property>
<property name="connection.username">sa</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<mapping resource="erp/domain/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
public class BaseHibernateDAO implements IBaseHibernateDAO {
public Session getSession() {
return HibernateSessionFactory.getSession();
}
}
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static org.hibernate.SessionFactory sessionFactory;
private static Configuration configuration = new Configuration();
private static ServiceRegistry serviceRegistry;
static {
try {
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
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;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
<hibernate-mapping>
<class name="erp.domain.User" table="User">
<id name="userId" type="java.lang.Integer">
<column name="userId" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<property name="password" type="java.lang.String">
<column name="password" />
</property>
<property name="logInUserTime" type="java.lang.Integer">
<column name="logInUserTime" not-null="true" />
</property>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="erp.domain.User" table="User" schema="dbo" catalog="ERP2017_Basic">
<id name="userId" type="java.lang.Integer">
<column name="userId" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<property name="password" type="java.lang.String">
<column name="password" />
</property>
<property name="logInUserTime" type="java.lang.Integer">
<column name="logInUserTime" not-null="true" />
</property>
</class>
</hibernate-mapping>