Fay:
Thank you for your response and test. This is an easy workaround.
My understanding is that the JPA OneToOne annotation is intended only for
entities. The language in section 11.1.37 of the JPA 2.0 specification may not
be normative but asserts that @OneToOne is for defining "a single-valued
association to another entity that has one-to-one multiplicity". In contrast,
the @Strategy mapping appears much more general and does not assume that the
Java-side object is an entity. The PointHandler example, for instance, uses
the java.awt.Point object which is not an entity as defined in section 2.1 of
the JPA 2.0 specification (e.g. be annotated by @Entity, have a no-arg
constructor, etc.). As such I am reluctant to endorse this as a long term
solution.
- Jerry
On Mar 8, 2010, at 5:58 PM, Fay Wang [via OpenJPA] wrote:
> Hi,
> As the exception clearly indicates, adding Cascade attribute should fix
> this unmanaged object problem, and the insert should work:
>
> @Strategy("MyPointHandler")
> @OneToOne(cascade=CascadeType.ALL)
> private MyPoint custom;
>
>
>
> Since you did not provide MyPoint.java, the following is the one I used:
>
> @Entity
> public class MyPoint {
> @Id
> String val;
>
> public MyPoint(String val) {
> this.val = val;
> }
>
> public String getValue() {
> return val;
> }
> }
>
> Regards,
> Fay
>
>
> ----- Original Message ----
> From: No1UNo <[hidden email]>
> To: [hidden email]
> Sent: Mon, March 8, 2010 10:20:16 AM
> Subject: Re: Help on basic @Strategy
>
>
> One other detail I should have mentioned. The values are correctly retrieved
> from the database, but the exception is thrown on INSERT before the @Strategy
> class is even invoked.
>
> On Mar 8, 2010, at 12:42 PM, No1UNo [via OpenJPA] wrote:
>
> > I am seeing an 'unmanaged object' exception for the objects build using
> > @Strategy, but isn't that exactly the point? These are supposed to be
> > unmanaged with the strategy being invoked as necessary for conversion. I
> > must be missing something obvious. ;-)
> >
> > > Caused by: <openjpa-2.0.0-beta2-r422266:915978 fatal user error>
> > > org.apache.openjpa.persistence.InvalidStateException: Encountered
> > > unmanaged object in persistent field "com.example.SimpleEntity.custom"
> > > 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.
> > > FailedObject: com.example.mypo...@a431693
> > > at
> > > org.apache.openjpa.kernel.SingleFieldManager.preFlushPC(SingleFieldManager.java:767)
> > >
> >
> > My entity looks something like this:
> >
> > > @Entity
> > > public abstract class SimpleEntity {
> > > @Id
> > > private long id;
> > > public long getId() { return id; }
> > > public void setId(long id) { this.id = id; }
> > >
> > > @Strategy("MyPointHandler")
> > > private MyPoint custom;
> > > MyPoint getCustom() { return custom; }
> > > void setCustom(MyPoint custom) { this. custom = custom; }
> > >
> > > ...
> > > }
> >
> > With a very simple handler class:
> >
> > > public class MyPointHandler extends AbstractValueHandler {
> > > @Override
> > > public Column[] map(ValueMapping vm, String name, ColumnIO io,
> > > boolean adapt) {
> > > Column c = new Column();
> > > c.setIdentifier(DBIdentifier.newColumn(name));
> > > c.setJavaType(JavaTypes.STRING);
> > > return new Column[]{ c };
> > > }
> > >
> > > public boolean isVersionable() {
> > > return true;
> > > }
> > >
> > > public Object toDataStoreValue(ValueMapping vm, Object val, JDBCStore
> > > store) {
> > > return new MyPoint((String) val);
> > > }
> > >
> > > public Object toObjectValue(ValueMapping vm, Object val) {
> > > if (val == null) return null;
> > > return ((MyPoint) val).getValue();
> > > }
> > > }
> >
> > I've used the PointHandler from the OpenJPA tests as a model.
> >
> >
> >
> > View message @
> > http://n2.nabble.com/Help-on-basic-Strategy-tp4696891p4696891.html
> > To start a new topic under OpenJPA Users, email [hidden email]
> > To unsubscribe from OpenJPA Users, click here.
> >
>
>
> --
> View this message in context:
> http://n2.nabble.com/Help-on-basic-Strategy-tp4696891p4697140.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>
>
>
>
>
>
> View message @
> http://n2.nabble.com/Help-on-basic-Strategy-tp4696891p4699019.html
> To start a new topic under OpenJPA Users, email
> [email protected]
> To unsubscribe from OpenJPA Users, click here.
>
--
View this message in context:
http://n2.nabble.com/Help-on-basic-Strategy-tp4696891p4700186.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.