我正在尝试将hibernate与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)
答案 0 :(得分:-1)
你的hibernate.hmb.xml应该是hibernate cfg文件,但在你的情况下它不是必需的,因为你创建了Config.java并且你声明了LocalSessionFactoryBean。在Config.java的sessionFactory方法中,你传递的是asfb.setMappingResources(“hibernate.cfg.xml”); .. setMappingResources方法是期待hbm文件ex:mapping.hbm.xml。