RooEntity 未映射

发布于 2024-12-19 03:29:15 字数 2692 浏览 5 评论 0原文

我正在使用 SpringRoo 来处理实体对象的休眠。

但是,在运行单元测试时,它们会失败,因为它表示实体未映射:

这是错误:

QuerySyntaxException:优惠券未映射 [SELECT o FROM COUPON o] 在 org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)

这就是我的持久性单元的配置方式:

<persistence-unit name="persistenceUnitTest" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
        <property name="hibernate.connection.charSet" value="UTF-8"/>
    </properties>
</persistence-unit>

这就是我的实体的定义方式:

@RooJavaBean
@RooToString
@RooEntity(identifierColumn = "COUPONID", identifierType = Integer.class, table = "COUPON")
public class Coupon {

设置了应用程序上下文:

<context:component-scan base-package="com.tamiflu.entities">
    <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
</context:component-scan>

<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnitTest"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:mem:tamiflu"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="true"/>
    <property name="testWhileIdle" value="true"/>
    <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
    <property name="numTestsPerEvictionRun" value="3"/>
    <property name="minEvictableIdleTimeMillis" value="1800000"/>
</bean>

我觉得我有一切就位。我不知道为什么 @RooEntity 没有被拾取为正在映射。

I'm using SpringRoo to handle the Hibernation of my entity objects.

But when running unit tests, they are failing because it says the entity is not mapped:

Here is the error:

QuerySyntaxException: COUPON is not mapped [SELECT o FROM COUPON o]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)

This is what my persistence unit is configured like:

<persistence-unit name="persistenceUnitTest" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
        <property name="hibernate.connection.charSet" value="UTF-8"/>
    </properties>
</persistence-unit>

This is how my entity is defined:

@RooJavaBean
@RooToString
@RooEntity(identifierColumn = "COUPONID", identifierType = Integer.class, table = "COUPON")
public class Coupon {

The application context is set:

<context:component-scan base-package="com.tamiflu.entities">
    <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
</context:component-scan>

<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnitTest"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:mem:tamiflu"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="true"/>
    <property name="testWhileIdle" value="true"/>
    <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
    <property name="numTestsPerEvictionRun" value="3"/>
    <property name="minEvictableIdleTimeMillis" value="1800000"/>
</bean>

I feel like I have everything in place. I don't know why the @RooEntity isn't being picked up as being mapped.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仲春光 2024-12-26 03:29:15

Mikko Maunu 是对的,您的 SELECT 对于 JPA 不正确,正确的是 SELECT o FROM Coupon o。

@RooJpaActiveRecord 是即将发布的 Roo: 1.2 版本(当前为 RC1)的新注释,@Entity 是当前 1.1.5 及之前版本 Roo 的注释。

如果问题仍然存在:

  1. 检查您的实体。 Roo 在 AspectJ 文件(.aj 扩展名,类似 _Roo_Entity 或 _Roo_Jpa_ActiveRecord)中生成该方法,但您可以将其移动(或推入)到 Java 文件中。查看查找方法是否具有正确的语法(您必须使用实体的名称而不是表的名称,如 Mikko Maunu 所说)
  2. 检查扫描的包。在您的 applicationContext 中找到 context:component-scan 元素,并查看您要加载的实体是否属于基础包的子包,在您的情况下为 com.tamiflu.entities。

第二点的注释:Roo 创建使用通配符加载应用程序上下文文件的代码。

如果您有多个文件,请务必小心,因为无法保证顺序(如果您有多个包含不同包的文件要扫描,则加载实体时会遇到问题,除非您强制删除通配符并手动指定所需的顺序) applicationContext.xml 文件)

Mikko Maunu is right, your SELECT is not correct for JPA, the correct one is SELECT o FROM Coupon o.

@RooJpaActiveRecord is the new annotation for the incoming release of Roo: 1.2 (currently in RC1), and @Entity is the one for the current 1.1.5 and previous versions of Roo.

If the problem persists:

  1. Review your Entity. Roo generates the method in an AspectJ file (.aj extension, something like _Roo_Entity or _Roo_Jpa_ActiveRecord), but you can move (or push it in) to the Java file. See if the find methods have the correct syntax (you have to use the name of the Entity instead of the name of the table, as Mikko Maunu said)
  2. Review the scanned packages. Find in your applicationContext the context:component-scan element and see if the Entity you want to load belongs to a sub-package of the base-package, com.tamiflu.entities in your case.

A note for the second point: Roo creates code that loads the application context files using wild cards.

Be careful if you have more than one, because the order is not guaranteed (if you have several files with different packages to scan, you will have issues loading entities, unless you force the order removing the wild cards and manually specifying the desired order for the applicationContext.xml files)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文