hibernate cast base class into subclass - java

Ok I got a base class and a subclass
what I want to do is to turn base class into sub class in sql table
By changing discriminator value inside xml at run time without using annotation
how can I achieve such an approach
I'm using shared table per subclasses

We can do the Hibernate Inhertitance with and without using annotation
Take a look at this article
1.Table Per Hierarchy using xml file
2.Table Per Concrete class using xml file
3.Table Per Subclass using xml file

Related

Java class parser

I am parsing all the class files in a jar via Objectweb asm (http://forge.ow2.org/projects/asm/). The idea is to parse and store (and use for something later) all the public/protected methods and fields in each class file. It is working as expected. What I dont get is the list of methods, declared by the interface and those inherited from superclasses and superinterfaces. Is there a smart parser available that would give me the above list?
I could load the class file and then use java.lang.Class object to get what I need. But loading classes might fail because of dependencies. I would rather parse and get that info.
The data you want is simply not there. Inherited members are implicit: you only have the names of the class and interfaces where they can be looked for, and you need to parse the corresponding class files.
You can simply do this :
Class superclass = aClass.getSuperclass();
aClass.getClass().getInterfaces();
aClass.getClass().getMethods();

How to handle various concrete implementations of an Interface with JAX-B

I have a class that any I need to marshal to XML.
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class ClassToBeMarshalled {
public Interface object;
}
The Interface is implemented by a lot of concrete classes and most of them are vendor specific whose source code I don't have access to.
So my problem is:
If I try to marshal that class, JAX-B will complain that the current concrete implementation of Interface is not known in the context - In another words, the concrete class was not loaded into the context by calling JAXBContext.newInstance providing the current implementation.
The most common ways to sort out that problem are:
1) Use XMLSeeAlso - not a viable option as there are a lot of concrete classes
2) Annotate each class with #XmlRootElement - not a viable option as I don't have access to all the classes
Does anyone know a way to make JAX-B load the concrete class into its context as the need arises?
Thanks.
P.S.: I'm using JAX-B RI
You could mark your object as #XmlAnyElement(InterfaceHandler.class) where InterfaceHandler is a DomHandler capable of translating between a DOM representation and the actual implementing classes. That handler should probably store the class name when marshalling, and use that class name to create the instance when unmarshalling. It might either configure the instance manually, perhaps using some helper classes designed to work with beans, or it might use another jaxb context which includes that specifically named class and will handle that object with all its nested children.
Also have a look at the #XmlElementRef annotation. I fear that in order to make this work properly, you'd have to at least know all the implementing classes at compile time, but perhaps there is a way you can make this work for you as well, with less trouble than the generic solution outlined in the previous paragraph.

Hibernate - derived java classes for tables

I generated mapping files and POJOs in Netbeans instead of writing them myself. Is it possible to use a derived class in a place of an inherited class? An example would be something like this:
Person.hbm.xml - mapping file
Person.java - generated class (strategy class per table)
PersonExtended - class that extends Person.java
So when I create a new object:
PersonExtended personextended = new PersonExtended(<parameters>);
Would I be able to call methods like:
session.save(personextended) or session.delete(personextended)
?
Is this scenario sensible or should I add any code that I need to in a generated class? Thanks in advance for help or suggestions.
-------Edit--------
In my database I don't have the typical structure that would be possible to be mapped as an inheritance. I merely want to keep the additional methods separate from the main java class for an entity.
Best Regards,
sass.
you will have to tell hibernate how your extended classes should be mapped via a the hbm.xml file. Depending on the strategy Hibernate should use for polymorphism you might have to assign a descriminator value. there are 3 different strategies when using subclasses known as "table per class" "table per concrete class" and "table per subclass".
You can define subclasses in the hbm-xml file by using the <subclass> or <joined-subclass> elements
if you correctly defined your hbm.xml file you can then use session.save(new PersonExtended()) or sth.
you can read up on this here:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/inheritance.html
hope that helped..

Marshalling arraylist of abstract class in JAXB

I have a Java application that I want to save the data in XML instead of a database.
We decided to go with JAXB and instead of generating files based on the schema, we just added annotations to our Java files.
The issue we are running into is that we have an ArrayList of an abstract class called Node. A Node has subclasses of either Module or ScreenImage. When we marshall the arraylist, it doesn't save the type. Such as:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Module"
Does JAXB support elements that are abstract?
How do I get JAXB to save the type, so that I'm able to unmarshall it?
axtavt's suggestion is fine. I just want to add another approach. You could make use of #XmlSeeAlso, where you can declare other classes that should defined and visible to JAXBContext. You only have to make sure to declare the annotation within a class that is already visible to JAXBContext.
e.g.:
#XmlRootElement
#XmlSeeAlso({Node.class, Module.class, ScreenImage.class})
class SomeContent {
private List<Node> nodes;
///... accessors
}
It should work fine.
Note that if your Module and ScreenImage are not statically accessible from the bound classes (i.e. the classes specified in JAXBContext.newInstance(...)), they should be bound explicitly (add them to the JAXBContext.newInstance(...) parameters).

Class inheritance with Hibernate and hbm2java

I am generating my domain objects from my hbm files with hbm2java and now I want them to all inherit a base class. This class will have some utility methods for dealing with listeners. It does not really need to be persisted and I am hoping that I would not have to do a hbm file for it since it will be abstract and only have methods. Is there a meta tag or something to have all generated class extend another class?
Or is this such a lame design that it simply cannot be done with hibernate?
I remember doing this way back. See this: http://docs.jboss.org/tools/2.1.0.Beta1/hibernatetools/html_single/#d0e4159
Basically you want to use a meta tag here with attribute extends for a common base class or implements for a common base interface.

Categories