Multiple Inheritance in Java using JPA - java

I have a problem with multiple inheritance since (obviously) class A extends B, C { } is not possible in Java. The traditional workaround is using B and C as interfaces. But as you know JPA doesn't support interface-entities. A workaround would be to use an aggregation instead of a composition but I want to make sure that A cannot exist without B. Therefore I need a composition. I show my problem in an example
family_name
kindergarden_name
child_name
is not null
can be null (not dependent to col3)
can be null (not dependent to col2)
I create a few classes:
class Family
- name: String
class Child
- name: String
class Kindergarden
- name: String
class FamilyWithChild extends Family, Child
class FamilyWithKindergarden extends Family, Kindergarden
class FamilyWithChildAndKindergarden extends FamilyWithChild, Kindergarden
- child: Child
How would you solve this problem. I haven't found any real solutions besides making a aggregation.
The table shown above might have more columns with additional information about Family, Child and Kindergarden.
Making an aggregation would work but this is not a good option for me in that scenario.

Related

How to implement inherited models in ActiveJDBC with associations between superclass and another model?

I'd like to build something like this:
abstract class Owner extends Model {}
class User extends Owner {}
class Group extends Owner {}
class Thing extends Model {}
where every Thing has one and only one Owner that can be a Group OR an User.
How is it possible? Do I have to make Owner not abstract to let ActiveJDBC map the owner_id-column in the things-table to an owner? But how does it determine what kind of owner we have as http://javalite.io/inheritance says single table inheritance is not implemented in ActiceJDBC.
You need to use Polymorphic Associations. There is a difference between inheritance and ownership.
ActiveJDBC inheritance is limited to passing common functionality to sub-classes. Then you need to use relationships to create "A has many B" and the like, and Polymorphic Associations seems like your best bet.
So, in your case User and Group will be polymorphic parents of Thing. It is your choice then to create a separate class Owner (only if they share some functionality).
So.. you will have three tables: USERS, GROUPS, THINGS.

detecting UML relationships from java source code

my program runs through source code easily enough and I can detect easy relationships such as implementation or inheritance using extends just by searching for where the class is defined. However, I'm a bit stuck with ideas on how to detect other relationships such as if a class has association or aggregation with another class.
So far I have tried parsing the code and looking for where other methods are called but I'm not sure of an exact code definition of these relationships.
Sorry if I am being unclear I can try and explain better if you don't understand just let me know in the comments.
Aggregation and composition both look like member variables in Java
e.g.
class MyClass {
private HerClass h;
}
MyClass HAS-A HerClass member - so that's composition or possible aggregation. You could tell the difference based on whether MyClass creates the HerClass - that would PROBABLY be composition.
Association is based on dependency. Why don't you use the imports to find out which classes are depended on? Or you could scan any use of type names in the code - the moment a type name is mentioned, there's a "uses" association.
The problem is that there is not any strict definition how to translate Java classes relations in associations, dependencies and aggregations. You should set the rules yourself, only check them against the UML standard.
I would advice the following :
UML Java
Dependency A->B Class A mentions class B
Association A->B Class A has reference {that can have reference} (it is recursive!} to class B
Composition A->B Class A has array or collection of B or of references to B AND
(black diamond) no other classes have instances of B or references to them,
either single or collective (arrays, collections)
Shared aggregation A->B Class A has array or collection of B or of references to B AND
(empty diamond) at least one other class has an instance of B or references to such,
either single or collective (arrays, collections)
If according to the last rule, you get two-sided shared aggregation A-B, it is forbidden. So, change it to two mutual shared aggregations.
Remember, that Association and Shared aggregation have NO strict definitions, only limitations.

Java - JPA: How can I create a table for each subclass but not for superclass?

I have a class structure like:
abstract class A {
String a;
}
class B extends A {
String b;
}
class C extends A {
String c;
}
Now I want JPA to create a table for each subclass.
I have looked some previous questions but I am confused. How can I map these classes?
If the A is inherited because of mappings and you do not need separate entity for it, you can defined it as MappedSuperClass. Mapped superclass is only for inheriting mappings, you cannot query it.
On the other hand if real inheritance is needed - depending about implementation - you can go for table per (concrete) class inheritance strategy. Support for this strategy is optional, so it is not guaranteed to work with all implementations. It is supported at least with fresh versions of
Hibernate
DataNucleus
OpenJPA
EclipseLink.
In your case needed step would be then to add following to the class A: #Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
Why not annotate Class A with #MappedSuperClass use Table sub class or Table for Concrete Class inhertience strategy

Using Hibernate Versioning in a middle class of a hierarchy

I want to use Hibernate Versioning in my application but I have a problem in a class hierarchy like this :
C extends B and B extends A.
I want to use version in Class B and I don’t need to use version in class A and I know when I use versioning in Class B automatically it will be effective in Class C.
I implement inheritance in hibernate with 'joined-subclass' and ‘Version’ tag is not defined for 'joined-subclass'. How can I use versioning in my class hierarchy?
You can't do this with joined-subclass, because, as you have already observed, it will apply to any further subclasses.
You'll probably have to use table-per-subclass to get what you want.

Hibernate: How can I join 2 classes in one table?

So, I'm pretty new to Hibernate and I have a problem.
I have an abstract class (the super-class, class Super), and 5 subclasses which should use the proprieties from the class Super and add a new propriety (a new column)
So how can I do this? Should I extend the class Super from java, or it's enough to join the classes using a JPA annotation.
Here is the second problem. How can I have 1 table for 2 classes.
Someone (smarter than me) told me to use the #JoinTable, but form my searches with google, I think I need to use #Inheritance(strategy=InheritanceStrategy.JOINED)
Can I use the #JoinTable too?
Yours is a case of inheritance:
add the #Inheritance(stretegy=InheritanceStrategy.SINGLE_TABLE) annotation on your Super
add the #DiscriminatorColumn annotation (and setting its attributes name and discriminatorType) (again on the Super)
on each subclass extend the Super, and add the annotation #DiscriminatorValue, with different value for each of the subclasses.
If you are new to Hibernate, you should read its documentation. Inheritance strategies are explained here and using annotations to express inheritance strategy is explained here

Categories