We’re trying to create JPA mappings atop some read only tables we imported from somebody else’s application. These are multiple 10-billion row tables, so changing their schemas is not an option. We’ve got one table, the Message table, which has an OBJECT_ID value and another table, the DistributionGroup table, which will have many rows of ENTITY_IDs associated with any given OBJECT_ID. The relevant table definitions are as follows:
CREATE TABLE Message (
OBJ_ID varchar(255) NOT NULL,
FileName varchar(255) NOT NULL,
KEY FileName (FileName)) ENGINE=InnoDB;
CREATE TABLE DistributionGroup (
OBJ_ID varchar(255) NOT NULL,
ENTITY_ID varchar(255) NOT NULL,
KEY OBJ_ID (OBJ_ID)) ENGINE=InnoDB;
And the JPA mapping to link these two:
public class MessageRecord {
private String obj_id;
private String file;
private List<DGRecord> list = new ArrayList<DGRecord>();
@Id
@Column(name = "OBJ_ID", nullable = false)
public String getObjID () { return obj_id; }
public void setObjID (String obj_id) { this.obj_id = obj_id; }
//... (Similar for FileName)
@OneToMany
@JoinColumn(name="OBJ_ID", referencedColumnName="OBJ_ID")
public List<DGRecord> getDGRecordList() { return list; }
public void setDGRecordList(List<DGRecord> list) { this.list = list; }
}
public class DGRecord {
private String obj_id;
private String entity_id;
@Id
@Column(name = "OBJ_ID", nullable = false)
public String getObjID () { return obj_id; }
public void setObjID (String obj_id) { this.obj_id = obj_id; }
@Column(name = "ENTITY_ID", nullable = false)
public String getEntityId () { return entity_id; }
public void setEntityId (String entity_id) { this.entity_id = entity_id; }
}
Now, the strange bit happens when we’re running some code to iterate over all of the DGRecords for a given MessageRecord:
MessageRecord record = [obtained earlier];
for (DGRecord dg : record.getDGRecordList()) {
System.out.println(dg.getEntityId());
//Do some work with the ENTITY_ID
}
When I run this operation manually against the database, I get what I’m expecting to see:
SELECT * FROM DistributionGroup WHERE OBJ_ID = 'ArbitraryObjID';
OBJ_ID, ENTITY_ID
ArbitraryObjID, EntityID1
ArbitraryObjID, EntityID2
ArbitraryObjID, EntityID3
But the output from the actual code, when record has the same ArbitraryObjID, is:
EntityID1
EntityID1
EntityID1
For any given combination, it’s not returning n different DGRecords, but the same DGRecord values n times, where n is the number of distinct rows returned by manually running the query. I’m not sure if this is relevant or not, but it’s actually looping over the same object n times (evidenced by System.out.println(dg) returning the same package.DistributionGroup@MemoryAddress n times).
What are we doing wrong, and how can we fix it? Keep in mind that table schema changes, or adding a join table, are so costly as to be effectively impossible. But it seems like this should still be able to work given the current setup, since it works well enough as a human.
I think your mapping is wrong,
try,