Eclipselink 2.5.2 JPA NPE在glassfish 4上查询elementcollection

时间:2014-08-28 11:02:14

标签: jpa glassfish eclipselink jpql glassfish-4

我面临一个奇怪的问题,即将工作应用程序从JEE6(glassfish 3 / eclipselink 2.5.1)移植到JEE7(glassfish 4 / eclipselink 2.5.2)。

我是这些实体(getter / setter是项目Lombok注释):

@Entity
@Table(name = "languages")
public class Language {
  @Getter
  @Setter
  @NotNull
  @Column(name = "code")
  private String code;

  @Getter
  @Setter
  @ManyToOne(cascade = CascadeType.ALL)
  @JoinColumn(name = "translation_id")
  private Translation translations = new Translation();
}

@Entity
@Table(name = "translations")
public class Translation { 
  @ElementCollection
  @MapKeyColumn(name="locale")
  @Column(name="description")
  @CollectionTable(name="translations_values")
  private Map<String, String> strings = new HashMap<>();

  public Translation() {
  }

  public Translation(Map<String, String> map) {
    this.strings = map;
  }

  public void setString(String locale, String text) {
    strings.put(locale, text);
  }

  public String getString(String locale) {
    String returnValue = strings.get(locale);
    return (returnValue != null ? returnValue : null);
  }
}

如果我运行这个JPQL查询,它可以工作:

select o from Language o join o.translations t join t.strings s where key(s) = 'it' and value(s) = 'Italiano'

如果我在elementcollection上运行相同的查询添加order by子句,它就不起作用:

select o from Language o join o.translations t join t.strings s where key(s) = 'it' and value(s) = 'Italiano' order by value(s)

结果是:

Local Exception Stack: 
Exception [EclipseLink-6168] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.QueryException
Exception Description: Query failed to prepare, unexpected error occurred: [java.lang.NullPointerException].
Internal Exception: java.lang.NullPointerException
Query: ReadAllQuery(referenceClass=Language jpql="select o from Language o join o.translations t join t.strings s where key(s) = 'it' and value(s) = 'Italiano' order by value(s)")
    at org.eclipse.persistence.exceptions.QueryException.prepareFailed(QueryException.java:1589)
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:680)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:901)
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:613)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:194)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603)
Caused by: java.lang.NullPointerException
    at org.eclipse.persistence.mappings.ForeignReferenceMapping.getOrderByNormalizedExpressions(ForeignReferenceMapping.java:2456)
    at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalizeOrderBy(SQLSelectStatement.java:1614)
    at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:1403)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildNormalSelectStatement(ExpressionQueryMechanism.java:549)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareSelectAllRows(ExpressionQueryMechanism.java:1720)
    at org.eclipse.persistence.queries.ReadAllQuery.prepareSelectAllRows(ReadAllQuery.java:813)
    at org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:744)
    at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:661)
    ... 9 more

我在glassfish 3 / eclipselink 2.5.1上的应用程序在上面的JPQL查询中运行良好。

有解决方法吗?

非常感谢提前

0 个答案:

没有答案