我正在尝试对数据库进行查询,我得到了这个:
java.lang.IllegalArgumentException异常: org.hibernate.hql.internal.ast.QuerySyntaxException:generalsetup is 未映射[FROM generalsetup] org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133) org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157) org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164) org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:670) org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:103) org.apache.jsp.Environments_jsp._jspService(Environments_jsp.java:188) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
重要提示:我可以写入数据库,但无法阅读。
这是我的代码,试图从中获取一个列表(generalsetup
下有一个错误标记:
SessionFactory factory = HibernateUtil.GetSessionFactory();
Session hibernateSession = factory.openSession();
Transaction tx = null;
try {
tx = hibernateSession.beginTransaction();
List environments = hibernateSession.createQuery("FROM generalsetup").list();
for (Iterator iterator = environments.iterator(); iterator.hasNext();) {
Environment environment = (Environment) iterator.next();
System.out.println(environment.getName());
}
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
hibernateSession.close();
}
我的GeneralSetup.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.atp.servlets.Model.GeneralSetup" table="generalsetup">
<meta attribute="class-description">
This class contains the general_setup detail.
</meta>
<id name="id" type="int" column="idGeneralSetup">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="hasProfile7" column="hasProfile7" type="boolean"/>
<property name="hasWebAdmin" column="hasWebAdmin" type="boolean"/>
<property name="hasWebClient" column="hasWebClient" type="boolean"/>
<property name="hasWebCSR" column="hasWebCSR" type="boolean"/>
<property name="profile7Branch" column="profile7Branch" type="string"/>
<property name="webAdminBranch" column="webAdminBranch" type="string"/>
<property name="webClientBranch" column="webClientBranch" type="string"/>
<property name="webCSRBranch" column="webCSRBranch" type="string"/>
<property name="initialDayEnd" column="initialSystemDate" type="string"/>
<property name="finalDayEnd" column="finalSystemDate" type="string"/>
<property name="amountOfDayEnds" column="amountOfDayEnds" type="int"/>
<property name="user" column="user" type="string"/>
</class>
</hibernate-mapping>
我的配置:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost/mydb
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
qetwr123
</property>
<!-- List of XML mapping files -->
<mapping resource="com/atp/servlets/Model/GeneralSetup.hbm.xml"/>
</session-factory>
</hibernate-configuration>