gpt4 book ai didi

java - Spring LocalSessionFactoryBean 无法与 Annontaions 和 HBM 映射一起使用

转载 作者:太空宇宙 更新时间:2023-11-04 09:15:40 25 4
gpt4 key购买 nike

我正在开发一个遗留的 Hibernate 项目。所有实体都有相应的hbm映射文件。

sessionFactory 的创建方式如下:

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations" ref="hibernateMappingLocations"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
</props>
</property>
<property name="packagesToScan" ref="packagesToScanForBaseSessionFactory" />
<property name="entityInterceptor" ref="entityInterceptor" />
</bean>

现在我需要查询一个表,该表的实体(Student.java)配置了注释而不是 hbm 映射文件。

我尝试在我的 Dao 类中以这种方式查询 Student.java:

org.hibernate.query.Query<?> query = getSession().createQuery("FROM Student");
List<?> studentList = query.list();

这不起作用。请注意,这种查询方式适用于具有 hbm 映射而不是注释的所有其他实体。

但是使用注解就不行了。

我尝试通过向 org.springframework.orm.hibernate5.LocalSessionFactoryBean 中的属性 annotatedClasses 添加值来更改 sessionFactory 的 bean 定义:

<property name="annotatedClasses">
<list>
<value>com.Student</value>
</list>
</property>

最后我的 sessionfactory bean deafinition 看起来像这样:

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations" ref="hibernateMappingLocations"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
</props>
</property>
<property name="packagesToScan" ref="packagesToScanForBaseSessionFactory" />
<property name="entityInterceptor" ref="entityInterceptor" />
<property name="annotatedClasses">
<list>
<value>com.Student</value>
</list>
</property>
</bean>

但即便如此,它还是给了我以下异常(exception):

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: com.Student is not mapped
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:171)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:79)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:326)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3706)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3595)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:576)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:266)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189)
... 61 more

请帮忙。

最佳答案

不要使用 XML 来执行此操作,而是尝试以编程方式执行此操作。我在获取正确的 xml 时遇到了问题,但总是取得成功(显然必须根据您的需求进行更改)重要的部分是扫描值的包。这应该指向其中包含映射实体的包:

@Configuration(value="com.database.DatabaseBeanConfiguration")
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = {"com.database.repos"},
entityManagerFactoryRef = "databaseEntityManagerFactory",
transactionManagerRef = "databaseTransactionManager"
)
public class DatabaseBeanConfiguration {
public static final String PERSISTENCE_UNIT = "database_pu";

private static final String[] PACKAGES_TO_SCAN = {"com.database.entities"};

private static final Properties hibernateProperties;
static {
hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.connection.zeroDateTimeBehavior", "convertToNull");
hibernateProperties.setProperty("hibernate.dbcp.maxActive", "50");
hibernateProperties.setProperty("hibernate.dbcp.maxIdle", "10");
hibernateProperties.setProperty("hibernate.dbcp.maxWait", "5000");
hibernateProperties.setProperty("hibernate.jdbc.batch_size property", "50");
hibernateProperties.setProperty("hibernate.connection.charSet", "UTF-8");
hibernateProperties.setProperty("hibernate.connection.characterEncoding", "UTF-8");
hibernateProperties.setProperty("hibernate.connection.useUnicode", "true");
hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", "false");
hibernateProperties.setProperty("hibernate.cache.use_query_cach", "true");
hibernateProperties.setProperty("hibernate.cache.region.factory_class", "true");
hibernateProperties.setProperty("hibernate.connection.useUnicode", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
hibernateProperties.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
}

@Autowired
private DataSource dataSource;

/**
* Create the entity manager factory bean used in the database connection
* @return entityManagerFactoryBean
*/
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean databaseEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(dataSource);
lef.setJpaVendorAdapter(databaseJpaVendorAdapter());
lef.setPackagesToScan(PACKAGES_TO_SCAN);
lef.setJpaProperties(hibernateProperties);
lef.setPersistenceUnitName(PERSISTENCE_UNIT);
return lef;
}

/**
* Create the session factory bean
* @return sessionFactoryBean
*/
@Bean
public LocalSessionFactoryBean databaseSessionFactory() {
LocalSessionFactoryBean sessionBean = new LocalSessionFactoryBean();
sessionBean.setDataSource(dataSource);
sessionBean.setPackagesToScan(PACKAGES_TO_SCAN);
sessionBean.setHibernateProperties(hibernateProperties);
return sessionBean;
}

/**
* Create the JPA Vendor adaptor bean that is used in the entity manager factory
* @return jpaVendorAdaptor
*/
@Primary
@Bean
public JpaVendorAdapter databaseJpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql(false);
hibernateJpaVendorAdapter.setGenerateDdl(false);
hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
return hibernateJpaVendorAdapter;
}

/**
* Create the transaction manager bean
* @return transactionManager
*/
@Primary
@Bean
public PlatformTransactionManager databaseTransactionManager() {
JpaTransactionManager manager = new JpaTransactionManager();
manager.setEntityManagerFactory(databaseEntityManagerFactory().getObject());
return new JpaTransactionManager();
}
}

关于java - Spring LocalSessionFactoryBean 无法与 Annontaions 和 HBM 映射一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59050573/

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