Hibernate Mapping with Generics and MappedSuperclass

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Hibernate Mapping with Generics and MappedSuperclass

Alexander Buchholtz
Hi,

I'm facing an issue with serializing our entities to XML. We have a generic
that is used for localization of our entities.

My data model looks as follows:

@MappedSuperclass
public class LocalizationName<LocalizationNameObject> extends
        private int id;
        protected LocalizationNameObject localizationObject;
        protected String locale;
        protected String localizedName;
}

@MappedSuperclass
public abstract class LocalizableName<LocalizableClass extends
LocalizationName> {
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "localizationObject")
        protected List<LocatableClass> localizationNames = new
ArrayList<LocatableClass>();
}

@Entity
public class WorkflowRole extends LocalizableName<WorkflowRoleNameLocalization>{
}

@Entity
public class WorkflowRoleNameLocalization extends
LocalizationName<WorkflowRole> {
}

If I serialize a WorkflowRole and try to
   1. set an alias for and
   2. omit the "id" field of LocalizationName
by setting:

   xstream.omitField(WorkflowRoleNameLocalization.class, "id");
   // doesn't work either
   xstream.omitField(LocalizationName.class, "id");
   
   xstream.alias("wfRoleLoc", WorkflowRoleNameLocalization.class);

these are ignored. I get the following XML:

 <workflowRole>
      <localizationNames>
        <com.acme.workflow.WorkflowRoleNameLocalization>
          <id>27</id>
          <localizationObject class="com.acme.workflow.WorkflowRole"
reference="../../.."/>
          <locale>de</locale>
          <localizedName>Role 1</localizedName>
        </com.acme.workflow.WorkflowRoleNameLocalization>
      </localizationNames>
    </workflowRole>

Do you have any suggestions how I can work around this or what I'm doing wrong?
I need to remove at least the ID's as I want to re-persist them in a
different database.

Thanks,
Alexander


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Hibernate Mapping with Generics and MappedSuperclass

Jörg Schaible-2
Hi Alexander,

Alexander Buchholtz wrote:

> Hi,
>
> I'm facing an issue with serializing our entities to XML. We have a
> generic that is used for localization of our entities.
>
> My data model looks as follows:
>
> @MappedSuperclass
> public class LocalizationName<LocalizationNameObject> extends
> private int id;
>         protected LocalizationNameObject localizationObject;
> protected String locale;
> protected String localizedName;
> }
>
> @MappedSuperclass
> public abstract class LocalizableName<LocalizableClass extends
> LocalizationName> {
>         @OneToMany(cascade = CascadeType.ALL, mappedBy =
>         "localizationObject")
> protected List<LocatableClass> localizationNames = new
> ArrayList<LocatableClass>();
> }
>
> @Entity
> public class WorkflowRole extends
> LocalizableName<WorkflowRoleNameLocalization>{ }
>
> @Entity
> public class WorkflowRoleNameLocalization extends
> LocalizationName<WorkflowRole> {
> }
>
> If I serialize a WorkflowRole and try to
>    1. set an alias for and
>    2. omit the "id" field of LocalizationName
> by setting:
>
>    xstream.omitField(WorkflowRoleNameLocalization.class, "id");
>    // doesn't work either


This is what normally works:


>    xstream.omitField(LocalizationName.class, "id");


The same for the alias:
   

>    xstream.alias("wfRoleLoc", WorkflowRoleNameLocalization.class);
>
> these are ignored. I get the following XML:
>
>  <workflowRole>
>       <localizationNames>
>         <com.acme.workflow.WorkflowRoleNameLocalization>
>           <id>27</id>
>           <localizationObject class="com.acme.workflow.WorkflowRole"
> reference="../../.."/>
>           <locale>de</locale>
>           <localizedName>Role 1</localizedName>
>         </com.acme.workflow.WorkflowRoleNameLocalization>
>       </localizationNames>
>     </workflowRole>
>
> Do you have any suggestions how I can work around this or what I'm doing
> wrong? I need to remove at least the ID's as I want to re-persist them in
> a different database.


Actually I have no experience with the Hibernate stuff at all, therefore I
don't know, if it really can make a difference. However, I'd be wondering.

Can you create a version of the classes above without the Hibernate stuff,
create an instance and marshal that to XML?

How do you initialize the XStream instance?

- Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Hibernate Mapping with Generics and MappedSuperclass

Alexander Buchholtz
Hi Jörg,

Jörg Schaible <joerg.schaible@...> writes:

