Changing container policy for toOne relationships [message #659214] |
Fri, 11 March 2011 10:11  |
Eclipse User |
|
|
|
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;
}
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.05804 seconds