I was getting the following error with openjpa 1.2.1...
<openjpa-1.2.1-r752877:753278 fatal user error>
org.apache.openjpa.persistence.InvalidStateException: Encountered unmanaged
object in persistent field
"org.adamsbros.rmi.ledger.journal.JournalEntry.journalType" during flush.
However, this field does not allow cascade persist. Set the cascade attribute
for this field to CascadeType.PERSIST or CascadeType.ALL (JPA annotations) or
"persist" or "all" (JPA orm.xml), or enable cascade-persist globally, or
manually persist the related field value prior to flushing. You cannot flush
unmanaged objects or graphs that have persistent associations to unmanaged
objects.
JournalEntry.journalType defined as...
@ManyToOne
@JoinColumn(name = "journal_type", referencedColumnName = "journal_type",
nullable = false, table = "journal")
JournalType constructor was like this...
@Id
@Column(name = "journal_type")
private String journalType;
@Column(name = "description")
private String description;
public JournalType()
{
}
// empty as you can see
public JournalType(final String journalType)
{
}
Keep in mind that I do not want cascading to occur with the journalType in
JournalEntry, as I will manage them separately.
As a result, creating a new JournalType would always result in empty instance
variables. I would have expected something like this from the postgresql db...
open_accounting=> INSERT INTO journal_types VALUES(null, null);
ERROR: null value in column "journal_type" violates not-null constraint
But instead I get the obscure error about unmanaged objects.
Not sure if something similar would happen in openjpa 2, as openejb is not yet
configured to use it. But, if it does do the same thing, I would highly
recommend changing something. This took me about 20-30 minutes to see, maybe
longer. I was trying to compare my entities with other entities that were
doing the exact same thing, and couldn't see any problems. It just seemed so
odd. And it was all because my constructor was not doing it's proper
initialization of the instance variables, from the values passed in.
Would you like me to post a bug report?