Skip to main content



      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
Eclipse UserFriend
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
Eclipse UserFriend
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.
Previous Topic:Set null causes the loading of the relationship
Next Topic:SerializationHelper.desserialize ClassNotFound
Goto Forum:
  


Current Time: Thu Jul 03 12:48:23 EDT 2025

Powered by FUDForum. Page generated in 0.05804 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top