gpt4 book ai didi

java - PersistenceUnit 中的多个类

转载 作者:行者123 更新时间:2023-11-30 11:35:30 26 4
gpt4 key购买 nike

我有一个项目使用另一个项目作为库,我在两个项目上都有 Spring 和 Hibernate,问题是我需要将类从我的第一个项目映射到我的第二个项目。

这是我得到的异常:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Usuario is not mapped [SELECT u FROM Usuario u WHERE usuario = 'abc' order by idUsuario desc]
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)

所以我尝试以这种方式映射 persistence.xml 中的类:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="default">

<class>com.planner.pov.model.Usuario</class>
<class>com.planner.pov.model.LogWork</class>
<class>com.planner.pov.model.Responsavel</class>
<class>com.planner.pov.model.GrupoResponsavel</class>
<class>com.planner.pov.model.Cargo</class>
<class>com.planner.pov.model.TarefaBackLog</class>
<class>com.planner.pov.model.ItemBackLog</class>
</persistence-unit>

但是要映射的类太多了,都是链式的...有更好的方法吗?

@编辑

applicationContext.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: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-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:component-scan base-package="com.planner.pov" />
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="ConfiguradorDePropriedades"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<description>The service properties file</description>
<property name="location" value="file:AppConfig.properties" />
</bean>

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSourceLocal" />
<property name="packagesToScan" value="com.planner.pov" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="${database}" />
<property name="showSql" value="false" />
<property name="generateDdl" value="true" />
</bean>
</property>
</bean>
<bean id="dataSourceLocal"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pass}" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<!-- Configuração do LogWorkController -->
<bean id="logWorkDao" class="br.javablocker.dao.LogWorkDaoImpl" />

<!-- Configuração do ClockMainController -->
<bean id="clockMainController" class="br.javablocker.controller.PlannerClockMainController">
<property name="usuarioController" ref="usuarioController" />
<property name="logWorkDao" ref="logWorkDao" />
</bean>

<!-- Configuração do UsuarioController -->
<bean id="usuarioDao" class="br.javablocker.dao.UsuarioDaoImpl" />
<bean id="responsavelDao" class="com.planner.pov.dao.ResponsavelDaoImpl" />

<bean id="usuarioController" class="br.javablocker.controller.UsuarioController">
<property name="usuarioDao" ref="usuarioDao" />
<property name="responsavelDao" ref="responsavelDao" />
<property name="logWorkDao" ref="logWorkDao" />
</bean>

<bean id="logWorkController" class="br.javablocker.controller.LogWorkController">
<property name="logWorkDao" ref="logWorkDao" />
</bean>

</beans>

最佳答案

在您的 Spring 配置中设置组件扫描以查找实体,请注意您必须包含上下文命名空间:

<context:component-scan base-package="com.planner.pov.model"/>

使用此设置,您应该能够从 persistence.xml 中删除类:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="default">
<provider>org.hibernate.ejb.HibernatePersistence</provider>

</persistence-unit>

关于java - PersistenceUnit 中的多个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15098780/

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