gpt4 book ai didi

Java Spring + Hibernate LocalSessionFactoryBean 不工作

转载 作者:行者123 更新时间:2023-12-01 06:13:36 25 4
gpt4 key购买 nike

我正在尝试将 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)

最佳答案

您的 hibernate.hmb.xml 应该是 hibernate cfg 文件,但在您的情况下,它不是必需的,因为您已经创建了 Config.java 并且声明了 LocalSessionFactoryBean。在 Config.java 的 sessionFactory 方法中,您传递 asfb.setMappingResources("hibernate.cfg.xml");.. setMappingResources 方法需要 hbm 文件,例如:mapping.hbm.xml。

关于Java Spring + Hibernate LocalSessionFactoryBean 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29625577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com