Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] OneToMany list insertion problem


I have a bi-directional ManyToOne mapping between two entities.  I maintain the order of the list on the OneToMany side based on a ranking field.  Initially I can create the entities and everything is fine.  After that, when I use a question line, I remove it from the list, update the ranking and then add it back into the list based on the new ranking value.  I realize that the ManyToOne side is the owning side and it's up to me to maintain the list order in memory on the ManyToOne side, so that's what I'm trying to do.

Everything seems to work fine at first.  Just before the EJB transaction finishes, I log the elements of the list and everything is as I expect.  However, when I make the next request (i.e., a new EJB transaction),  the list shows that the element was removed from the list, but never added back in.  This doesn't match what it previously showed, however, in the first EJB transaction context.

If I do a JPA query for the elements that are actually in the database, all of the elements are there.  And if I restart the server to force everything to be restored from the database, it all comes back as I expected (i.e., with the element in the list and ordered as I expected), so it seems to be a caching issue somehow with the persistence context.  I can't figure out why, in the new transaction, I would see the change from removing the element, but not from inserting it.  They both happen in the same transaction so I should see either both changes or neither.

Does anyone have any ideas as to what might be causing this problem?  I've tried a variety of things, and nothing has worked so far.

Thanks for any help,
Renee



The relevant parts of the entities are:

public class UserSelectorQuestionLine implements Serializable {
    private Integer ranking;
    @ManyToOne
    private UserSelectorQuestionGroup questionGroup
}

and

public class UserSelectorQuestionGroup implements Serializable {
    @OrderBy("ranking ASC")
    @OneToMany(fetch=FetchType.EAGER, mappedBy="questionGroup")
    private List<UserSelectorQuestionLine> questionLines;
}


And the routine I use to add an element into the list  in the UserSelectorQuestionGroup class is:

public void addUserSelectorQuestionLine(UserSelectorQuestionLine newUserSelectorQuestionLine) {
    int index=0;
    Random gen = new Random();

    if (this.questionLines.size() == 0) {
        this.questionLines.add(newUserSelectorQuestionLine);
    } else {
        for (UserSelectorQuestionLine nextQuestionLine : this.questionLines) {
            if (newUserSelectorQuestionLine.getRanking() < nextQuestionLine.getRanking()) {
                break;
            } else if (newUserSelectorQuestionLine.getRanking().equals(nextQuestionLine.getRanking())) {
                // We want to mix up the order of the question lines that have equal ranking
                if (gen.nextInt(2) == 1) {
                    break;
                }
            }
            index++;
        }
        this.questionLines.add(index, newUserSelectorQuestionLine);
    }
    this.minRanking = questionLines.get(0).getRanking();
}

I know this routine is called and is working correctly because I can display the elements of the list in the log after this is called and everything is as expected in the first EJB transaction context.


Back to the top