Here they are, simplified. I apologize for the length of this email.
X is linked to Y by an XY instance; XY has a composite key called an XY.ID.
I challenge someone to put a Y into X's bindings under the type "foo" with:
(a) no flush invocations (as though in a JTA world)
(b) a minimum of merge() or persist() invocations.
X:
package blox.party.jpa;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.*;
@Entity
public class X implements Serializable {
@Id
@GeneratedValue
private int id;
public X() {
super();
}
@OneToMany(mappedBy = "owner", cascade = CascadeType.ALL)
@MapKey(name = "id")
private Map<XY.ID, XY> bindings;
public int getID() {
return this.id;
}
public void put(final String type, final Y y) {
if (type != null && y != null) {
if (this.bindings == null) {
this.bindings = new HashMap<XY.ID, XY>();
}
final XY.ID id = new XY.ID(this, type);
final XY xy = new XY(id, y);
this.bindings.put(id, xy);
}
}
}
Y:
package blox.party.jpa;
import java.io.Serializable;
import javax.persistence.*;
@Entity
public class Y implements Serializable {
@Id
@GeneratedValue
private int id;
@ManyToOne
@JoinColumn(name = "xid", referencedColumnName = "id")
private X owner;
public Y() {
}
public Y(final X owner) {
super();
this.owner = owner;
}
public X getOwner() {
return this.owner;
}
}
And XY:
package blox.party.jpa;
import java.io.Serializable;
import javax.persistence.*;
@Entity
public class XY implements Serializable {
@EmbeddedId
private ID id;
@ManyToOne
@JoinColumn(name = "xid", referencedColumnName = "id", insertable = false,
updatable = false)
private X owner;
@ManyToOne
@JoinColumn(name = "yid", referencedColumnName = "id")
private Y y;
public XY() {
super();
}
public XY(final ID id, final Y y) {
super();
this.id = id;
if (id != null) {
this.owner = id.getOwner();
}
this.y = y;
}
public Y getY() {
return this.y;
}
@Embeddable
public static class ID implements Serializable {
@Column(name = "xid")
private int ownerID;
@Transient
private X owner;
private String type;
public ID() {
super();
}
public ID(final X owner, final String type) {
super();
this.ownerID = owner == null ? -1 : owner.getID();
this.owner = owner;
this.type = type;
}
public X getOwner() {
return this.owner;
}
@Override
public int hashCode() {
int hashCode = this.ownerID;
if (this.type != null) {
hashCode += this.type.hashCode();
}
return hashCode;
}
@Override
public boolean equals(final Object other) {
if (other == this) {
return true;
} else if (other instanceof ID) {
final ID him = (ID)other;
if (him.ownerID != this.ownerID) {
return false;
} else if (this.type == null) {
return him.type == null;
} else {
return this.type.equals(him.type);
}
} else {
return false;
}
}
}
}
Thanks for your help.
Best,
Laird
On Thu, Oct 1, 2009 at 12:27 PM, crispyoz (via Nabble) <
[email protected]<ml-user%[email protected]>
> wrote:
> Maybe just send a copy of your class definition, just the part where you
> define your variables, join, cascades etc. might give a clue.
>
> -----Original Message-----
> From: ljnelson [mailto:[hidden
> email]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3750314&i=0>]
>
> Sent: Friday, 2 October 2009 2:15 AM
> To: [hidden
> email]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3750314&i=1>
> Subject: Re: Cascade and persist issues
>
>
> Thanks, but I'm using emacs (gasp) on the command line. :-(
>
> I'll see if I can reproduce this thing in a sane, trimmed down manner and
> send the files this way.
>
> L
>
> On Thu, Oct 1, 2009 at 11:56 AM, crispyoz (via Nabble) <
> [hidden
> email]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3750314&i=2>
> <[hidden
> email]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3750314&i=3>.
>
> com>
> > wrote:
>
> > A lot of those error messages don't make any sense to me either. Try
> > cleaning your project, if I change the cascade types on fields Eclipse
> > doesn't seem to pick it up. Doing a clean then rebuild fixes the issue.
> >
> > Chris
> >
> >
> > -----Original Message-----
> > From: ljnelson [mailto:[hidden
> email]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3750077&i=0>]
>
> >
> > Sent: Friday, 2 October 2009 1:45 AM
> > To: [hidden
> email]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3750077&i=1>
>
> > Subject: Re: Cascade and persist issues
> >
> >
> > Yes, I guess my point is: I am cascading the relationship with a
> > CascadeType
> > of ALL, but OpenJPA is telling me that I need to mark it as
> > CascadeType.ALL,
> > which...it is.
> >
> > On Thu, Oct 1, 2009 at 11:01 AM, crispyoz (via Nabble) <
> > [hidden
> email]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3750077&i=2>
>
> > <[hidden
> email]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3750077&i=3>.
>
>
> >
> > com>
> > > wrote:
> >
> > > I do something similar, I have a user and a user has a single address
> so
> > I
> > > use like this:
> > >
> > > User:
> > >
> > > Private String name;
> > > @OneToOne cascade={CascadeType.ALL})
> > > Private Address address;
> > > ....
> > > ...
> > >
> > > I construct the user, construct the address, link the address to the
> user
> >
> > > and just use persist on the user which causes the address to be
> persisted
> >
> > > due to the cascade type. I do the same thing with users that may share
> > the
> > > same address, just the relationship is changed to
> > >
> > > @ManyToOne cascade={CascadeType.ALL})
> > >
> > > Chris
> > >
> > > -----Original Message-----
> > > From: Laird Nelson [mailto:[hidden
> >
> email]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3749649&i=0>]
>
> >
> > >
> > > Sent: Friday, 2 October 2009 12:33 AM
> > > To: [hidden
> > email]<
> http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3749649&i=1>
> >
> > > Subject: Cascade and persist issues
> > >
> > > On a field marked @OneToMany(/*...*/ cascade = CascadeType.ALL), I am
> > > getting this error at persist() time:
> > >
> > > <openjpa-1.3.0-SNAPSHOT-r422266:820051 nonfatal user error>
> > > org.apache.openjpa.persistence.ArgumentException: Encountered new
> object
> > in
> > >
> > > persistent field "blox.party.jpa.PartyEntity.postalAddresses<key:class
> > > blox.party.jpa.PartyPostalAddressBinding$ID>" during attach. However,
> > this
> > >
> > > field does not allow cascade attach. Set the cascade attribute for this
>
> > > field to CascadeType.MERGE or CascadeType.ALL (JPA annotations) or
> > "merge"
> > > or "all" (JPA orm.xml). You cannot attach a reference to a new object
> > > without cascading.
> > >
> > > I am following the advice of
> http://www.screaming-penguin.com/node/7513
> in
> >
> > >
> > > terms of structure.
> > >
> > > To be specific, I have a Party identified with an autogenerated int PK
> > with
> > >
> > > a @OneToMany relationship with a PartyPostalAddressBinding, which has a
>
> > > @ManyToOne relationship with a PostalAddress. A PostalAddress, in
> turn,
> > is
> > >
> > > owned by exactly one Party.
> > >
> > > In my test case, I persist the Party (then I have to flush(), which is
> > > tremendously irritating) yielding, let's say, party1. Then I create a
> > new
> > > PostalAddress and assign party1 as its owner. I persist that (and
> again
> > > have to flush() in order to avoid errors--no idea why), yielding
> > > postalAddress1. Finally, I insert a new binding that links party1 and
> > > postalAddress1 into party1's Map of bindings. I then merge() party1
> > (which
> > >
> > > I don't think I should have to do). It is at this point that OpenJPA
> > tells
> >
> > >
> > > me that my @OneToMany field, with cascade markings all over it, needs
> to
> > > be,
> > > um, marked with CascadeType.ALL, which it is.
> > >
> > > I am quite happy to attach all of this, but wanted to see if I'm doing
> > > something obviously stupid first.
> > >
> > > Thanks,
> > > Laird
> > >
> > >
> > >
> > > ------------------------------
> > > View message @
> > > http://n2.nabble.com/Cascade-and-persist-issues-tp3749443p3749649.html
> > > To start a new topic under OpenJPA Users, email
> > >
> > [hidden
> email]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3750077&i=4>
>
> > <[hidden
> email]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3750077&i=5>.
>
> >
> > com>
> > > To unsubscribe from OpenJPA Users, click here< (link removed) =>.
> > >
> > >
> > >
> >
> > --
> > View this message in context:
> > http://n2.nabble.com/Cascade-and-persist-issues-tp3749443p3750003.html
> > Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >
> >
> >
> > ------------------------------
> > View message @
> > http://n2.nabble.com/Cascade-and-persist-issues-tp3749443p3750077.html
> > To start a new topic under OpenJPA Users, email
> >
> [hidden
> email]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3750314&i=4>
> <[hidden
> email]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3750314&i=5>.
>
> com>
> > To unsubscribe from OpenJPA Users, click here< (link removed) =>.
> >
> >
> >
>
> --
> View this message in context:
> http://n2.nabble.com/Cascade-and-persist-issues-tp3749443p3750197.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>
>
> ------------------------------
> View message @
> http://n2.nabble.com/Cascade-and-persist-issues-tp3749443p3750314.html
> To start a new topic under OpenJPA Users, email
> [email protected]<ml-node%[email protected]>
> To unsubscribe from OpenJPA Users, click here< (link removed) =>.
>
>
>
--
View this message in context:
http://n2.nabble.com/Cascade-and-persist-issues-tp3749443p3750385.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.