2

I have a project which uses another project as a lib and I have Spring and Hibernate on both projects, the problem is that I need to map the classes from my first project to my second project.

This is the exception I got:

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)

So I tried to map the classes in the persistence.xml, this way:

<?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>

But there are so many classes to map, they are all chained... Is there a better way to do it?

@EDIT

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>
Gabriel Câmara
  • 1,249
  • 15
  • 22
  • Maybe [this](http://stackoverflow.com/a/1999380/778118) will help. My memory says you don't have to specify each entity if you want to use all the entities from a given location. Maybe [this](http://stackoverflow.com/a/1780362/778118) or [this](http://stackoverflow.com/q/1999307/778118) might help, too. – jahroy Feb 26 '13 at 20:40
  • this one may also help : http://stackoverflow.com/questions/1780341/do-i-need-class-elements-in-persistence-xml – benzonico Feb 26 '13 at 20:44
  • Thanks for the answer! But if I use the tag MyProject.jar it'll only works if I have my project as a jar file, but what if I don't have it yet, I mean in case of testing? – Gabriel Câmara Feb 26 '13 at 22:58
  • An answer of mine in the past, I think it gives you several choices: http://stackoverflow.com/questions/6380728/jpa-2-0-adding-entity-classes-to-persistenceunit-from-different-jar-automatic/13756652#13756652 – Adrian Shum Feb 27 '13 at 01:08
  • Thanks, Adrian, I tried the property "packagesToScan" and Hibernate autodetection... Still not working... – Gabriel Câmara Feb 27 '13 at 12:05

1 Answers1

1

In your Spring configuration setup a component scan to look for the entities, note that you must include the context namespace:

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

With this setup you should be able to remove the classes from 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>

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • @GabrielCâmara try adding the provider in your persistence.xml, also if you would post your spring configuration I could better help you. – Kevin Bowersox Feb 28 '13 at 10:25
  • Ok, I'm going to edit my post and add my applicationContext.xml. To continue the project, I mapped classes one by one, in the end were not so many classes, but it is good to avoid this in the future. – Gabriel Câmara Feb 28 '13 at 14:55