springspring-roodatanucleus

DataNucleus: Class ... for query has not been resolved. Check the query and any imports specification


I have a problem, hopefully someone can give me some hints.

Environment:

The application is built using some Spring Roo generated code as basis, later modified and extended.

The issue is that, when starting the app and trying to load the data, I receive the exception:

 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

The weirdest thing is that the sample roo-generated aplication used as basis, with exactly the same dependencies, but a different modularization works like a charm, without this symptom, so I am puzzled now... Please also note that I tried to replace the 'Document' with the explicit qualification 'com.myvdm.server.domain.Document' in the query, with no positive result:

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

Another thing, although it might not be relevant, on every request, this exception is thrown:

 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.

The last exception is thrown by DataNucleus. It's also confusing, since I do not run in a Java EE container, but GWT development mode.

Here's the document entity:

@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;
}

The annotation @RooJpaActiveRecord adds EntityManager operations but these are declared in a separate file - ITD(inter-type declarations)

Any suggestions, please? Thanks a lot in advance.

----------- EDIT --------------

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 declaration

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;
    }
}

Solution

  • Ok, I found a fix for this problem. As I posted earlier, using Spring's applicationContext.xml and persistence.xml files as basic configuration I could not make it work. I deleted persistence.xml and instead I used this configuration (please note the usage of packagesToScan property and passsing DataNucleus properties - and basically all the info that was traditionally inside 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>
    

    So this is the only way I could make it work, could this be a Spring bug?

    And about that second (minor) issue, obviously the exception will be thrown since I am using Spring's LocalContainerEntityManagerFactoryBean :)