Spring与hibernate的问题

05-08-18 lyxdl20020010
我遇到这样一个问题,比较郁闷,请朋友们帮忙看一下:

这是一个新闻维护例子

package com.news;

import java.io.Serializable;

import org.apache.commons.lang.builder.ToStringBuilder;

/** @author Hibernate CodeGenerator */

public class SchoolNews implements Serializable {

/** identifier field */

private Integer newsID;

/** nullable persistent field */

private String newsTitle;

/** nullable persistent field */

private String newsContent;

/** nullable persistent field */

private String newsDate;

/** full constructor */

public SchoolNews(String newsTitle, String newsContent, String newsDate) {

this.newsTitle = newsTitle;

this.newsContent = newsContent;

this.newsDate = newsDate;

}

/** default constructor */

public SchoolNews() {

}

public Integer getNewsID() {

return this.newsID;

}

public void setNewsID(Integer newsID) {

this.newsID = newsID;

}

public String getNewsTitle() {

return this.newsTitle;

}

public void setNewsTitle(String newsTitle) {

this.newsTitle = newsTitle;

}

public String getNewsContent() {

return this.newsContent;

}

public void setNewsContent(String newsContent) {

this.newsContent = newsContent;

}

public String getNewsDate() {

return this.newsDate;

}

public void setNewsDate(String newsDate) {

this.newsDate = newsDate;

}

public String toString() {

return new ToStringBuilder(this)

.append("newsID", getNewsID())

.toString();

}

}

package net.xiaxin.dao;

import com.news.SchoolNews;

/**

* @author Administrator

*

* TODO To change the template for this generated type comment go to

* Window - Preferences - Java - Code Style - Code Templates

*/

public interface IUserDao {

public void insertUser(SchoolNews news);

}

package net.xiaxin.dao;

import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import com.news.SchoolNews;

import java.io.InputStream;

import java.io.FileInputStream;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

/**

* @author Administrator

*

* TODO To change the template for this generated type comment go to

* Window - Preferences - Java - Code Style - Code Templates

*/

public class UserDao extends HibernateDaoSupport implements IUserDao{

public void insertUser(SchoolNews news){

getHibernateTemplate().save(news);

}

public static void main(String[] args){

try{

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("Hibernate-Context.xml"));

IUserDao userDao = (IUserDao)factory.getBean("userDAOProxy");

SchoolNews obj = new SchoolNews();

obj.setNewsTitle("title");

obj.setNewsContent("aaaaaaaaaaa");

obj.setNewsDate("2002-05-09");

userDao.insertUser(obj);

}catch(Exception e){

e.printStackTrace();

}

}

}

Hibernate-Context.xml内容如下:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName">

<value>oracle.jdbc.driver.OracleDriver</value>

</property>

<property name="url">

<value>jdbc:oracle:thin:@127.0.0.1:1521:school</value>

</property>

<property name="username">

<value>SCOTT</value>

</property>

<property name="password">

<value>liuyuan</value>

</property>

</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">

<property name="dataSource">

<ref local="dataSource" />

</property>

<property name="mappingResources">

<list>

<value>com/news/SchoolNews.hbm.xml</value>

</list>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.Oracle9Dialect

</prop>

<prop key="hibernate.show_sql">

true

</prop>

</props>

</property>

</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">

<property name="sessionFactory">

<ref local="sessionFactory" />

</property>

</bean>

<bean id="userDao" class="net.xiaxin.dao.UserDao">

<property name="sessionFactory">

<ref local="sessionFactory" />

</property>

</bean>

<bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

<property name="transactionManager">

<ref local="transactionManager" />

</property>

<property name="target">

<ref local="userDao" />

</property>

<property name="transactionAttributes">

<props>

<prop key="insert*">PROPAGATION_REQUIRED</prop>

<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>

</props>

</property>

</bean>

</beans>

运行上面的UserDao存储新闻,结果总是报“表或视图不存在”异常,单表的的确确存在,而且我写一个测试类直接调用Hibernate的方法存储新闻是成功的,高不明白为什么,请朋友们帮忙看看,谢谢。我的数据库是oracle9i,spring.jar是1.2.3版本的。

lyxdl20020010
2005-08-18 10:14
Hibernate-Context.xml内容如下:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName">

<value>oracle.jdbc.driver.OracleDriver</value>

</property>

<property name="url">

<value>jdbc:oracle:thin:@127.0.0.1:1521:school</value>

</property>

<property name="username">

<value>SCOTT</value>

</property>

<property name="password">

<value>liuyuan</value>

</property>

</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">

<property name="dataSource">

<ref local="dataSource" />

</property>

<property name="mappingResources">

<list>

<value>com/news/SchoolNews.hbm.xml</value>

</list>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.Oracle9Dialect

</prop>

<prop key="hibernate.show_sql">

true

</prop>

</props>

</property>

</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">

<property name="sessionFactory">

<ref local="sessionFactory" />

</property>

</bean>

<bean id="userDao" class="net.xiaxin.dao.UserDao">

<property name="sessionFactory">

<ref local="sessionFactory" />

</property>

</bean>

<bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

<property name="transactionManager">

<ref local="transactionManager" />

</property>

<property name="target">

<ref local="userDao" />

</property>

<property name="transactionAttributes">

<props>

<prop key="insert*">PROPAGATION_REQUIRED</prop>

<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>

</props>

</property>

</bean>

</beans>

lyxdl20020010
2005-08-18 10:16

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName">
			<value>oracle.jdbc.driver.OracleDriver</value>
		</property>
		<property name="url">
			<value>jdbc:oracle:thin:@127.0.0.1:1521:school</value>
		</property>
		<property name="username">
			<value>SCOTT</value>
		</property>
		<property name="password">
			<value>liuyuan</value>
		</property>
	</bean>
	<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref local="dataSource" />
		</property>
		<property name="mappingResources">
			<list>
				<value>com/news/SchoolNews.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.Oracle9Dialect
				</prop>
				<prop key="hibernate.show_sql">
					true
				</prop>
			</props>
		</property>
	</bean>
	<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>
	<bean id="userDao" class="net.xiaxin.dao.UserDao">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>
	<bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager">
			<ref local="transactionManager" />
		</property>
		<property name="target">
			<ref local="userDao" />
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="insert*">PROPAGATION_REQUIRED</prop>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
			</props>
		</property>
	</bean>
</beans>

猜你喜欢