我正在尝试将休眠与Spring集成在一起。这是我的代码:
Main.java
import java.util.List;
import local.bb.entities.Words;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String args[]) {
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("spring.xml");
SessionFactory sf = (SessionFactory)appContext.getBean("sessionFactory");
Session s = sf.openSession();
try {
List<Words> words = s.createCriteria(Words.class).list();
int i=1;
for( Words w : words ) {
System.out.println( "["+ (i++) +"]" + w.getWord());
}
} catch(Exception e) {
e.printStackTrace();
}
sf.close();
}
}
spring.xml
<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="local.bb.config">
</context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
Config.java(local.bb.config包)
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
@Configuration
public class Config {
@Bean
DriverManagerDataSource dataSource() {
DriverManagerDataSource dmds = new DriverManagerDataSource("jdbc:mysql://localhost:3306/java?zeroDateTimeBehavior=convertToNull");
dmds.setUsername("root");
return dmds;
}
@Bean
LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean asfb = new LocalSessionFactoryBean();
asfb.setDataSource(dataSource());
asfb.setMappingResources("hibernate.cfg.xml");
Properties props = new Properties();
props.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
asfb.setHibernateProperties(props);
return asfb;
}
}
最后是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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/java?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<mapping class="local.bb.entities.Users" />
<mapping class="local.bb.entities.Words" />
</session-factory>
</hibernate-configuration>
没有错误,但是它不会从数据库中读取数据。如果我替换SessionFactory sf = (SessionFactory)appContext.getBean("sessionFactory");
Main.java,SessionFactory sf = new Configuration().configure().buildSessionFactory();
它可以正常工作。所以问题可能出在Config.java
哪里,也许我的方法根本不是那么好吗?无论如何,数据库连接正在工作,并且其中包含一些记录。最后,我粘贴了控制台结果:
run:
kwi 14, 2015 12:47:58 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1bce4f0a: startup date [Tue Apr 14 12:47:58 CEST 2015]; root of context hierarchy
kwi 14, 2015 12:47:58 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
kwi 14, 2015 12:47:59 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
kwi 14, 2015 12:47:59 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
kwi 14, 2015 12:47:59 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
kwi 14, 2015 12:47:59 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
kwi 14, 2015 12:47:59 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
kwi 14, 2015 12:47:59 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
kwi 14, 2015 12:48:00 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.HSQLDialect
kwi 14, 2015 12:48:00 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
kwi 14, 2015 12:48:00 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
kwi 14, 2015 12:48:00 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.0.0.Final
BUILD SUCCESSFUL (total time: 3 seconds)
您的hibernate.hmb.xml应该是hibernate cfg文件,但是在您的情况下,由于创建了Config.java并声明了LocalSessionFactoryBean,因此它不是必需的。在Config.java的sessionFactory方法中,您要传递asfb.setMappingResources(“ hibernate.cfg.xml”); .. setMappingResources方法期望使用hbm文件,例如:mapping.hbm.xml。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句