hibernate+spring时出错,请大家指教! 没有加载spring的配置文件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我的hibernate的配置文件hibernate.cfg.xml配置正确,单独测试通过,内容如下:<?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"><!-- Generated by MyEclipse Hibernate Tools. --><hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url"> jdbc:mysql://localhost/SAMPLEDB </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">111111</property> <property name="hibernate.connection.pool.size">20</property> <property name="hibernate.show_sql">true</property> <property name="jdbc.fetch_size">50</property> <property name="jdbc.batch_size">25</property> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="jdbc.use_scrollable_resultset">false</property> <!-- Mapping files --> <mapping resource="com/jandar/model/User.hbm.xml" /> </session-factory></hibernate-configuration>1请问大家spring的配置文件还应该怎样配置? <beans><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>org.gjt.mm.mysql.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost/example</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value></value> </property></bean><bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource"/> </property> <property name="mappingResources"> <list> <value>eqzhou/AAA.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property></bean>然后如果你的其他的类要用sessionFactory,则需要将sessionFactory注入其中。然后在应用的时候,初始化你的Spring 容器。你的程序好象没有这个步骤。如果是web应用,则可以在web.xml中进行配置,在web容器初始化的时候就加载spring容器。 zeq258(近朱者赤) ,我把你的代码加到我的appconext.xml,并修改我的BusinessService中的代码:package mypack;import java.util.Iterator;import java.util.List;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;import org.springframework.orm.hibernate.LocalSessionFactoryBean;import org.springframework.orm.hibernate.support.HibernateDaoSupport;BusinessService{ public BusinessService(){ Resource resource=new ClassPathResource("appcontext.xml"); BeanFactory factory= new XmlBeanFactory(resource); LocalSessionFactoryBean sessionFactory2=(LocalSessionFactoryBean)factory.getBean("sessionFactory"); } public static void main(String args[]) throws Exception { BusinessService t=new BusinessService(); t.getCustomers(); //sessionFactory2.close(); }}但 LocalSessionFactoryBean sessionFactory2=(LocalSessionFactoryBean)factory.getBean("sessionFactory");这句在运行时出现以下错误:java.lang.ClassCastException请指教! java.lang.ClassCastException造型错误,请仔细检查! 编写J2EE程序时的并发问题 添加删除替换一个字符 求jfreechart 折线和柱状组合图 String对象的问题 散分散分!!!!!散分!!!!关于spring的依赖注入,控制反转问题 使用eclipse+myecilpse只配置Hibernate数据关联的问题? 调试roller2.3版的blog问题 MyEclipse中如何配置jboss4? 我下载了ant1.6可是运行却提示出错. struts中使用datasource的问题 请教一个对servlet进行提交的问题。 在Action中如何取得pageContext
<?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"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/SAMPLEDB
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">111111</property>
<property name="hibernate.connection.pool.size">20</property>
<property name="hibernate.show_sql">true</property>
<property name="jdbc.fetch_size">50</property>
<property name="jdbc.batch_size">25</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="jdbc.use_scrollable_resultset">false</property> <!-- Mapping files -->
<mapping resource="com/jandar/model/User.hbm.xml" /> </session-factory></hibernate-configuration>
1请问大家spring的配置文件还应该怎样配置?
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/example</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>eqzhou/AAA.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
然后如果你的其他的类要用sessionFactory,则需要将sessionFactory注入其中。
然后在应用的时候,初始化你的Spring 容器。你的程序好象没有这个步骤。如果是web应用,则可以在web.xml中进行配置,在web容器初始化的时候就加载spring容器。
package mypack;import java.util.Iterator;
import java.util.List;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.orm.hibernate.LocalSessionFactoryBean;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;BusinessService{
public BusinessService(){
Resource resource=new ClassPathResource("appcontext.xml");
BeanFactory factory= new XmlBeanFactory(resource);
LocalSessionFactoryBean sessionFactory2=(LocalSessionFactoryBean)factory.getBean("sessionFactory");
}
public static void main(String args[]) throws Exception {
BusinessService t=new BusinessService();
t.getCustomers();
//sessionFactory2.close();
}
}
但
LocalSessionFactoryBean sessionFactory2=(LocalSessionFactoryBean)factory.getBean("sessionFactory");
这句在运行时出现以下错误:
java.lang.ClassCastException
请指教!
造型错误,请仔细检查!