gpt4 book ai didi

spring - hibernate 4 sessionfactory packagestoscan 属性使用

转载 作者:行者123 更新时间:2023-12-04 08:56:36 32 4
gpt4 key购买 nike

我们有一个基于 Spring 3.2.4 和 Hibernate 4.2.3 的 Web 应用程序。它是使用 appfuse 2.2.1 原型(prototype)生成的。根据可用的文档,我们可以使用 SessionFactory 的 packagesToScan 属性来动态选择实体类并设置 sessionFactory。但是,我们面临一个问题,我们必须在 hibernate.cfg.xml 中明确提及实体类名称才能被 hibernate 识别。
我在论坛上找不到任何合适的答案,所以在这里发帖。可能是我对其工作原理的理解可能不正确,因此欢迎任何指针。

谢谢

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans -- namespace declarations here
default-lazy-init="true">
<!-- Activates scanning of @Autowired -->
<context:annotation-config/>
<!-- Activates scanning of @Repository and @Service -->
<context:component-scan base-package="com.xyz"/>
</beans>

hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="com.xyz.model.Address" />
<mapping class="com.xyz.model.Company" />
<mapping class="com.xyz.model.CompanyBilling" />
<!-- BIG LIST OF MY ENTITY CLASSES -->
</session-factory>
</hibernate-configuration>

applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans -- namespace declarations here
default-lazy-init="true">
<bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" destroy-method="destroy">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
<property name="packagesToScan"><list><value>com.xyz.model</value></list></property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Activates scanning of @Autowired -->
<context:annotation-config/>
<!-- Activates scanning of @Repository -->
<context:component-scan base-package="com.xyz.dao"/>
</beans>

公司.java
 package com.xyz.model;
// imports
@Entity
@Table(name = "company")
@SQLDelete(sql = "UPDATE company SET activeFlag = 0, lastupdated=now() WHERE id = ?")
@Where(clause = "1=activeflag")
@Component
public class Company extends BaseEntity implements java.io.Serializable {
// properties and getter setters
}

最佳答案

第二个属性 packagesToScan 指定 Java 包自动扫描带注释的实体类。这样就不再需要准备 Hibernate 映射文件。

关于spring - hibernate 4 sessionfactory packagestoscan 属性使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20650502/

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