我有一个问题,希望有人能给我一些提示。
环境:
maven 项目具有两个模块
一个模块是“模型”,并具有DataNucleus 3.1、HSQLDB和Spring 3> 依赖关系。 HSQLDB 在内存中嵌入运行,从 spring applicationContext.xml 配置
另一个模块是“web”并且具有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;
}
}
最佳答案
好的,我找到了解决此问题的方法。 正如我之前发布的,使用 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 :)
关于spring - DataNucleus:查询的类...尚未解决。检查查询和任何进口规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11925449/