org.hibernate.MappingException: Unknown entity:

后端 未结 3 1732
说谎
说谎 2021-01-01 23:26

I really want to understand what is going on with my code.

I have a standalone application which uses spring and Hibernate as JPA and I am trying to run the test usi

相关标签:
3条回答
  • 2021-01-01 23:56

    You have to list your classes in your session factory configuration. You can have your entities auto-discovered if you are using EntityManager.

    In order to use annotations with hibernate and spring, you have to use AnnotationSessionFactoryBean:

     <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="annotatedClasses">
            <list>
                <value>edu.acct.tsegay.model.User</value>
            </list>
        </property>
        ....
     </bean>
    

    Also, it is rather strange that your User entity is a spring bean. You don't need that. Hibernate entities are supposed to be created with the new operator.

    0 讨论(0)
  • 2021-01-02 00:06

    In addition to Bozho answer, if you are using spring + hibernate with annotation then in your session factory bean you can register your bean like below:

    LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
    localSessionFactoryBean.setDataSource(appContext.getBean(HikariDataSource.class));
    localSessionFactoryBean.setAnnotatedClasses(
            AppUser.class, Assignment.class
    );
    
    0 讨论(0)
  • 2021-01-02 00:07

    I've encountered the same problem and didn't find any good answer for this

    What worked for me was to declare my entity class in the persistence.xml file:

    <persistence ...>
        <persistence-unit ...>
    
            <class>com.company.maenad.core.model.News</class>
            <class>com.company.maenad.core.model.AdExtraInfo</class>
    
        </persistence-unit>
    </persistence>
    
    0 讨论(0)
提交回复
热议问题