hibernate field inheritance without baseclass access - java

I want to persist one class that extends another from an external API...
I've no access to API class so I can't anotate it as #MappedSuperclass or #Entity nor anotate its fields
But i would like to persit the public fields from superclass plus extra fields from my extended class.
//i've no access to modifications on this class as it comes from external api
public class BaseClass{
public int field1;
public int field2;
public int field3;
}
#Entity
public class MyClass extends BaseClass{
public int extraField1;
public int extraField2;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public int extraField3;
}
How can I approach this situation?
I'm using Hibernate 5.1.0.Final

I'd suggest creating getters/setters for the inherited fields and using accessor-based annotations (like is shown in this answer)

Related

Java Spring #MappedSuperclass field as #Id field in child class

I have the following superclass that will be extended by many classes. Its one property, foreignKey, will be the #Id for one of the inheriting classes.
The superclass:
#MappedSuperclass
public abstract Superclass {
#Column(name="FOREIGN_KEY")
private String foreignKey;
// Getters and setters
}
The inheriting class:
#Entity
public class ClassA extends Superclass {
#Id
#Column(name="FOREIGN_KEY")
private String foreignKey;
// Getters and setters
}
When I try building I am given the error:
No identifier specified for ClassA
Is there anyway to accomplish this?

Objectbox inherit properties from base class (which is NOT an entity itself)

I have a simple base class in which I want to have some common fields, like id etc. The base class is not an Entity by itself.
public class Base {
#Id
protected long id;
protected String someOtherCommonProperty;
}
And I have an entity class, extending the base class.
#Entity
public class Entity extends Base {
String name;
String address;
}
I would expect the entity class to inherit the fields from the base class, but what I get is
[ObjectBox] No #Id property found for 'Entity', add #Id on a not-null long property.
Is there any way to fix that, besides using interfaces and have a lot of duplicated code?
You can use the #BaseEntity annotation.
Have a look at the documentation: Objectbox - Entity Inheritence.
Shameless copy for future reference:
In addition to the #Entity annotation, we introduced a #BaseEntity annotation for base classes, which can be used instead of #Entity.
There three types of base classes, which are defined via annotations:
No annotation: The base class and its properties are not considered for persistence.
#BaseEntity: Properties are considered for persistence in sub classes, but the base class itself cannot be persisted.
#Entity: Properties are considered for persistence in sub classes, and the base class itself is a normally persisted entity.
Example:
// base class:
#BaseEntity
public abstract class Base {
#Id long id;
String baseString;
public Base() {
}
public Base(long id, String baseString) {
this.id = id;
this.baseString = baseString;
}
}
// sub class:
#Entity
public class Sub extends Base {
String subString;
public Sub() {
}
public Sub(long id, String baseString, String subString) {
super(id, baseString);
this.subString = subString;
}
}

How can I get an attribute in a class from a superclass?

I want to use Hibernate. I have a database schema and I would like to write annotations in my code.
I have az A class. It's look like this:
In A.java:
public class A {
public Integer id;
}
In B.java:
#Entity
#Table(name="table_b")
public class B extends A {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
//How can I get that id attribute from the A class?
#Column
public String string;
}
you put the annotation #MappedSuperClass above class A. The field id has to be annotated in clas A not B.

Null metatamodel fields with inherited #Embedded

I am experiencing NPEs with some embedded JPA metamodel fields using Hibernate 4.3.5.Final.
Specifically, I have the following situation:
#Entity class A
#Embedded class B in class A
#Embedded class C extending class B
I am not getting the 'Unable to locate static metamodel field...' error on startup which seems common in these situations, however all of C_'s fields are null.
Pertinent chunks of my code, simplified for legibility are:
#Entity
#Table(name = "...")
public class A extends AbstractA {
// Attempting to define override in superclass
#AttributeOverride(name = "cField", column = #Column(name = "SOME_FIELD"))
#Embedded
private B b;
...
}
#Embeddable
#Access(AccessType.FIELD)
public class B extends C {
#Column(name="SOMETHING")
private String bField;
...
}
#Embeddable
#Access(AccessType.FIELD)
public abstract class C implements Serializable {
private static final long serialVersionUID = 1L;
private String cField;
...
}
In this example, C_.cField is null.
The corresponding generated metamodel classes for the embedded classes are:
#Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
#StaticMetamodel(B.class)
public abstract class B_ extends C_ {
public static volatile SingularAttribute<B, String> bField;
}
#Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
#StaticMetamodel(B.class)
public abstract class C_ {
// This object is null
public static volatile SingularAttribute<C, String> cField;
}
Unless I've just mucked something up, a key question which I haven't seen answered is - can an embeddable class extend another embeddable class? I've got it working using association rather than inheritance - namely, having C as an an #Embedded field in B - but I'd prefer inheritance if possible.

What is the point of #MappedSuperclass if it can't be used with queries and the entity manager?

I have some tables that have some common columns and I thought that when creating entities I would have the entities extend a class where all of the mapping is defined:
#MappedSuperclass
public class AbstractLogMaster implements Serializable
{
#Id
#Basic(optional = false)
#Column("id")
protected long id;
#Column("field1")
protected String field1;
#Column("field2")
protected String field2;
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
etc...
}
#Entity
#Table(name = "logwork")
public class LogWork extends AbstractLogMaster implements Serializable
{
#Column("field3")
protected int field3;
public int getField3()
{
return field3;
}
}
Using this in a query Predicate p1 = builder.equal(logWork.get(LogWork_.field1), f1); results in a NPE (specifically because of the logWork.get(LogWork_.field1) part - the #StaticMetaModel doesn't seem to work with mapped superclass)
Looking at the JPA documentation I discovered that:
Mapped superclasses cannot be queried and can’t be used in EntityManager or Query operations. You must use entity subclasses of the mapped superclass in EntityManager or Query operations. Mapped superclasses can’t be targets of entity relationships. Mapped superclasses can be abstract or concrete.
Does this mean that none of the fields I map in the mapped superclass are available to be used by the extending entities in criteria queries? If so, what is the point of putting mapping into the superclass?? If not, what am I doing wrong??
My bad. Turns out the answer to this question is the "proper" use of the #StaticMetamodel annotated companion classes. The mapped superclasses must all have individual static metamodels that inherit in the same way (I had bundled everything for the entity into one metamodel, LogWork_)
#StaticMetamodel(AbstractLogMaster.class)
public final class AbstractLogMaster_
{
public static volatile SingularAttribute<AbstractLogMaster, Long> id;
public static volatile SingularAttribute<AbstractLogMaster, String> field1;
public static volatile SingularAttribute<AbstractLogMaster, String> field2;
}
#StaticMetamodel(LogWork.class)
public final class LogWork_ extends AbstractLogMaster_
{
public static volatile SingularAttribute<LogWork, Integer> field3;
}

Categories