hibernate配置oracle后无法查询

J2EE 码拜 10年前 (2015-04-13) 1120次浏览 0个评论

报错:Caused by: org.hibernate.exception.SQLGrammarException: could not execute query。

环境条件:1、用MyEclipse10,配置的spring+hibernate环境,连接oracle10g数据库, db broswer里面数据库链接正常。
2、javabean、访问的dao、hbm文件都是用的MyEclipse反向工程生成的,没有一行代码手动编写,因此不可能出现配置文件不对的情况。请库表只有两个字段。name1、pwd1.

我反复创建工程试了很多次了,都没有成功。求教大神这是什么原因导致的?卡了几天了

附上bean、dao及映射文件。

package com.wxt.policeOA.baseDbManager.pojo;

/**
 * Testtable entity. @author MyEclipse Persistence Tools
 */

public class Testtable implements java.io.Serializable {

	// Fields

	private String name1;
	private String pwd1;

	// Constructors

	/** default constructor */
	public Testtable() {
	}

	/** minimal constructor */
	public Testtable(String name1) {
		this.name1 = name1;
	}

	/** full constructor */
	public Testtable(String name1, String pwd1) {
		this.name1 = name1;
		this.pwd1 = pwd1;
	}

	// Property accessors

	public String getName1() {
		return this.name1;
	}

	public void setName1(String name1) {
		this.name1 = name1;
	}

	public String getPwd1() {
		return this.pwd1;
	}

	public void setPwd1(String pwd1) {
		this.pwd1 = pwd1;
	}

}
<?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.wxt.policeOA.baseDbManager.pojo.Testtable" table="TESTTABLE" schema="POLICEDB">
        <id name="name1" type="java.lang.String">
            <column name="name1" length="32" />
            <generator class="assigned" />
        </id>
        <property name="pwd1" type="java.lang.String">
            <column name="pwd1" length="32" />
        </property>
    </class>
</hibernate-mapping>
package com.wxt.policeOA.baseDbManager.pojo;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * Testtable entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.wxt.policeOA.baseDbManager.pojo.Testtable
 * @author MyEclipse Persistence Tools
 */

public class TesttableDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(TesttableDAO.class);

	protected void initDao() {
		// do nothing
	}

	public void save(Testtable transientInstance) {
		log.debug("saving Testtable instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Testtable persistentInstance) {
		log.debug("deleting Testtable instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Testtable findById(java.lang.String id) {
		log.debug("getting Testtable instance with id: " + id);
		try {
			Testtable instance = (Testtable) getHibernateTemplate().get(
					"com.wxt.policeOA.baseDbManager.pojo.Testtable", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Testtable instance) {
		log.debug("finding Testtable instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Testtable instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Testtable as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Testtable instances");
		try {
			String queryString = "from Testtable";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Testtable merge(Testtable detachedInstance) {
		log.debug("merging Testtable instance");
		try {
			Testtable result = (Testtable) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Testtable instance) {
		log.debug("attaching dirty Testtable instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Testtable instance) {
		log.debug("attaching clean Testtable instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static TesttableDAO getFromApplicationContext(ApplicationContext ctx) {
		return (TesttableDAO) ctx.getBean("TesttableDAO");
	}
}
hibernate配置oracle后无法查询
详细的出错信息如下所示:

九月
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ""DataInitManager"" defined in ServletContext resource [/config/initManager.xml]: Invocation of init method failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select testtable0_.name1 as name1_0_, testtable0_.pwd1 as pwd2_0_ from POLICEDB.TESTTABLE testtable0_]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)

	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
	at javax.servlet.GenericServlet.init(GenericServlet.java:160)
	)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1496)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1506)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1485)
	at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select testtable0_.name1 as name1_0_, testtable0_.pwd1 as pwd2_0_ from POLICEDB.TESTTABLE testtable0_]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
	at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:629)
	at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
	at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
	at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)

	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1544)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1485)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
	... 31 more
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
	at org.hibernate.loader.Loader.doList(Loader.java:2235)
	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
	at org.hibernate.loader.Loader.list(Loader.java:2124)
	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
	at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
	at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
	at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
	at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
	at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:921)
	at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:1)
	at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
	... 43 more
Caused by: java.sql.SQLException: ORA-00904: "TESTTABLE0_"."PWD1": 标识符无效

	at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
	at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
	at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
	at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)
	at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:213)
	at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:796)
	at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1031)
	at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:836)
	at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1124)
	at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3285)
	at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3329)
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
	at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
	at org.hibernate.loader.Loader.getResultSet(Loader.java:1812)
	at org.hibernate.loader.Loader.doQuery(Loader.java:697)
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
	at org.hibe
rnate.loader.Loader.doList(Loader.java:2232)
	... 53 more

九月 04, 2014 10:18:44 下午 org.apache.catalina.core.StandardContext loadOnStartup
严重: Servlet /testOracle threw load() exception
java.sql.SQLException: ORA-00904: "TESTTABLE0_"."PWD1": 标识符无效

	at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
	at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
	at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
	at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)
	at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:213)
	at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:796)
	at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1031)
	at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:836)
	at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1124)
	at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3285)
	at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3329)
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
	at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
	at org.hibernate.loader.Loader.getResultSet(Loader.java:1812)
	at org.hibernate.loader.Loader.doQuery(Loader.java:697)
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
	at org.hibernate.loader.Loader.doList(Loader.java:2232)
	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
	at org.hibernate.loader.Loader.list(Loader.java:2124)
	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
	at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
	at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
	at o
	at com.wxt.policeOA.baseDbManager.pojo.TesttableDAO.findAll(TesttableDAO.java:94)
	at com.wxt.policeOA.baseDbManager.Interface.impl.DataInitManager.initFunction(DataInitManager.java:57)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1544)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1485)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)

在线等,求大神帮忙

hibernate配置oracle后无法查询
100分
 ORA-00904: “TESTTABLE0_”.”PWD1″: 标识符无效 
数据库表中有pwd1 这个字段吗?
hibernate配置oracle后无法查询
引用 2 楼 magi1201 的回复:

 ORA-00904: “TESTTABLE0_”.”PWD1″: 标识符无效 
数据库表中有pwd1 这个字段吗?

  有这个字段的。我后来发现原因 了,是xml文件没写对,复合主键设置错了。谢谢大神。

hibernate配置oracle后无法查询
引用 3 楼 LOVE_GG 的回复:
Quote: 引用 2 楼 magi1201 的回复:

 ORA-00904: “TESTTABLE0_”.”PWD1″: 标识符无效 
数据库表中有pwd1 这个字段吗?

  有这个字段的。我后来发现原因 了,是xml文件没写对,复合主键设置错了。谢谢大神。

  说错了,是hibernate配置文件错了,应该加上<prop key=”hibernate.hbm2ddl.auto”>update</prop>


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明hibernate配置oracle后无法查询
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!