[ https://issues.apache.org/jira/browse/OPENJPA-2429?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16983433#comment-16983433 ]
Frej Bjon edited comment on OPENJPA-2429 at 11/27/19 11:31 AM: --------------------------------------------------------------- It seems this bug can indeed show up even if all applicable fields have @OrderBy-annotations. In the following abbreviated example, trying to update (merge) an existing Element-entity after starting up the system results in this same NPE. However, persisting an ElementGroup causes the relevant internal metadata fields to be populated (i.e. {{_orderDec)}}, and everything works after that. There is no workaround that I can see short of using reflection to mess with the OpenJPA internals. The patch by [~jmntn2000] should work in this case too. {noformat} @Entity public class Element { @OneToMany(cascade=CascadeType.PERSIST, mappedBy="element", fetch=FetchType.LAZY ) @OrderBy("order asc") private List<PageElement> pageElement; @Column(name = "NAME", length=255) private String name; @ManyToOne( fetch=FetchType.EAGER ) private ElementGroup elementGroup; } @Entity public class PageElement { @Column( name="ORDER" ) private Long order; @ManyToOne( fetch=FetchType.EAGER ) private Element element; } @Entity public class ElementGroup { @OneToMany(cascade=CascadeType.ALL, mappedBy="elementGroup", fetch=FetchType.EAGER ) @OrderBy("name asc") private List<Element> element; } {noformat} was (Author: fbjon): It seems this bug can indeed show up even if all applicable fields have @OrderBy-annotations. In the following abbreviated example, trying to update (merge) an existing Element-entity after starting up the system results in this same NPE. However, persisting an ElementGroup causes the relevant internal metadata fields to be populated (i.e. {{_orderDec}}, and everything works after that. There is no workaround that I can see short of using reflection to mess with the OpenJPA internals. The patch by [~jmntn2000] should work in this case too. {noformat} @Entity public class Element { @OneToMany(cascade=CascadeType.PERSIST, mappedBy="element", fetch=FetchType.LAZY ) @OrderBy("order asc") private List<PageElement> pageElement; @Column(name = "NAME", length=255) private String name; @ManyToOne( fetch=FetchType.EAGER ) private ElementGroup elementGroup; } @Entity public class PageElement { @Column( name="ORDER" ) private Long order; @ManyToOne( fetch=FetchType.EAGER ) private Element element; } @Entity public class ElementGroup { @OneToMany(cascade=CascadeType.ALL, mappedBy="elementGroup", fetch=FetchType.EAGER ) @OrderBy("name asc") private List<Element> element; } {noformat} > NPE was thrown out when a @OrderBy modified column is being persisted. > ---------------------------------------------------------------------- > > Key: OPENJPA-2429 > URL: https://issues.apache.org/jira/browse/OPENJPA-2429 > Project: OpenJPA > Issue Type: Bug > Components: datacache > Affects Versions: 2.2.1 > Environment: Windows 7 > WebSphere 8.5 > Reporter: Kymair Wu > Priority: Major > Attachments: OpenJPA-2429.diff > > > Following NPE was thrown out when a @OrderBy modified column is being > persisted. > {quote} > Caused by: java.lang.NullPointerException > at > org.apache.openjpa.datacache.DataCachePCDataImpl.clearInverseRelationCache(DataCachePCDataImpl.java:179) > at > org.apache.openjpa.datacache.DataCachePCDataImpl.storeField(DataCachePCDataImpl.java:159) > {quote} > I notice that getOrderDeclaration() of FieldMetaData will return null when > both _orders and _orderDec are null, which cause the NPE. > The only place _orders will be set after class initialization seems to be > getOrders()? But getOrders() is not involved for all the FieldMetaData of > "fields[i].getInverseMetaDatas()" > I'm not sure whether this is a OpenJPA issue but whether this NPE should be > handled internally? > Google returns a potential case but no more information found. See > https://issues.apache.org/jira/browse/OPENJPA-2096?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel > and > http://mail-archives.apache.org/mod_mbox/openjpa-users/201006.mbox/%3c4c1be781.9000...@cyberspaceroad.com%3E > Thanks! -- This message was sent by Atlassian Jira (v8.3.4#803005)