Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Support for inner classes?

Since Inner cannot exist as an independent object, it is not possible to map
it as such.  Potentially if it was embeddable you might be able to hack
something together, but I would not recommend it.

I would recommend you make the inner class static or an independent class. 
Another solution is to map the inner class in the outer class.  i.e. use
property access and define get/set methods for each attribute of the inner
class, and have your default constructor assign an instance to the inner
class attribute in the outer.

If you wish to hack something together, you could use a thread local, or
TopLink Session property to store the last Outer that was instantiated.  You
can do this using an instantiation policy on Outer that populates the
property, then use an instantiation policy on Inner to call the Inner class
constructor through reflection passing in the Outer instance.  For the
method-base InstantiationPolicy the method is a static method on the class
that returns an instance and takes no arguments.



Bugzilla from stephan@xxxxxxxxxxxxxxx wrote:
> 
>  
>> James Sutherland wrote:
>> > [...]
>> > 
>> > Perhaps include an example of the exact type of inner class you want to
>> > persist and someone can try it out.
> I'm sure it won't work out-of-the box. Here is the simplest of all
> examples:
> 
> public class Outer {
> 	public class Inner {
> 		String value;
> 	}
> }
> 
> Now when creating an instance of Inner, Java offers two options:
> a) within a non-static method of Outer you may simply use "new Inner()"
> b) outside Outer you need an explicit instance of Outer as in
> 	Outer o = ...
> 	Outer.Inner i = o.new Inner();
> Note, that this distinction is relevant only at the source code level.
> At the bytecode level class Inner looks pretty much like this:
> 	public class Outer$Inner {
> 		final Outer this$0;
> 		public Outer$Inner(Outer this$0) {
> 			this.this$0 = this$0;
> 		}
> 	}
> Thus instantiation is translated into
> a)	new Inner(this); // within a non-static context of Outer
> b)	new Inner(o);    // outside Outer with an explicit Outer instance
> 
> As for reading an Inner from a database this means, we need to retrieve 
> the enclosing instance (this$0) first, before we can create the Inner.
> If this$0 is stored as a foreign key, the instantiation method would need
> to fetch and resolve this key before creating the Inner instance.
> That's why I ask about context above: is the current db-row already
> available
> when the instantiation method is invoked?
> 
> thanks,
> Stephan
> 
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/Support-for-inner-classes--tp16703848p16743990.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top