I have following use-case for marshalling a POJO to XML using Eclipselink MOXy 2.3:
public abstract class A {
public abstract getX();
}
public class B extends A {
private Foo x;
#Override
public Foo getX() {
return this.x;
}
}
public class C extends B {
// Various fields and properties here
}
I need to marshal B and C but not A.
So i set A to be transient which makes B inherit all its members that will be marshalled when marshalling B.
I cant set B to be transient since i need to marshal it by itself, but when i marshal C, i need property B.getX() to be marshalled as well.
Is there any way other than #Override getX() in C to have it marshalled? At the moment it is just one property for which i need to do this, but imagine a large B class with many members, which one would need to #Override in C to marshal them together with C.
Is there any annotation or possibility in the external mapping file to mark a property in a superclass to be inherited by its immediate subclass (or all subclasses)?
What is the Eclipselink/JAXB way to go here?
Regards,
There is nothing special you need to do:
B
I have modified the B class based on one of your previous questions in order to populate the x property:
package forum8739246;
public class B extends A {
private Foo x;
public B() {
x = new Foo();
}
public Foo getX() {
return this.x;
}
}
oxm.xml
Below is the metadata file that I based on your comments to my original answer.
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
version="2.3"
package-name="forum8739246">
<java-types>
<java-type name="B" xml-accessor-type="FIELD">
<java-attributes>
<xml-element java-attribute="x" name="X"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
Demo
package forum8739246;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.*;
import javax.xml.namespace.QName;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum8739246/oxm.xml");
JAXBContext jc = JAXBContext.newInstance(new Class[] {C.class},properties);
System.out.println(jc.getClass());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JAXBElement<B> b = new JAXBElement<B>(new QName("b"), B.class, new B());
marshaller.marshal(b, System.out);
JAXBElement<C> c = new JAXBElement<C>(new QName("c"), C.class, new C());
marshaller.marshal(c, System.out);
}
}
Output
As can be seen from the output the x property is marshalled for both instances of B and C:
class org.eclipse.persistence.jaxb.JAXBContext
<?xml version="1.0" encoding="UTF-8"?>
<b>
<X/>
</b>
<?xml version="1.0" encoding="UTF-8"?>
<c>
<X/>
</c>
Related
I'm new to OSGI framework and I'm trying to access the 'Derived' Class variable 'publicVariable' from another class 'Derived2' like "Derived.publicVariable" but publicVariable is always shows null. I really appreciate if someone can help me out with this.
Thanks
Manifest file - Derived2
Require-Bundle:com.xxxxxx.Derived1
Java code
abstract class Base {
protected Vector <String> supportedCommands = new Vector <String> ();
protected abstract void initialiseCommands();
}
class Derived extends Base {
private static Derived derivedPlugin = null;
public Derived()
{
derivedPlugin = this;
}
public static Derived getPlugin()
{
return derivedPlugin;
}
public String publicVariable = null;
protected void initialiseCommands()
{
publicVariable = "someData";
System.out.println("Derived" + publicVariable);
}
}
class Derived2 extends Base {
protected void initialiseCommands()
{
supportedCommands.add(Derived.getPlugin().publicVariable);
System.out.println("IMRSAUtilitiesPlugin" +supportedCommands);
}
Also referred below link, which is a similar issue but i'm not using any static variable, it is just a public variable.
how use Singleton object in different class loader....?
The code in the question will not compile. You are trying to access an instance field (publicVariable in class Derived) in a static way, i.e. Derived.publicVariable.
OSGi does not change the semantics of the Java language, and if you cannot even compile your code then OSGi will certainly not be able to run it.
I am using spring boot (2.0.0) with eclipse link to persist data (over 500 entity classes) to a postgres db (6.5). Thats works very well. For receiving the data over REST I build an other spring boot application. Here I have some inheriance problem with JPA (sorry for my drawing):
Class C and class D (abstract) inherit from class B. Class A have a reference (attribute1) to class B. This attribute is an instance of entity class E, which inherit from abstract class D. I am using inheritance strategy table per class. Every class using the annotation Entity with the table name. In the database, table from class A have a correct foreign key to table from class E, but if I want to read the data the attribute1 is null. I see from the log level that eclipse link only look inside table from class C. How can I resolve this problem?
Greets Benjamin
here are the classes, class E:
#Entity(name="ep_core_voltagelevel")
public class VoltageLevel extends EquipmentContainer {
#Embedded
#AttributeOverrides(#AttributeOverride(name="value", column=#Column(name="highVoltageLimit_value")
)
)
private myPackage.DomainProfile.Voltage highVoltageLimit;
public myPackage.DomainProfile.Voltage getHighVoltageLimit() {
return highVoltageLimit;
}
public void setHighVoltageLimit(myPackage.DomainProfile.Voltage parameter) {
this.highVoltageLimit = parameter;
}
#Embedded
#AttributeOverrides(#AttributeOverride(name="value", column=#Column(name="lowVoltageLimit_value")
)
)
private myPackage.DomainProfile.Voltage lowVoltageLimit;
public myPackage.DomainProfile.Voltage getLowVoltageLimit() {
return lowVoltageLimit;
}
public void setLowVoltageLimit(myPackage.DomainProfile.Voltage parameter) {
this.lowVoltageLimit = parameter;
}
#ManyToOne(cascade={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
#JoinColumn(nullable=false, name="basevoltage_id")
private BaseVoltage baseVoltage;
public BaseVoltage getBaseVoltage() {
return baseVoltage;
}
public void setBaseVoltage(BaseVoltage parameter) {
this.baseVoltage = parameter;
}
#ManyToOne(cascade={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
#JoinColumn(nullable=false, name="substation_id")
private Substation substation;
public Substation getSubstation() {
return substation;
}
public void setSubstation(Substation parameter) {
this.substation = parameter;
}
}
Class D:
#Entity(name = "ep_core_equipmentcontainer")
public abstract class EquipmentContainer extends ConnectivityNodeContainer {
}
Class B:
#Entity(name="ep_core_connectivitynodecontainer")
public abstract class ConnectivityNodeContainer extends PowerSystemResource {
}
Class A:
public class ConnectivityNode extends IdentifiedObject {
#ManyToOne(cascade={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
#JoinColumn(nullable=false, name="connectivitynodecontainer_id")
private ConnectivityNodeContainer connectivityNodeContainer;
public ConnectivityNodeContainer getConnectivityNodeContainer() {
return connectivityNodeContainer;
}
public void setConnectivityNodeContainer(ConnectivityNodeContainer parameter) {
this.connectivityNodeContainer = parameter;
}
}
I'm trying to upgrade from Jackson 2.3 to Jackson 2.7 , and it appears the #JsonIgnore(Include.NON_DEFAULT) behavior has changed.
With the following code
package jackson;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
public static void main (String args[]) throws Exception {
MyClass foo = new MyClass();
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(foo));
}
public static enum MyEnum { A, B, C };
public static class MyClass {
public String name = "John";
#JsonInclude(Include.NON_DEFAULT)
public MyEnum myEnum = MyEnum.A;
}
}
I get this output using 2.7:
{"name":"John","myEnum":"A"}
and the following using 2.3:
{"name":"John"}
How do I replicate the behavior in 2.3 using version 2.7 ?
According to the bug ticket I put in for this, it is working as intended and that functionality can be achieved by structuring your class as:
#JsonInclude(JsonInclude.Include.NON_DEFAULT)
public static class MyClass {
#JsonInclude(JsonInclude.Include.ALWAYS)
public String name = "John";
public MyEnum myEnum = MyEnum.A;
}
See the ticket for a more thorough explanation from the author.
I tried to find an answer to this question in the Orika documentation but no luck.
I have the following classes:
public class A {
private String partNumber1;
private String partNumber2;
...
}
public class B {
private Integer shelfNumber;
private A a;
...
}
public class BDTO {
private Integer selfNumber;
private ADTO someA;
...
}
public class ADTO {
private String partNumber;
...
}
.. and the following CustomMapper's to map Objects of B to objects BDO
#Component
public class BMapper extends CustomMapper<B, BDTO> {
#Override
public void mapAtoB(B b, BDTO bdto, MappingContext context) {
super.mapAtoB(b, bdto, context);
//??? what to do here ???
}
}
#Component
public class AMapper extends CustomMapper<A, ADTO> {
#Override
public void mapAtoB(A a, ADTO adto, MappingContext context) {
super.mapAtoB(a, adto, context);
adto.setPartNumber(a.getPartNumber1() + a.getPartNumber2());
}
}
In my client code I have:
B b = new B(5, new A("100392", "100342"));
BDTO bdto = mapper.map(b, BDTO.class);
My question is, in BMapper, what is the correct way to get the AMapper to map "a" to "someA"? To put it differently, what is the correct way to map a to someA in BMapper? I suspect that it can be done through some interface in the MappingContext object.
I found an answer after some experimentation. To map property objects in the main objects mapper, i.e. the scenario explained above, one can use the protected "mapperFacade" member of CustomMapper.
So you can do something like this:
bdto.setSomeA(super.mapperFacade.map(b.getA(), ADTO.class));
Suppose there's 2 classes : A and B.
A can operate on B.
I need to be able to query all B instances that A has operated on.
And for a specific B instance, I need to be able to query all A instances that have operated on it.
What's the elegant(in the OO taste..) solution for this kind of problem?
In a language like Java I would do something like:
package com.whatever.blah;
public class A {
private Set<B> patients = new HashSet<B>;
public void operateOn(B patient) {
patient.startRecoveringFromOperation(this);
patients.add(patient);
}
public List<B> getPatients() {
return patients;
}
}
public class B {
private Set<A> surgeons = new HashSet<A>;
//this has package access to `A` can access it but other classes can't
void startRecoveringFromOperation(A theSurgeon) {
surgeons.add(theSurgeon);
}
public List<A> getSurgeons() {
return surgeons;
}
}
This really isn't doing anything special, beyond using package access to allow A access to B's startRecoveringFromOperation() method while hiding the method from most other classes. In other languages you might use a different approach to accomplish this. For instance in C++ you might declare A as a friend of B instead.
import java.util.*;
class A {
void operate(B b) {
operatedOn.add(b);
b.operatedOnBy.add(this);
}
final Set<B> operatedOn = new HashSet<B>();
}
class B {
final Set<A> operatedOnBy = new HashSet<A>();
}
public class Main {
public static void main(String[] args) {
A a=new A();
B b=new B();
a.operate(b);
System.out.println(a+" "+a.operatedOn);
System.out.println(b+" "+b.operatedOnBy);
}
}