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)
@Entity @Table(name = "Charger") public class Charger extends ChargerEntityAbstract implements Serializable { @OneToMany(mappedBy=" Charger charger ",targetEntity=PinEntityExt2.class, fetch=FetchType.EAGER) @Fetch(value = FetchMode.SUBSELECT) public List<PinEntityExt2> pinEntityExtList; public List<PinEntityExt2> getPinEntityExtList() { return pinEntityExtList; }
public void setPinEntityExtList(List<PinEntityExt2> pinEntityExtList) { this.pinEntityExtList = pinEntityExtList; } }
2)
@MappedSuperclass public class ChargerEntityAbstract implements Serializable {
private static final long serialVersionUID = 1L;
@Id @Column(name="ID") private Long Id;
@Column(name="CHARGER_ID", nullable = true) private String chargerId; // *** Constructor **** public ChargerEntityAbstract() { }
// *** Getter and Setters *** public Long getId() { return this.Id; } public void setId(Long Id) { this.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.
@Entity @Table(name = "PIN") public class PinEntityExt2 extends PinEntityAbstract 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 Charger charger; public Charger getCharger() { return this.charger; }
public void setCharger(Charger charger) { this.charger = charger; } }
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
@MappedSuperclass @IdClass(ChargerPin.class) public class PinEntityAbstract implements Serializable{ @Transient private static final long serialVersionUID = 1L; @Id @Column(name="CHARGER_ID", nullable = true) private String chargerId; @Id @Column(name="PIN_NUMBER", nullable = true) private Long pinNumber; public String getChargerId() { return chargerId; }
public void setChargerId(String chargerId) { this.chargerId = chargerId; }
public Long getPinNumber() { return pinNumber; }
public void setPinNumber(Long pinNumber) { this.pinNumber = pinNumber; }
}
public class ChargerPin implements java.io.Serializable{
@Transient private static final long serialVersionUID = 1L;
private String chargerId; private Long pinNumber;
// **** Constructor ***** public ChargerPin() { } public ChargerPin(String chargerId, Long pinNumber) { this.chargerId = chargerId; this.pinNumber = pinNumber; }
public String getChargerId() { return chargerId; }
public void setChargerId(String chargerId) { this.chargerId = chargerId; }
public Long getPinNumber() { return pinNumber; }
public void setPinNumber(Long pinNumber) { this.pinNumber = pinNumber; }
@Override public int hashCode() { final int prime = 31; int result = 1; int chargerId = chargerId != null ?(Integer.parseInt(chargerId)):0; int pinNumber = pinNumber!=null?(pinNumber).intValue():0; result = prime * result + chargerId; result = prime * result + pinNumber; return result; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof ChargerPin)) return false; ChargerPin other = (ChargerPin) obj; if (chargerId == null) { if (other.chargerId != null) return false; } else if (!chargerId.equals(other.chargerId)) return false; if (pinNumber == null) { if (other.pinNumber != null) return false; } else if (pinNumber != other.pinNumber) return false; return true; } }
5) Now while I'm invoking the below query I'm getting a nullPointer exception with Empty satck trace.
List<Charger> ChargerList = null; Query query = null; String queryString = "SELECT ce FROM Charger ce"; try { em = getChargerEntityManager(); query = em.createQuery(queryString); ChargerList = query.getResultList();
} catch (IllegalStateException e) { log.error(e); } catch (IllegalArgumentException e) { log.error(e); } catch (Exception e) { log.error(e); } finally{ closeEntityManager(em); }
Can you please help me here. IS there anything wrong with the COMPOSITE Key implementation. 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. |
|