EclipseLink JPA动态模型和标准查询

时间:2015-09-03 07:13:35

标签: jpa eclipselink jpa-2.0

我想针对动态模型创建(crtieria-)查询,但我总是得到异常

No [EntityType] was found for the key class [demo.DynamicResult] in the Metamodel

在最后一行

final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

final CriteriaQuery<Tuple> query = criteriaBuilder.createTupleQuery();   
DynamicHelper helper = new DynamicHelper(JpaHelper.getServerSession(emf));
Class<? extends DynamicEntity> rootclass = helper.getType("demo.DynamicResult").getJavaClass();
query.from(rootclass);

使用以下代码生成DynamicResult

ServerSession serverSession = JpaHelper.getEntityManager(entityManager).getServerSession();
DynamicClassLoader dcl = DynamicClassLoader.lookup(serverSession);
JPADynamicHelper jpaDynamicHelper = new JPADynamicHelper(entityManager);
Class<?> dynamicResult = dcl.createDynamicClass("demo.DynamicResult");

JPADynamicTypeBuilder dynamicResultBuilder = new JPADynamicTypeBuilder(dynamicResult, null, "DynamicResult");

dynamicResultBuilder.addDirectMapping("id", String.class, "id");

        //Some more addDirectMappings ....

dynamicResultBuilder.setPrimaryKeyFields("id");
type = dynamicResultBuilder.getType();
jpaDynamicHelper.addTypes(false, false, type);

我错过了什么? 这种查询是否可行?

此致

1 个答案:

答案 0 :(得分:1)

您可能会遇到https://bugs.eclipse.org/bugs/show_bug.cgi?id=429760错误。将新创建的动态实体元数据复制到当前会话中时出现问题。 检查此错误和附加的差异。 jpa / eclipselink.jpa.test / src / org / eclipse / persistence / testing / tests / jpa / dynamic / simple / SimpleQueryTestSuite.java测试套件包含测试,它做类似的事情 - 创建动态实体并在其中运行JPQL查询事务(UnitOfWork)。 我会尝试从https://www.eclipse.org/eclipselink/downloads/nightly.php开始编写最新的2.7.0或2.6.1(不要使用.WAS)来查看它是否适用于您。