我有一个问题,希望有人可以给我一些提示。
环境:
maven 项目两个模块
一个模块是'模型',并且 DataNucleus 3.1 , HSQLDB 和 Spring 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生成的aplication作为基础,具有完全相同的依赖性,但是不同的模块化就像魅力一样,没有这种症状,所以我现在感到困惑...... 另请注意,我尝试在查询中使用显式限定条件'com.myvdm.server.domain.Document'替换'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;
}
}
答案 0 :(得分:1)
好的,我找到了解决这个问题的方法。 正如我之前发布的那样,使用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 bug吗?
关于第二个(次要)问题,显然会抛出异常,因为我使用的是Spring的LocalContainerEntityManagerFactoryBean:)
答案 1 :(得分:1)
这不是一个Spring bug,它只是一个“Debug”级别的消息。要取消该消息,请更改日志级别<logger name="org.springframework.orm.jpa.EntityManagerFactoryUtils" additivity="false" level="error"/>
。
请在此处查看我的ORM演示:https://github.com/gordonad/core-spring-demos/tree/master/demos/orms