3

我有一个问题,希望有人能给我一些提示。

环境:

  • 具有两个模块的maven项目

  • 一个模块是“模型”,具有DataNucleus 3.1HSQLDBSpring 3依赖项。HSQLDB 在内存中运行嵌入式,从 spring applicationContext.xml 配置

  • 另一个模块是“网络”并且具有GWT 依赖项

该应用程序是使用一些 Spring Roo 生成的代码作为基础构建的,随后对其进行了修改和扩展。

问题是,在启动应用程序并尝试加载数据时,我收到异常:

 Class Document for query has not been resolved. Check the query and any imports specification; nested exception is javax.persistence.PersistenceException: Class Document for query has not been resolved. Check the query and any imports specification

最奇怪的是,示例 roo 生成的应用程序用作基础,具有完全相同的依赖关系,但不同的模块化就像一个魅力,没有这个症状,所以我现在很困惑......请注意我试图替换查询中具有显式限定“com.myvdm.server.domain.Document”的“文档”,没有肯定结果:

return entityManager().createQuery("SELECT COUNT(o) FROM Document o", Long.class).getSingleResult(); 

另一件事,尽管它可能不相关,但在每个请求上,都会引发此异常:

 DEBUG org.springframework.orm.jpa.EntityManagerFactoryUtils - Unexpected exception on closing JPA EntityManager [INFO] java.lang.IllegalStateException: EntityManager is managed by a container (JEE) and so cannot be closed by calling the EM.close() method. Please read JPA2 spec 3.1.1 for the close() method.

最后一个异常是由 DataNucleus 引发的。这也令人困惑,因为我不是在 Java EE 容器中运行,而是在 GWT 开发模式下运行。

这是文档实体:

@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Document {

    @NotNull
    private String name;

    @ManyToOne
    private DocumentType type;

    @OneToMany(fetch = FetchType.EAGER,
        cascade = CascadeType.ALL)
    private Set<Field> fields;
}

注释 @RooJpaActiveRecord 添加了 EntityManager 操作,但这些操作是在单独的文件中声明的 - ITD(类型间声明)

请问有什么建议吗?提前非常感谢。

- - - - - - 编辑 - - - - - - -

privileged aspect Document_Roo_Jpa_ActiveRecord {

    @PersistenceContext
    transient EntityManager Document.entityManager;

    public static final EntityManager Document.entityManager() {
        EntityManager em = new Document().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long Document.countDocuments() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Document o", Long.class).getSingleResult();
    }

    public static List<Document> Document.findAllDocuments() {
        return entityManager().createQuery("SELECT o FROM Document o", Document.class).getResultList();
    }

    public static Document Document.findDocument(Long id) {
        if (id == null) return null;
        return entityManager().find(Document.class, id);
    }

    public static List<Document> Document.findDocumentEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Document o", Document.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    @Transactional
    public void Document.persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void Document.remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Document attached = Document.findDocument(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void Document.flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void Document.clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public Document Document.merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Document merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }
}

@Entity 声明

privileged aspect Document_Roo_Jpa_Entity {

    declare @type: Document: @Entity;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long Document.id;

    @Version
    @Column(name = "version")
    private Integer Document.version;

    public Long Document.getId() {
        return this.id;
    }

    public void Document.setId(Long id) {
        this.id = id;
    }

    public Integer Document.getVersion() {
        return this.version;
    }

    public void Document.setVersion(Integer version) {
        this.version = version;
    }
}
4

2 回答 2

2

好的,我找到了解决此问题的方法。正如我之前发布的,使用 Spring 的 applicationContext.xml 和 persistence.xml 文件作为基本配置,我无法使其工作。我删除了persistence.xml,而是使用了这个配置(请注意packagesToScan属性的使用和传递DataNucleus属性——基本上所有传统上在persistence.xml中的信息):

 <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
      id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="packagesToScan" value="com.myvdm.server.domain"/>
    <property name="persistenceProviderClass" value="org.datanucleus.api.jpa.PersistenceProviderImpl"/>
    <property name="jpaPropertyMap">
        <map>
            <entry key="datanucleus.ConnectionDriverName" value="org.hsqldb.jdbc.JDBCDriver"/>
            <entry key="datanucleus.storeManagerType" value="rdbms"/>
            <entry key="datanucleus.ConnectionURL" value="jdbc:hsqldb:mem:myvdm"/>
            <entry key="datanucleus.ConnectionUserName" value="sa"/>
            <entry key="datanucleus.ConnectionPassword" value=""/>
            <entry key="datanucleus.autoCreateSchema" value="true"/>
            <entry key="datanucleus.autoCreateTables" value="true"/>
            <entry key="datanucleus.autoCreateColumns" value="false"/>
            <entry key="datanucleus.autoCreateConstraints" value="false"/>
            <entry key="datanucleus.validateTables" value="false"/>
            <entry key="datanucleus.validateConstraints" value="false"/>
            <entry key="datanucleus.jpa.addClassTransformer" value="false"/>
        </map>
    </property>
    <property name="dataSource" ref="dataSource"/>
</bean>

所以这是我让它工作的唯一方法,这可能是一个 Spring 错误吗?

关于第二个(次要)问题,显然会抛出异常,因为我使用的是 Spring 的 LocalContainerEntityManagerFactoryBean :)

于 2012-08-17T16:23:39.877 回答
1

这不是 Spring 错误,它只是一个“调试”级别的消息。要禁止显示该消息,请更改日志级别<logger name="org.springframework.orm.jpa.EntityManagerFactoryUtils" additivity="false" level="error"/>

在此处查看我的 ORM 演示:https ://github.com/gordonad/core-spring-demos/tree/master/demos/orms

于 2013-01-15T21:19:05.417 回答