Getting nullpointer exception while executing query

Description

Hi,

I'm trying to upgrade my application from OpenJPA implementation 1.0.1 which was using JPA 1.0 to Hibernate 5.1.2

I'm getting nullpointer exception when trying to to call geResultList on a Entity class.

My Sample classes are below: (I've changed the class names as I can not share my original source code. But the mapping are same.)

1) Class Charger (Representing a sort of mobile charger that has 2 Pin)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 @Entity @Table(name = "Charger") class ChargerEntityExt5 extends ChargerEntity1Abstract implements Serializable { @Transient private static final long serialVersionUID = -1L; @OneToMany(mappedBy="chargerEntityExt5",targetEntity=PortEntityExt5.class, fetch=FetchType.EAGER) @Fetch(value = FetchMode.SUBSELECT) public List<PinEntityExt5> pinEntityExtList; public List<PinEntityExt5> getPinEntityExtList() { return pinEntityExtList; } public void setPinEntityExtList(List<PinEntityExt5> pinEntityExtList) { this.pinEntityExtList = pinEntityExtList; } }

2)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 @MappedSuperclass class ChargerEntity1Abstract implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name="ID") public Long Id; @Column(name="CHARGER_ID") public String chargerId; // *** Constructor **** public ChargerEntity1Abstract() { } @Override public String toString() { return "ChargerEntity [cpeId=" + Id!=null? Id.toString():"" + ", chargerId=" + chargerId!=null? chargerId.toString():"" + "]"; } public Long getId() { return Id; } public void setId(Long id) { Id = id; } public String getChargerId() { return chargerId; } public void setChargerId(String chargerId) { this.chargerId = chargerId; } }

3) every charger has 2 pins so I've declared a bi-directional one to many mapping between Charger and pin.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 @Entity @Table(name = "PIN") class PinEntityExt5 extends PinEntity1Abstract implements Serializable{ @Transient private static final long serialVersionUID = -1L; @ManyToOne(optional=false) @NotFound(action = NotFoundAction.IGNORE) @JoinColumn(name="CHARGER_ID", referencedColumnName="CHARGER_ID", insertable= false, updatable = false) private ChargerEntityExt5 chargerEntityExt5; public ChargerEntityExt5 getChargerEntityExt5() { return chargerEntityExt5; } public void setChargerEntityExt5(ChargerEntityExt5 chargerEntityExt5) { this.chargerEntityExt5 = chargerEntityExt5; } }

4) PIN Entity has 2 properties. CHARGER_ID and PIN_NUMBER. this 2 field defines one record in the PIN table uniquely. So I've declared a IDclass (ChargerPin) for representing the composite key

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 @MappedSuperclass @IdClass(ChargerPin.class) class PinEntity1Abstract implements Serializable{ @Transient private static final long serialVersionUID = 1L; @Id @Column(name="CHARGER_ID", nullable = true) public String chargerId; @Id @Column(name="PIN_NUMBER", nullable = true) public Long pin; public PinEntity1Abstract() { } @Override public String toString() { return "PinEntity [chargerId=" + chargerId + ", port=" + pin!=null? pin.toString():"" + "]"; } public String getChargerId() { return chargerId; } public void setChargerId(String chargerId) { this.chargerId = chargerId; } public Long getPin() { return pin; } public void setPin(Long pin) { this.pin = pin; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 class ChargerPin implements java.io.Serializable{ @Transient private static final long serialVersionUID = 1L; private String chargerId; private Long pin; // **** Constructor ***** public ChargerPin() { } public ChargerPin(String kapId, Long port) { this.chargerId = kapId; this.pin = port; } @Override public String toString() { return "kapId [chargerId=" + chargerId!=null? chargerId.toString():"" + ", port=" + pin!=null? pin.toString():"" + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; int kap = chargerId != null ?(Integer.parseInt(chargerId)):0; int kap_port = pin!=null?(pin).intValue():0; result = prime * result + kap; result = prime * result + kap_port; return result; } @Override public boolean equals(Object obj) { //return ((o instanceof CpePort) && (kapId.equalsIgnoreCase(((CpePort) o).getKapId())) && (port == (((CpePort) o).getPort()))); if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof CpePort1)) return false; ChargerPin other = (ChargerPin) obj; if (chargerId == null) { if (other.chargerId != null) return false; } else if ((other.chargerId != null) && chargerId.equals(other.chargerId)) { return false; } if (pin == null) { if (other.pin != null) return false; } else if ((other.pin != null) && (pin != other.pin)) return false; return true; } public String getChargerId() { return chargerId; } public void setChargerId(String chargerId) { this.chargerId = chargerId; } public Long getPin() { return pin; } public void setPin(Long pin) { this.pin = pin; } }

5) Now while I'm invoking the below query I'm getting a nullPointer exception with Empty satck trace.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 public class HibernateTestClass { /** * @param args */ public static EntityManagerFactory entityManagerFactory; public static void main(String[] args) { // TODO Auto-generated method stub EntityManager em = null; List<ChargerEntityExt5> ChargerList = null; Query query = null; String queryString = "SELECT ce FROM ChargerEntityExt5 ce WHERE ce.chargerId=?1"; try { try { em = createEntityManager(); } catch (Exception e) { System.out.println(e); } query = em.createQuery(queryString); query.setParameter(1, "110284026"); ChargerList = query.getResultList(); System.out.println(ChargerList.size()); } catch (IllegalStateException e) { System.out.println(e); } catch (IllegalArgumentException e) { System.out.println(e); } catch (Exception e) { System.out.println(e); } finally{ em.close(); } } public static EntityManager createEntityManager() { entityManagerFactory = Persistence.createEntityManagerFactory("polcaincapu"); return entityManagerFactory.createEntityManager(); } }

Can you please look into this bug.
Earlier I was trying with Hibernate 5.1.1. version and then the code was throwing error as "Error accessing field by reflection for persistent property".
Then I changed the code base to use Hibernate version 5.1.2 and then without change of source code the exception got changed into Nullpointer exception.

Environment

Hibernate 5.1.2 running on Weblogic 12.1.2 with Java 1.6 SDK.

Status

Assignee

Unassigned

Reporter

sagnik sarkar

Fix versions

None

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

5.1.2

Priority

Blocker
Configure