Return-Path: Delivered-To: apmail-openjpa-users-archive@minotaur.apache.org Received: (qmail 29704 invoked from network); 9 Mar 2010 17:31:51 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 9 Mar 2010 17:31:51 -0000 Received: (qmail 96658 invoked by uid 500); 9 Mar 2010 17:31:24 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 96573 invoked by uid 500); 9 Mar 2010 17:31:24 -0000 Mailing-List: contact users-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@openjpa.apache.org Delivered-To: mailing list users@openjpa.apache.org Received: (qmail 96564 invoked by uid 99); 9 Mar 2010 17:31:24 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 09 Mar 2010 17:31:24 +0000 X-ASF-Spam-Status: No, hits=2.2 required=10.0 tests=FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: local policy) Received: from [216.252.110.62] (HELO web55901.mail.re3.yahoo.com) (216.252.110.62) by apache.org (qpsmtpd/0.29) with SMTP; Tue, 09 Mar 2010 17:31:22 +0000 Received: (qmail 53079 invoked by uid 60001); 9 Mar 2010 17:31:01 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1268155861; bh=8e2lfgfIWAIMNxi0G5O0TPnfxS08nkbXL9fQ2Q4dygg=; h=Message-ID:X-YMail-OSG:Received:X-Mailer:References:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type; b=MW2Cx5sROjigtsT8gAxjPC8llGWbisMt86U/ru24LuOhD3c+wEoexzKQbvedGFPkM7tNhPCi45xoqXKv7sLHA60uUmykOVk+p4JwW8g8lA9qhgOxPAKufFL/o5onlxKAHQASa+Vst5zFQijbkgQvu0q6intmIP49oakHvIZXIwE= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:X-YMail-OSG:Received:X-Mailer:References:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type; b=YXvdvnNQnqG5A/1a3AMHTbV+UrFfDsujLpwZCizH7jQarwmFhRHqM+m5r+j/+zLVyWrzRojFPNC3MprVwrdJzPtvHmCyMnU7i8M6uyRckAWpXk98EtAA+EtJfJSGHeZF7UGSlNIezxh91G5bw0QFDAieYREBv3FAYVJRaS8kRr0=; Message-ID: <87882.51933.qm@web55901.mail.re3.yahoo.com> X-YMail-OSG: lo1LEwoVM1mzFRPvirxX9Nx301enXZM89BS32EvQPAd26Mg jDkDxXl2L1jKzEEvSmZy1.lB3Acjfx.D3nXYBAy2_Xh9xV70OPUC2Y1FGtCD 1u3H3LPip4bYVrP3L_YRs3AVY2w8sAWciBw2CNZxxtxwAXv1LpyPWUhSvybP witlNjgcYuTXrRqiSUO3PH3zHYIZ2BuzEzDvB4Pe9.PkMqFZ..1a4DAXIYl6 gdDjxD8.UxOEjAGlThfuXT_wh4brW0OT5AyWs1bCwhbDUJsNbV4faXyr3KtK Z5Kh76mc6UJYyLQ-- Received: from [32.97.110.64] by web55901.mail.re3.yahoo.com via HTTP; Tue, 09 Mar 2010 09:31:00 PST X-Mailer: YahooMailRC/300.3 YahooMailWebService/0.8.100.260964 References: <814F5F5D-C15F-4393-A346-D2D1234EB0F0@jerrycarter.org> <8FA53175-E3D4-4A0C-B04F-10AF5AF57E28@jerrycarter.org> Date: Tue, 9 Mar 2010 09:31:00 -0800 (PST) From: Fay Wang Subject: Re: Help on basic @Strategy To: users@openjpa.apache.org In-Reply-To: <8FA53175-E3D4-4A0C-B04F-10AF5AF57E28@jerrycarter.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Hi Jerry, Ok, I take out the OneToOne annotation, and employ inheritance as shown below. The test works just fine. I don't see unmanaged object exception. (1) @Entity public class SimpleEntity { @Id private long id; public long getId() { return id; } public void setId(long id) { this.id = id; } @Strategy("MyPointHandler") @Persistent(fetch = FetchType.LAZY) private MyPoint custom; public MyPoint getCustom() { return custom; } public void setCustom(MyPoint custom) { this. custom = custom; } } (2) public class MyPoint { String val; public MyPoint(String val) { this.val = val; } public String getValue() { return val; } } (3) public class SpecializedPoint extends MyPoint { int intVal; public SpecializedPoint(String val) { super(val); } public SpecializedPoint(String val, int intVal) { super(val); this.intVal = intVal; } public int getIntVal() { return intVal; } public void setIntVal(int intVal) { this.intVal = intVal; } } (4) public class MyPointHandler extends AbstractValueHandler { public Column[] map(ValueMapping vm, DBIdentifier name, ColumnIO io, boolean adapt) { String dbIdName = name.getName(); Column c1 = new Column(); c1.setIdentifier(DBIdentifier.newColumn(dbIdName + "_val")); c1.setJavaType(JavaTypes.STRING); Column c2 = new Column(); c2.setIdentifier(DBIdentifier.newColumn(dbIdName + "_intVal")); c2.setJavaType(JavaTypes.INT_OBJ); return new Column[]{ c1, c2 }; } public Column[] map(ValueMapping vm, String name, ColumnIO io, boolean adapt) { Column c1 = new Column(); c1.setIdentifier(DBIdentifier.newColumn(name + "_val")); c1.setJavaType(JavaTypes.STRING); Column c2 = new Column(); c2.setIdentifier(DBIdentifier.newColumn(name + "_intVal")); c2.setJavaType(JavaTypes.INT_OBJ); return new Column[]{ c1, c2 }; } public boolean isVersionable() { return true; } public Object toDataStoreValue(ValueMapping vm, Object val, JDBCStore store) { return new Object[] {((SpecializedPoint)val).getValue(), ((SpecializedPoint)val).getIntVal()}; } public Object toObjectValue(ValueMapping vm, Object val) { Object[] objVal = (Object[]) val; return new SpecializedPoint((String)objVal[0], (Integer)objVal[1]); } } (5) Here is my test program: public void testStrat() { EntityManager em = emf.createEntityManager(); SimpleEntity e = new SimpleEntity(); e.setId(1); SpecializedPoint custom = new SpecializedPoint("myPoint", 1); e.setCustom(custom); em.persist(e); em.getTransaction().begin(); em.getTransaction().commit(); em.clear(); SimpleEntity e1 = em.find(SimpleEntity.class, 1); System.out.println("e1 = " + e1); } Fay ----- Original Message ---- From: No1UNo To: users@openjpa.apache.org Sent: Mon, March 8, 2010 8:08:55 PM Subject: Re: Help on basic @Strategy On Mar 8, 2010, at 4:03 PM, No1UNo [via OpenJPA] wrote: > The 'unmanaged object' exception is being caused by an inheritance relationship on the Java side. Suppose that I have a specialized class derive from a base class: 'class SpecializedPoint extends MyPoint'. If I write the entity as > >> >> @Strategy("MyPointHandler") >> private SpecializedPoint custom; >> SpecializedPoint getCustom() { return custom; } >> void setCustom(SpecializedPoint custom) { this. custom = custom; } > > everything works fine. If instead I have > >> >> @Strategy("MyPointHandler") >> private MyPoint custom; >> MyPoint getCustom() { return custom; } >> void setCustom(MyPoint custom) { this. custom = custom; } > > and call 'setCustom(new SpecializedPoint())' then the exception is thrown when the data should be written to the database and BEFORE ever calling the class designated by @Strategy. > > Is this a bug in OpenJPA 2.0.0? Most likely the behavior is by design. This prevents scenarios where the child class requires persisting more information than the base class. Unfortunately, it doesn't help much in my case as the objects are serializing binary data to the database and only a single column is required for the whole family of classes. Is there a different mechanism for this scenario? FWIW, I've employed a slightly different workaround. I defined a simple class holding the base class and made corresponding changes to the setter, getter, and strategy class. > @Strategy("MyPointHandler") > private HolderClass custom; > MyPoint GetCustom() { return custom.getPoint(); } > void setCustom(MyPoint custom) { custom = new HolderClass(custom); } This seems unnecessary but it works. -- View this message in context: http://n2.nabble.com/Help-on-basic-Strategy-tp4696891p4700217.html Sent from the OpenJPA Users mailing list archive at Nabble.com.