0

I am working on a Spring application which has a persistence unit configured in the application-context.xml. I need to add an additional package in in order to use new entities. Even though this part of the persistence.xml file looks like below, my entities from the additional package are not seen by the application and I get an exception saying that the entity is unknown.

<bean id="transactionManager_students" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoryStudents" />
        <qualifier value="clientTransaction" />
    </bean>

    <bean id="entityManagerFactoryStudents"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="datasource_College" />
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
        <property name="packagesToScan">
            <list>
                <value>com.load.model</value>
                <value>com.students.entity</value>
            </list>
        </property>
        <property name="persistenceUnitName" value="unit_stud" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.cache.use_query_cache">false</prop>
                <prop key="hibernate.cache.use_second_level_cache">false</prop>
                <prop key="hibernate.hbm2ddl.auto">none</prop>
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>  

I also have to mention that I annotated the entities with @Entity and in the class where I am operating on the entities I have this ( the row with em.persist(student) is giving me the error )


@PersistenceContext(unitName = "unit_stud")
public EntityManager em;

public Student student;

@Transactional(value = "clientTransaction", propagation = Propagation.REQUIRED)
public long persistStudentObject() {

            long studentId = 0;

            try
            {
                logger.debug("Start Persisting...");
                em.persist(student);
                // unique ID
                studentId = student.getId();
                logger.debug("Persisting OK...");
            }
            catch (PersistenceException persistenceException)
            {
                logger.error("PersistenceException occur", persistenceException);
            }

            }
            return studentId ;
    }

The entity:

package com.students.entity;

@Entity
@Table(name = "STUDENTS", schema = "DEMO", catalog = "")
public class Student{
    private long id;
    private String firstname;
    private String name;
    private String streetnumber;
    private String zipcodecity;

Can anyone help me? I do not know what to do in order to make my entities visible.

Maria1995
  • 439
  • 1
  • 5
  • 21
  • That isn't a JPA `persistence.xml` but just a plain Spring XML application context (which might be named `persistence.xml`). Make sure the package name is correct and that it is readable. – M. Deinum Oct 29 '19 at 15:12
  • indeed, it is application-context. how can I make sure the package name is readable? – Maria1995 Oct 29 '19 at 15:32
  • I don't see anything wrong in the config. Please add an example of an entity which cannot be loaded to the question. You don't need to post the entire thing, the package statement, the class and it's annotations should be enough. – Gimby Oct 29 '19 at 16:00
  • Done, I added more code – Maria1995 Oct 29 '19 at 16:14
  • I have to mention that I also have other functions before this one that change the students object, so when I debug I see it populated with all the values. The em.persist() step is the big issue – Maria1995 Oct 29 '19 at 16:15
  • What is the problem with the `em.persist` call? Can you post the code that isn't working? Also you are persisting an instance level variable, which is a big no-go. You are keeping state and that will simply not work in the long run (especially in concurrent scenarios). – M. Deinum Oct 29 '19 at 18:04
  • The problem is that I get IllegalStateException, unknown entity because of the line em.persist(student). This is the piece of code that does not work...the entity gets populated as expected, but I cannot persist it...that is my problem. When I debug and I look at the entity manager, at the persistence unit, the classes that are visible to it are the ones from the first package I have in the "packagesToScan". The second one is not visible and I do not know why ( name of it is right, not issues of this kind) – Maria1995 Oct 29 '19 at 19:35
  • Are these "additional package" classes in a separate jar by any chance? If they are not... it has to be something silly. Like: the context file you are editing is actually not loaded in the environment you run it in. – Gimby Oct 31 '19 at 13:05

0 Answers0