>
> Actually I have no experience with the Hibernate stuff at all, therefore I
> don't know, if it really can make a difference. However, I'd be wondering.
>
> Can you create a version of the classes above without the Hibernate stuff,
> create an instance and marshal that to XML?
>
> How do you initialize the XStream instance?
>
> - Jörg
>

I'll double check without the Hibernate mappings on Monday. But one of the
benefits of the Hibernate extension is automatic loading of lazy collections
and attributes. I would rather not miss this, as the above code is just a
small portion of the data model.

I pretty much initialize the xstream following the tutorial:

final XStream xstream = new XStream(new StaxDriver()) {

                        @Override
                        protected MapperWrapper wrapMapper(final MapperWrapper next) {
                                return new HibernateMapper(next);
                        }
                };

                Mapper mapper = xstream.getMapper();
                xstream.registerConverter(new HibernateProxyConverter());
                xstream.registerConverter(new HibernatePersistentCollectionConverter(mapper));
                xstream.registerConverter(new HibernatePersistentMapConverter(mapper));
                xstream.registerConverter(new HibernatePersistentSortedMapConverter(mapper));
                xstream.registerConverter(new HibernatePersistentSortedSetConverter(mapper));

String xml = xstream.toXML(workflowRole);

I would appreciate if you or anyone else has some other ideas.

Thanks,
Alexander



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Hibernate Mapping with Generics and MappedSuperclass

Jörg Schaible-3
Hi Alexander,

Alexander Buchholtz wrote:

> Hi Jörg,
>
> Jörg Schaible <joerg.schaible@...> writes:
>
>>
>> Actually I have no experience with the Hibernate stuff at all, therefore
>> I don't know, if it really can make a difference. However, I'd be
>> wondering.
>>
>> Can you create a version of the classes above without the Hibernate
>> stuff, create an instance and marshal that to XML?
>>
>> How do you initialize the XStream instance?
>>
>> - Jörg
>>
>
> I'll double check without the Hibernate mappings on Monday. But one of the
> benefits of the Hibernate extension is automatic loading of lazy
> collections and attributes. I would rather not miss this, as the above
> code is just a small portion of the data model.

Somehow we have to identify what causes the problem with the ignored
settings.

> I pretty much initialize the xstream following the tutorial:
>
> final XStream xstream = new XStream(new StaxDriver()) {
>
> @Override
> protected MapperWrapper wrapMapper(final MapperWrapper next) {
> return new HibernateMapper(next);
> }
> };
>
> Mapper mapper = xstream.getMapper();
> xstream.registerConverter(new HibernateProxyConverter());
> xstream.registerConverter(new
> HibernatePersistentCollectionConverter(mapper));
> xstream.registerConverter(new HibernatePersistentMapConverter(mapper));
> xstream.registerConverter(new
> HibernatePersistentSortedMapConverter(mapper));
> xstream.registerConverter(new
> HibernatePersistentSortedSetConverter(mapper));
>
> String xml = xstream.toXML(workflowRole);

And where are here now the calls to XStream.omitField or XStream.alias?

> I would appreciate if you or anyone else has some other ideas.

As said, although I have no experience with Hibernate, I cannot real imagine
that the problem is related to Hibernate.

Cheers,
Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Hibernate Mapping with Generics and MappedSuperclass

Alexander Buchholtz
Hi Jörg,


Jörg Schaible <Joerg.Schaible@...> writes:

>
> Hi Alexander,
>
> Alexander Buchholtz wrote:
>
> > Hi Jörg,
> >
> > Jörg Schaible <joerg.schaible <at> ...> writes:
> >
> >>
> >> Actually I have no experience with the Hibernate stuff at all, therefore
> >> I don't know, if it really can make a difference. However, I'd be
> >> wondering.
> >>
> >> Can you create a version of the classes above without the Hibernate
> >> stuff, create an instance and marshal that to XML?
> >>
> >> How do you initialize the XStream instance?
> >>
> >> - Jörg
> >>
> >
> > I'll double check without the Hibernate mappings on Monday. But one of the
> > benefits of the Hibernate extension is automatic loading of lazy
> > collections and attributes. I would rather not miss this, as the above
> > code is just a small portion of the data model.
>
> Somehow we have to identify what causes the problem with the ignored
> settings.
>
> > I pretty much initialize the xstream following the tutorial:
> >
> > final XStream xstream = new XStream(new StaxDriver()) {
> >
> >  <at> Override
> > protected MapperWrapper wrapMapper(final MapperWrapper next) {
> > return new HibernateMapper(next);
> > }
> > };
> >
> > Mapper mapper = xstream.getMapper();
> > xstream.registerConverter(new HibernateProxyConverter());
> > xstream.registerConverter(new
> > HibernatePersistentCollectionConverter(mapper));
> > xstream.registerConverter(new HibernatePersistentMapConverter(mapper));
> > xstream.registerConverter(new
> > HibernatePersistentSortedMapConverter(mapper));
> > xstream.registerConverter(new
> > HibernatePersistentSortedSetConverter(mapper));
> >
> > String xml = xstream.toXML(workflowRole);
>
> And where are here now the calls to XStream.omitField or XStream.alias?

