Hi everybody,
I'm using Hibernate 3.x and its extended JPA annotations to model a many to many relationship in my application. In summary, I have two "entity" tables, Workflow and Person plus one "relationship" table, WorkflowCoordinator. For each workflow there must be one or more persons, and each person may appear in zero, one or more workflows.
I modelled this with the following code:
public class Workflow {...
// Workflow Coordinators @ManyToMany (fetch=FetchType.LAZY) @CollectionId (columns=@Column (name="Id$"), type=@Type (type="int"), generator="idSequence") @SequenceGenerator (name="idSequence", sequenceName="S$Id", initialValue=1, allocationSize=1) @JoinTable (name="WorkflowCoordinator", joinColumns={ @JoinColumn (name="Workflow") }, inverseJoinColumns={ @JoinColumn (name="Person") }) @IndexColumn (name="Pos", base=0) protected java.util.List<com.mycompany.model.Person> workflowCoordinators;
The three tables, Workflow, Person and Workflowcoordinator, have "Id$" as primary key. The Workflowcoordinator has a foreign key to Workflow (column name is "workflow") and another foreign key to Person ( column name is "person" ). As I need to keep the order that was used to assign the coordinators to a given workflow ( because there is the concept of "main" coordinator ), I added a column named "pos" in the WorkflowCoordinator table. Example
Workflow(id$=100) ---> WorkflowCoordinator(id$=750, workflow=100, person=3500, pos=0) <--- Person (id=3500) Workflow(id$=100) ---> WorkflowCoordinator(id$=760, workflow=100, person=3600, pos=1) <--- Person (id=3600) Workflow(id$=100) ---> WorkflowCoordinator(id$=790, workflow=100, person=3500, pos=2) <--- Person (id=3700) ... Workflow(id$=200) ---> WorkflowCoordinator(id$=9950, workflow=200, person=3500, pos=0) <--- Person (id=3999) Workflow(id$=200) ---> WorkflowCoordinator(id$=9951, workflow=200, person=3600, pos=1) <--- Person (id=3500)
So, in this example, the person whose id$=3500 appears in two workflows as coordinator, and the given workflows have several coordinators.
The problem I have is that sometimes I can see that there's a "gap" in the list of coordinators. For instance:
Workflow(id$=100) ---> WorkflowCoordinator(id$=750, workflow=100, person=3500, pos=0) <--- Person (id=3500) Workflow(id$=100) ---> WorkflowCoordinator(id$=760, workflow=100, person=3600, pos=1) <--- Person (id=3600) Workflow(id$=100) ---> WorkflowCoordinator(id$=790, workflow=100, person=3500, pos=3) <--- Person (id=3700)
In this example, the pos=2 is missing ... and this causes a NullPointerException when Hibernate tries to populate the java.util.List of Person ( please see above workflowCoordinators property in the Workflow class )
Thank you very much in advance, and apologies if this case has been answered in othe post, but I haven't been able to see it.
Best regards,
TomCasas
|