Introduction
I have a problem that I’m finding very mysterious. I’ve searched on Google and StackOverflow, and not found anyone having a similar problem. I tried switching our persistence provider to Hibernate, but our code is too dependent on EclipseLink idiosyncrasies to make that a practical option for debugging. If this problem persists (ha, ha; Java EE pun), I may well re-write all persistence code for Hibernate on the off-chance it helps.
Short version:
- An entity of mine,
ExperimentBlock, is being correctly persisted to the database, and its autoincrement primary key is being generated correctly. However, when it is read from the database, EclipseLink gives me the entity with a null primary key field. This is causing a devastating bug in production software. ExperimentBlockinherits its primary key field with annotations, a getter, and a setter, from its superclassPeriodResident. Several other subclasses ofPeriodResidentdo the same, but EclipseLink correctly loads their primary key. This is the mystery of this bug: the exact same field/getter/setter code is being used for every subclass ofPeriodResident, butExperimentBlockis the only one that EclipseLink returns with a nulled-out primary key!- Running a JPQL query that selects
ExpermentBlocks returns them with null primary keys. (Sadface.) However, running a JPQL query that specifically selects the primary key field ofExperimentBlockworks correctly, and returns a list of integer primary keys! This is seen in the queryExperimentBlock.findWithIdsBySchedulebelow.
Details
GlassFish 3.1.2, EclipseLink 2.4.0, Java EE 6, MySQL 5.0.95-log
I am not getting any exception. This problem occurs in remote and local session beans.
Code follows, please let me know if I should provide any more details.
PeriodResident.java
(The superclass of the problem ExperimentBlock. This is where the primary key field — uniqueID — is defined)
package ohno;
import ohno.ShiftDateConverter;
import ohno.Shift;
import ohno.Facility;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.eclipse.persistence.annotations.Convert;
import org.eclipse.persistence.annotations.Converter;
@Converter(converterClass=ShiftDateConverter.class, name="ShiftDateConverter")
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name="bs_blocks")
@DiscriminatorColumn(name="block_type")
public abstract class PeriodResident implements Serializable {
private static final long serialVersionUID = 1L;
public PeriodResident() {
}
/**
* Sets the database unique id.
*
* @param uniqueID the new database unique id
*/
public void setUniqueID(Integer uniqueID) {
this.uniqueID = uniqueID;
}
/**
* Provides a useful super-constructor handling initialization of the first
* and last Shifts of the PeriodResident's time block, and the Facility
* where it takes place.
*
* @param startShift The first Shift in the PeriodResident's time block.
* @param endShift The last Shift in the PeriodResident's time block.
* @param facility The Facility where the PeriodResident takes place.
* @param uniqueID the unique id
*/
public PeriodResident(Shift startShift, Shift endShift, Facility facility,
int uniqueID) {
this.startShift = startShift;
this.endShift = endShift;
this.facility = facility;
this.uniqueID = uniqueID;
}
/**
* Instantiates a new period resident.
*
* @param startShift The first Shift in the PeriodResident's time block.
* @param endShift The last Shift in the PeriodResident's time block.
* @param facility The Facility where the PeriodResident takes place.
*/
public PeriodResident(Shift startShift, Shift endShift, Facility facility) {
this(startShift, endShift, facility, null);
}
/**
* Gets the facility.
*
* @return the Facility where the PeriodResident takes place.
*
* @see #setFacility(Facility)
*/
public Facility getFacility() {
return facility;
}
/**
* Gets the shifts.
*
* @return the total number of Shifts in the PeriodResident's time block.
*
* @see #getStartShift()
* @see #getEndShift()
*/
public int getShifts() {
return endShift.getShiftsDifference(startShift);
}
/**
* Gets the start shift.
*
* @return the first <code>Shift</code> in the PeriodResident's time
* block.
*
* @see #setStartShift(Shift)
* @see #getShifts()
*/
public Shift getStartShift(){
return startShift;
}
/**
* Gets the end shift.
*
* @return the last <code>Shift</code> in the PeriodResident's time block.
*
* @see #setEndShift(Shift)
* @see #getShifts()
*/
public Shift getEndShift(){
return endShift;
}
/**
* Gets the database table unique ID for this particular PeriodResident, or null if
* it hasn't one.
*
* @return this PeriodResident's unique ID in the database.
*/
public Integer getUniqueID() {
return uniqueID;
}
/**
* sets this PeriodResident's facility location with the given Facility.
*
* @param facility the PeriodResident's new Facility.
*
* @see #getFacility()
*/
public void setFacility(Facility facility) {
this.facility = facility;
}
/**
* sets this PeriodResident's time block's new starting shift with the given
* Shift.
*
* @param startShift the new starting shift.
*
* @see #getStartShift()
*/
public void setStartShift(Shift startShift) {
this.startShift = startShift;
}
/**
* sets this PeriodResident's time block's new ending shift with the given
* Shift.
*
* @param endShift the new ending shift.
*
* @see #getEndShift()
*/
public void setEndShift(Shift endShift) {
this.endShift = endShift;
}
public Schedule getSchedule() {
return schedule;
}
public void setSchedule(Schedule schedule) {
this.schedule = schedule;
}
protected void copyCharacteristics(PeriodResident original, PeriodResident clone) {
clone.setStartShift(original.getStartShift().clone());
clone.setEndShift(original.getEndShift().clone());
clone.setFacility(original.getFacility());
clone.setSchedule(original.getSchedule());
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "PeriodResident (Facility: " + facility + ") from " + startShift + " to " + endShift;
}
@Override
public abstract Object clone();
public abstract String getResidentType();
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final PeriodResident other = (PeriodResident) obj;
if ((uniqueID == null && other.uniqueID != null) || (this.uniqueID != null && !this.uniqueID.equals(other.uniqueID))) {
return false;
}
return true;
}
@Override
public int hashCode() {
if (uniqueID != null) {
return uniqueID;
}
int hash = 7;
hash = 73 * hash + (this.uniqueID != null ? this.uniqueID.hashCode() : 0);
hash = 73 * hash + (this.startShift != null ? this.startShift.hashCode() : 0);
hash = 73 * hash + (this.endShift != null ? this.endShift.hashCode() : 0);
hash = 73 * hash + (this.facility != null ? this.facility.hashCode() : 0);
hash = 73 * hash + (this.schedule != null ? this.schedule.hashCode() : 0);
hash = 73 * hash + getClass().hashCode();
return hash;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equivalent(PeriodResident other) {
if (this == other) {
return true;
}
if (other == null) {
return false;
}
if (getClass() != other.getClass()) {
return false;
}
if (endShift == null) {
if (other.endShift != null) {
return false;
}
} else if (!endShift.equals(other.endShift)) {
return false;
}
if (facility == null) {
if (other.facility != null) {
return false;
}
} else if (!facility.equals(other.facility)) {
return false;
}
if (startShift == null) {
if (other.startShift != null) {
return false;
}
} else if (!startShift.equals(other.startShift)) {
return false;
}
if (schedule == null) {
if (other.getSchedule() != null) {
return false;
}
} else if (!schedule.equals(other.getSchedule())) {
return false;
}
return true;
}
public Date getStartShiftDate() {
return getStartShift().getTime();
}
public Date getEndShiftDate() {
return getEndShift().getTime();
}
/** The database unique id. */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
Integer uniqueID;
/** The start shift of the PeriodResident. */
@Column(name = "start_time")
@Convert(value="ShiftDateConverter")
Shift startShift;
/** The end shift of the PeriodResident.*/
@Column(name = "end_time")
@Convert(value="ShiftDateConverter")
Shift endShift;
/** The facility where the PeriodResident lives. */
@ManyToOne
@JoinColumn(name = "facility", referencedColumnName = "ID")
Facility facility;
@ManyToOne
@JoinColumn(name = "schedule", referencedColumnName = "ID")
private Schedule schedule;
}
ExperimentBlock.java
(The problem class … note the last JPQL query, which selects an ExperimentBlock and its uniqueID. When this query is run, ExperimentBlock.uniqueId is null, but the uniqueID that’s selected manually is correct.)
package ohno;
import ohno.Experiment;
import ohno.Role;
import java.io.Serializable;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;
/**
*
* @author Nick
*/
@Entity
@Table(name = "bs_blocks_experiment")
@NamedQueries({
@NamedQuery(name = "ExperimentBlock.findAll", query = "SELECT e FROM ExperimentBlock e"),
@NamedQuery(name = "ExperimentBlock.findActualByExperiment", query = "SELECT e FROM ExperimentBlock e WHERE e.experiment = :experiment AND e.schedule.scheduleProperties.revision = :active ORDER BY e.schedule.id DESC, e.uniqueID DESC"),
@NamedQuery(name = "ExperimentBlock.findByTime", query = "SELECT e FROM ExperimentBlock e WHERE e.startShift < :endTime AND e.endShift > :startTime and e.schedule = :schedule"),
@NamedQuery(name = "ExperimentBlock.findWithIdsBySchedule", query = "SELECT e, e.uniqueID FROM ExperimentBlock e WHERE e.schedule = :schedule")})
@DiscriminatorValue(value = "EXPERIMENT_BLOCK")
public class ExperimentBlock extends PeriodResident implements Serializable {
private static final long serialVersionUID = 1L;
// #########
// Some columns are inherited from PeriodResident!
// #########
@Embedded
protected Equipment equipment;
@ManyToOne
@JoinColumn(name = "experiment", referencedColumnName = "exp_id")
private Experiment experiment;
public ExperimentBlock() {
}
public Equipment getEquipment() {
return equipment;
}
public void setEquipment(Equipment equipment) {
this.equipment = equipment;
}
public Experiment getExperiment() {
return experiment;
}
public void setExperiment(Experiment experiment) {
this.experiment = experiment;
}
@Override
public boolean equivalent(PeriodResident pr) {
if (!super.equivalent(pr)) {
return false;
}
ExperimentBlock other = (ExperimentBlock) pr;
if (!getExperiment().equals(other.getExperiment())) {
return false;
}
if (!getEquipment().equals(other.getEquipment())) {
return false;
}
return true;
}
@Override
public ExperimentBlock clone() {
ExperimentBlock clone = new ExperimentBlock();
copyCharacteristics(this, clone);
clone.setExperiment(getExperiment());
clone.setEquipment(equipment.clone());
return clone;
}
@Override
public String getResidentType() {
return RESIDENT_TYPE;
}
@Transient
public static final String RESIDENT_TYPE = "ExperimentBlock";
@Override
public String toString() {
return "Experiment " + experiment + "\nChannel: " + getFacility()
+ "\nEquipment: " + getEquipment() + "\nFrom "
+ getStartShift() + "\nto " + getEndShift();
}
/**
* Gets an HTML string representation of this.
*
* @return the string
*/
public String toFancyString() {
return "<html>Experiment <b>" + (experiment.isNoExperiment() ? "N/A" : experiment) + "</b><br>Channel: " + getFacility()
+ "<br>Equipment: " + getEquipment() + "<br>From: <b>"
+ getStartShift().toString(Role.BEGINNING) + "</b><br>To: <b>" + getEndShift().toString(Role.ENDING)
+ "</b><br>Shift Count: <b>" + getShifts() + "</b></html>";
}
}
&c.
So I run the query ExperimentBlock.getWithIdsBySchedule in a session bean. I debug, put in a breakpoint right after running that query, and inspect the results. In the query results, I get a list of [ExperimentBlock, Integer] — the ExperimentBlocks’ uniqueIDs are all null; however, the Integers — the manually-queried uniqueIDs of the ExperimentBlocks — are all populated correctly.
Conclusion
Any ideas? This would really help me out of a jam. Again, let me know if there’s any more information I could provide.
Addenda
- Hackish (viz. “cludge”) solutions to this problem are definitely welcome, as well as elegant solutions.
- I checked the database (table definitions, foreign keys, etc.) and can’t find anything special about
ExperimentBlockthere that could explain my problem. - (From comment below:) All other persisted properties of
ExperimentBlockare loading correctly. Loading anExperimentBlockwithem.find()results in the same problem. (Above, I loaded it in from a JPQL query and as a property of a different class,Schedule.)
Odd. There must be something different with this object from the siblings that work.
A few things you could try to narrow down the issue,
Also enable logging, and include the SQL generated for the query and the full exception stack trace.