I'm currently trying to isolate the issue for you to reproduce in a small
test project.

In my last code snippet I forgot to add the call to omit the Ids
accidentally (as I have to remove allot of stuff to reduce complexity of the
code). So here it comes:

        private static final Class[] CLASSES_TO_EXCLUDE_ID_FIELD = new Class[] {
                        WorkflowRole.class,
                        WorkflowRoleNameLocalization.class,
                        LocalizationName.class
        };

protected void omitIDFields(final XStream xstream) {
                for (Class clazz : CLASSES_TO_EXCLUDE_ID_FIELD) {
                        xstream.omitField(clazz, "id");
                }
        }

and the call to omitIDFields right before toXML call:

...
omitIDFields(xstream);
String xml = xstream.toXML(workflowRole);
...



>
> > I would appreciate if you or anyone else has some other ideas.
>
> As said, although I have no experience with Hibernate, I cannot real imagine
> that the problem is related to Hibernate.
>
> Cheers,
> Jörg
>


I'll send an update once I've isolated a test case, so far no success to
reproduce in my test project.

Thanks,
Alexander




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Hibernate Mapping with Generics and MappedSuperclass

Alexander Buchholtz
Hi Jörg,


Alexander Buchholtz <alexander.buchholtz@...> writes:

>

> >
> > And where are here now the calls to XStream.omitField or XStream.alias?
>
> I'm currently trying to isolate the issue for you to reproduce in a small
> test project.
>
> In my last code snippet I forgot to add the call to omit the Ids
> accidentally (as I have to remove allot of stuff to reduce complexity of the
> code). So here it comes:
>
> private static final Class[] CLASSES_TO_EXCLUDE_ID_FIELD = new Class[] {
> WorkflowRole.class,
> WorkflowRoleNameLocalization.class,
> LocalizationName.class
> };
>
> protected void omitIDFields(final XStream xstream) {
> for (Class clazz : CLASSES_TO_EXCLUDE_ID_FIELD) {
> xstream.omitField(clazz, "id");
> }
> }
>
> and the call to omitIDFields right before toXML call:
>
> ...
> omitIDFields(xstream);
> String xml = xstream.toXML(workflowRole);
> ...
>
> >
> > > I would appreciate if you or anyone else has some other ideas.
> >
> > As said, although I have no experience with Hibernate, I cannot real
imagine

> > that the problem is related to Hibernate.
> >
> > Cheers,
> > Jörg
> >
>
> I'll send an update once I've isolated a test case, so far no success to
> reproduce in my test project.
>
> Thanks,
> Alexander
>

I just made another round trip when writing a new test and it seems the
culprit must have been somewhere in my code, as I've already fixed the issue
by coincident probably a few days ago. The id fields are omitted properly
now and I can set aliases for the generics as well.
Pretty weird as I didn't change anything serious but just did some
refactoring to restructure code and write unit tests. The methods to omit
fields wasn't even touched at all.

So thank you for your help, I really appreciate it.

Thanks,
Alexander



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Hibernate Mapping with Generics and MappedSuperclass

Jörg Schaible-2
Hi Alexander,

Alexander Buchholtz wrote:

[snip]

>> I'll send an update once I've isolated a test case, so far no success to
>> reproduce in my test project.
>>
>> Thanks,
>> Alexander
>>
>
> I just made another round trip when writing a new test and it seems the
> culprit must have been somewhere in my code, as I've already fixed the
> issue by coincident probably a few days ago. The id fields are omitted
> properly now and I can set aliases for the generics as well.
> Pretty weird as I didn't change anything serious but just did some
> refactoring to restructure code and write unit tests. The methods to omit
> fields wasn't even touched at all.

Well, good :)

Basically you may not change the setup anymore one you used the instance for
marshalling or unmarshalling, because of the internal caches.

> So thank you for your help, I really appreciate it.

You're welcome.

- Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email