Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Changing container policy for toOne relationships
Changing container policy for toOne relationships [message #659214] Fri, 11 March 2011 10:11 Go to next message
Mauro Flores is currently offline Mauro Flores
Messages: 84
Registered: September 2009
Location: Brasil
Member
How can I change the serialization behavior of a toOne relationship in a SessionCustomizer implementation ?

The following two classes change the behavior of the serialization just for a toMany lazy relationships. If the relationship is not instantiated, the new container policy returns null.
How can I do the same for a toOne relationship?
I get an exception when the method colMapping.getContainerPolicy()is called for this relationships.

public class CustomSerializeSession implements SessionCustomizer  {
  
    public void customize(Session session) throws Exception {
      for (Iterator<?> dI = session.getProject().getAliasDescriptors()
          .values().iterator(); dI.hasNext();) {
        ClassDescriptor descriptor = (ClassDescriptor) dI.next();
        for (Iterator<?> mI = descriptor.getMappings().iterator(); mI
            .hasNext();) {
          DatabaseMapping mapping = (DatabaseMapping) mI.next();
          // only for relationships set to lazy
          if (mapping.isForeignReferenceMapping()
              && mapping.isCollectionMapping()
              && mapping.isLazy()) {
            CollectionMapping colMapping = (CollectionMapping) mapping;
            if (colMapping.getContainerPolicy().getContainerClass() == ClassConstants.IndirectList_Class) {
              
              ListContainerPolicy policy = new ListContainerPolicy(
                  CustomSerializeIndirectList.class);
              colMapping.setContainerPolicy(policy);
            } 
          }
        }
      }
    }
}

public class CustomSerializeIndirectList extends IndirectList {

	public CustomSerializeIndirectList() {
		super();
	}

	public CustomSerializeIndirectList(int size) {
		super(size);
	}

	public Object writeReplace() throws ObjectStreamException {
		if (!isInstantiated()) {
			return null;
		}
		return this;
	}
}
Re: Changing container policy for toOne relationships [message #660822 is a reply to message #659214] Mon, 21 March 2011 14:54 Go to previous message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You would need to set the IndirectionPolicy of the mapping to a custom IndirectionPolicy that used some custom type of ValueHolder.

What are you trying to accomplish? You could also overwrite the serialization of your Entity.


James : Wiki : Book : Blog : Twitter
Previous Topic:Set null causes the loading of the relationship
Next Topic:SerializationHelper.desserialize ClassNotFound
Goto Forum:
  


Current Time: Tue Jul 22 13:28:39 EDT 2014

Powered by FUDForum. Page generated in 0.01626 seconds