http://alvinalexander.com/java/jwarehouse/hibernat

本文分享了2019年独角兽企业对于Python工程师的招聘要求,并附带了一个关于Hibernate框架中类型覆盖测试配置文件的链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd " > <context:property-placeholder location="classpath:application.properties"></context:property-placeholder> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="com.aiearth.drone.dao,com.aiearth.pojo" /> <property name="hibernateProperties"> <props> <!-- <prop key="hibernate.hbm2ddl.auto"> update </prop> --> <prop key="hibernate.dialect"> <!-- org.hibernate.spatial.dialect.postgis.PostgisDialect--> org.hibernate.spatial.dialect.postgis.PostgisPG10Dialect <!-- org.hibernate.dialect.PostgreSQLDialect--> </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.use_sql_comments">false</prop> <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> <prop key="hibernate.query.escape_like_parameter">false</prop> <prop key="hibernate.query.sql.escape-identifier">false</prop> <prop key="hibernate.logger.org.hibernate.mapping">DEBUG</prop> </props> </property> </bean> <bean id="dataSource" class="com.aiearth.drone.security.DataSource.UmspscDataSource"> <property name="driverClassName" value="org.postgresql.Driver"/> <property name="jdbcUrl" value="${drone.jdbc.url}?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&stringtype=unspecified"/> <property name="username" value="${drone.jdbc.username}"/> <property name="password" value="${drone.jdbc.password}"/> <property name="connectionTimeout" value="30000"/> <property name="idleTimeout" value="180000"/> <property name="maximumPoolSize" value="30"/> <property name="minimumIdle" value="10"/> <property name="maxLifetime" value="1200000"/> <property name="connectionTestQuery" value="SELECT 1"/> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" primary="true"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" propagation="REQUIRED" /> <tx:method name="count*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" /> </tx:attributes> </tx:advice> <aop:config expose-proxy="true"> <!-- 只对业务逻辑层实施事务 --> <aop:pointcut id="txPointcut" expression="execution(* com.aiearth..*.service..*Service.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" /> </aop:config> </beans>这样配对吗,如果不对请给我完整的修改后配置
07-31
### 配置分析与修正建议 #### 1. 数据源配置(`dataSource`) 当前配置使用了自定义的 `UmspscDataSource`,该类可能基于 HikariCP 或其他连接池实现。配置中设置了 JDBC URL、用户名、密码、连接池参数等,并输出了日志信息,有助于排查连接问题。然而,若未正确继承或实现标准的 `DataSource` 接口,可能导致 Spring 无法识别或管理连接池,从而影响事务行为。 **建议修正:** - 如果使用的是 HikariCP,应直接使用 `HikariDataSource` 并确保引入了相关依赖。 - 若 `UmspscDataSource` 是自定义实现,应确保其正确实现了 `DataSource` 接口,并能被 Spring 管理。 ```java @Bean(name = "dataSource") public DataSource dataSource() { HikariDataSource dataSource = new HikariDataSource(); dataSource.setJdbcUrl(env.getProperty("drone.jdbc.url") + "?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&stringtype=unspecified"); dataSource.setUsername(env.getProperty("drone.jdbc.username")); dataSource.setPassword(env.getProperty("drone.jdbc.password")); dataSource.setMaximumPoolSize(30); dataSource.setMinimumIdle(10); dataSource.setIdleTimeout(180000); dataSource.setMaxLifetime(1200000); dataSource.setConnectionTestQuery("SELECT 1"); return dataSource; } ``` #### 2. 实体管理工厂(`entityManagerFactory`) 当前配置使用了 `LocalContainerEntityManagerFactoryBean`,并设置了 JPA 供应商适配器为 `HibernateJpaVendorAdapter`。配置中指定了实体类扫描路径、数据源、JPA 属性等。但存在以下问题: - 使用了 `org.hibernate.dialect.HSQLDialect`,而实际数据库为 PostgreSQL,应使用 `org.hibernate.spatial.dialect.postgis.PostgisPG10Dialect`。 - `hibernate.query.sql.escape-identifier` 设置为 `false`,但未确保 Hibernate 版本支持此配置,可能导致未生效。 **建议修正:** - 确保使用正确的方言。 - 若使用 Hibernate 6.2 或以上版本,可尝试启用 `hibernate.query.escape_like_parameter`。 ```java Properties properties = new Properties(); properties.setProperty("hibernate.dialect", "org.hibernate.spatial.dialect.postgis.PostgisPG10Dialect"); properties.setProperty("hibernate.show_sql", "true"); properties.setProperty("hibernate.query.escape_like_parameter", "false"); em.setJpaProperties(properties); ``` #### 3. SessionFactory 配置 当前配置使用了 `LocalSessionFactoryBean`,并设置了数据源、扫描路径及 Hibernate 属性。但未启用 JPA 事务管理器,可能导致事务行为不一致。 **建议修正:** - 若使用 JPA,应优先使用 `JpaTransactionManager` 而非 `HibernateTransactionManager`。 - 若需同时使用 Hibernate 和 JPA,应配置多个事务管理器,并通过 `@Transactional` 注解指定使用哪一个。 ```java @Bean(name = "transactionManager") @Primary public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { return new JpaTransactionManager(entityManagerFactory); } ``` #### 4. 事务管理器(`transactionManager`) 当前配置使用了 `HibernateTransactionManager`,并绑定到 `sessionFactory`。但在 JPA 环境中,应使用 `JpaTransactionManager` 以确保事务一致性。 **建议修正:** - 若使用 JPA,应替换为 `JpaTransactionManager`。 - 若需兼容 Hibernate,可保留 `HibernateTransactionManager`,但需确保所有 DAO 使用 Hibernate API。 ```java @Bean(name = "transactionManager") @Primary public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { return new JpaTransactionManager(entityManagerFactory); } ``` #### 5. 事务拦截器(`txAdvice`)与 AOP 配置 当前配置使用了 `TransactionInterceptor` 和 `DefaultPointcutAdvisor` 来定义事务规则。但未明确指定事务管理器,可能导致事务未正确应用。 **建议修正:** - 确保 `txAdvice` 中使用的 `transactionManager` 与主事务管理器一致。 - 可简化为使用 `@EnableTransactionManagement` 注解自动配置。 ```java @Configuration @EnableTransactionManagement public class HibernatePostgisConfig { // 其他配置不变 } ``` --- ### 总结 当前配置中存在多个潜在问题,包括数据源实现、Hibernate 方言、事务管理器类型等。建议根据上述修正方案调整配置,以确保 Hibernate、数据源和事务管理正常工作。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值