MOXy: mapping constant values?

View: New views
4 Messages — Rating Filter:   Alert me  

MOXy: mapping constant values?

by polly.c.chang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

The situation I have is that there are some constant values that I want to appear in my XML output document (like application=Foo).  I don't want this value to be affected by unmarshalling.  How does one map this kind of "constant"?

After playing around with EclipseLink, it looks like even though this value is supposed to be a constant, I need to create a variable for it in my domain object in order to create a mapping to the proper XPath.  But then what should I do?  I don't want this variable's value to change by unmarshalling.  I can write an afterLoad method to reset the variable back to its original value.  That way when I marshall the object, the correct value will be output in the XML.  But is there a better way to do this?  Does EclipseLink support one-way mappings or marshalling constants?

Thanks!
--Polly

Re: MOXy: mapping constant values?

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

There is a ConstantTransformer that can be used with an XMLTransformationMapping that may solve your requirement (you will need to add this transformer through an amendment method if using the MW).  There is also a document preservation feature see, XMLDescriptor.setShouldPreserveDocument().


amphoras wrote:
Hi,

The situation I have is that there are some constant values that I want to appear in my XML output document (like application=Foo).  I don't want this value to be affected by unmarshalling.  How does one map this kind of "constant"?

After playing around with EclipseLink, it looks like even though this value is supposed to be a constant, I need to create a variable for it in my domain object in order to create a mapping to the proper XPath.  But then what should I do?  I don't want this variable's value to change by unmarshalling.  I can write an afterLoad method to reset the variable back to its original value.  That way when I marshall the object, the correct value will be output in the XML.  But is there a better way to do this?  Does EclipseLink support one-way mappings or marshalling constants?

Thanks!
--Polly

Re: MOXy: mapping constant values?

by Matt MacIvor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Polly,

EclipseLink handles read-only mappings, but not really write-only
mappings. The easiest thing to do here would be to set up a mapping for
your constant value using method-access. Then in the getter you can just
return your constant value, and in the setter you can do nothing. For
example:

public String getMyConstant() {
  return "My Constant String";
}

public void setMyConstant() {
  //do nothing
}

When using method access for a mapping it's not required by the runtime
that the variable actually exist on the class, but you may need to add
the instance variable in order to create the mapping in the workbench.

A couple of updates about other issues you were seeing:
The default namespace issue has been fixed in the runtime, so you no
longer need a prefix for each namespace. You can now set a default
namespace on a NamespaceResolver. There's a separate bug that has been
created for Workbench support (236491).

The bug about error handling in JAXBContext (236525) is being looked at now.

Hope this helps,

-Matt

amphoras wrote:

>Hi,
>
>The situation I have is that there are some constant values that I want to
>appear in my XML output document (like application=Foo).  I don't want this
>value to be affected by unmarshalling.  How does one map this kind of
>"constant"?
>
>After playing around with EclipseLink, it looks like even though this value
>is supposed to be a constant, I need to create a variable for it in my
>domain object in order to create a mapping to the proper XPath.  But then
>what should I do?  I don't want this variable's value to change by
>unmarshalling.  I can write an afterLoad method to reset the variable back
>to its original value.  That way when I marshall the object, the correct
>value will be output in the XML.  But is there a better way to do this?
>Does EclipseLink support one-way mappings or marshalling constants?
>
>Thanks!
>--Polly
>  
>
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Re: MOXy: mapping constant values?

by polly.c.chang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi James and Matt,

Thank you for the helpful responses.  I had guessed that I could do what Matt suggested with the empty setter, but I wanted to keep the domain objects as clean as possible.  Since I have four "constants" that I want to write out to the XML file, I did not want to have four getters and four setters that were not used for anything except integrating with EclipseLink.  

I researched the ConstantTransformer that James suggested and found some sample code in the EclipseLink source code.  It would be nice if I can specify this using the Workbench and therefore put it in XML.  As it is, it seems that I can tell the Workbench to create a Transformation mapping using ConstantTransformer, but there's no way for me to set the constant value that I want it to use.  But this is what I ended up using because it at least allows me to stick the glue code in a separate class and set it in the afterLoad method.

Matt:  thanks for the update on the bugs that I reported.  I appreciate all your hard work and quick responses to my questions.  :)

Thanks,
Polly


Matt MacIvor wrote:
Hi Polly,

EclipseLink handles read-only mappings, but not really write-only
mappings. The easiest thing to do here would be to set up a mapping for
your constant value using method-access. Then in the getter you can just
return your constant value, and in the setter you can do nothing. For
example:

public String getMyConstant() {
  return "My Constant String";
}

public void setMyConstant() {
  //do nothing
}

When using method access for a mapping it's not required by the runtime
that the variable actually exist on the class, but you may need to add
the instance variable in order to create the mapping in the workbench.

A couple of updates about other issues you were seeing:
The default namespace issue has been fixed in the runtime, so you no
longer need a prefix for each namespace. You can now set a default
namespace on a NamespaceResolver. There's a separate bug that has been
created for Workbench support (236491).

The bug about error handling in JAXBContext (236525) is being looked at now.

Hope this helps,

-Matt

amphoras wrote:

>Hi,
>
>The situation I have is that there are some constant values that I want to
>appear in my XML output document (like application=Foo).  I don't want this
>value to be affected by unmarshalling.  How does one map this kind of
>"constant"?
>
>After playing around with EclipseLink, it looks like even though this value
>is supposed to be a constant, I need to create a variable for it in my
>domain object in order to create a mapping to the proper XPath.  But then
>what should I do?  I don't want this variable's value to change by
>unmarshalling.  I can write an afterLoad method to reset the variable back
>to its original value.  That way when I marshall the object, the correct
>value will be output in the XML.  But is there a better way to do this?
>Does EclipseLink support one-way mappings or marshalling constants?
>
>Thanks!
>--Polly
>  
>
_______________________________________________
eclipselink-users mailing list
eclipselink-users@eclipse.org
https://dev.eclipse.org/mailman/listinfo/eclipselink-users