gpt4 book ai didi

Javax 持久性错误 : Unknown Entity com. samplewebentities.Customer

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:13:33 27 4
gpt4 key购买 nike

我正在尝试使用我的 bean 中定义的 EntityManagerFactory 来持久化一个实体。该函数如下所示:

  private BaseMasterEntity saveEntity(BaseMasterEntity entity){
EntityManagerFactory emf = (EntityManagerFactory)context.getBean("entityManagerFactory");
EntityManager sf = emf.createEntityManager();
sf.getTransaction().begin();
sf.persist(entity);
sf.getTransaction().commit();
sf.close();
return entity;

}

这里的问题是当它仍然存在时找不到实体。该实体使用 javax.persistance 注释明确定义了 @Entity。顺便说一句,这是我的上下文文件的样子:

    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="*my jdbc setting*" />
<property name="username" value="hr" />
<property name="password" value="hr" />

</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<property name="packagesToScan"
value="classpath*:com.samplewebentities"></property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>

</property>
</bean>

<bean id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
<tx:annotation-driven transaction-manager="transactionManager" />

如果有帮助,classpath*:com.samplewebentites 是一个不同的组件(该应用程序是许多不同组件/项目的组合:在此处使用 SCA)。

最佳答案

不需要类路径

..
<property name="packagesToScan"
value="com.samplewebentities"></property>
..

关于Javax 持久性错误 : Unknown Entity com. samplewebentities.Customer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40146693/

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