Eclipselink class extractor - jpa

I have 2 entities object A and B. B extended A and I have #ClassExtractor on A to return the correct class type from the database row. However, if I want to query on A, will it check for class extractor?
A.java
#Entity
#Table
#ClassExtractor(MyClassExtractor.class)
public class A implements Serializable{
...
}
B.java
#Entity
#Table
public class A extends B{
...
}
MyClassExtractor.java
public class MyClassExtractor extends ClassExtractor{
#SuppressWarnings("rawtypes")
#Override
public Class extractClassFromRow(Record record, Session session){
return B.class;
}
So when I do query on A, will it go to the class extractor and return B?
Thanks in advance!

Related

MapStruct: How to specify in mapper the type of objects should be created in the destination List?

In Dozer the mapping between domain and DTO classes is done as following:
<mapping>
<class-a>Domain.A</class-a>
<class-b>DTO.A</class-b>
<field>
<a>cField</a>
<b>cField</b>
<a-hint>Domain.Ab</a-hint>
<b-hint>DTO.Ab</b-hint>
</field>
</mapping>
hint is used to let dozer know what type of objects you want created in the destination List(Correct me if I'm wrong).
How can we achieve the same in MapStruct?
Where, the implementation of class A is as following:
public class A<T extends Ab> extends B<T>{
}
Implementation of B is as following:
public class B<T extends C> implements serializable{
private List<T> cField = new ArrayList<T>();
private String d;
//getters and setters
}
Implementation of Ab is as following:
public abstract class Ab implements C{
}
Here C is an interface with no fields in it.
There are no fields in Ab and is extended by some E and F classes.
Could you please let me know how would the mapper look like for the above scenario ?

JPA annotation that will include fields from non-entity super class (from JAR!)

I am working with spring boot, spring JPA and have following problem: I created some class "B" that extends some class "A" from JAR, put #Entity and #Table annotations on class "B" (I can't do the same on class "A") but when Spring/Hibernate is creating tables, only fields from class "B" are included.
// some class from JAR
// not an Entity
class A {
String fieldA;
}
#Entity
#Table(name = "B")
class B extends A {
String fieldB;
}
Table that is created in database
+--------+ +--------+--------+
| fieldB | but I want this to be made | fieldA | fieldB |
+--------+ +--------+--------+
How to manage that? Is there some JPA annotation that could be put on class B that would solve this?
EDIT: If #AttributeOverride annotation is the solution, how to mark that overriden field as Id when #Id annotation can't be put outside the class?
You can set the #Id on a getter.
#Entity
#Table(name = "B")
class B extends A {
String fieldB;
#Id
#Override
public String getFieldA() {
return super.getFieldA();
}
}

Can I use the #Value annotated into the a class annotated as #Entity?

I have a abstract class where I would do:
#Value("${dao.value:5}")
private Integer value;
The value return null, The abstract class is annotatedas #Configurable
This class is used in several class annotated as #Entity
This is the sping boot configuration:
#SpringBootApplication
#EnableJpaRepositories("package.dao")
#ComponentScan("package.server")
#EntityScan("package.model")
#Import(LoggerRegistryClientContext.class)
#EnableSpringConfigured

How do a Criteria polymorphic join in JPA?

I have a project that have a modeling like this:
public abstract class Actor{}
public class Person extends Actor {}
public class Organization extends Actor {}
public abstract class Role{ #ManyToOne #JoinColumn(name="ID_ACTOR") }
public class Customer extends Role{}
public class Employee extends Role{}
I would like to get a List<Role> which plays a particular Actor:
public List<Role> getRoles(Actor actor) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Role> criteria = builder.createQuery(Role.class);
//How to do this filter using Criteria API (JPA 2.1)?
return query.getResultList();
}
The SQL below makes the job:
SELECT 'Customer' as role, c.id as id, c.id_actor as actor
FROM customer c
left join person p on p.id = c.id_actor
left join organization o on o.id = c.id_actor
where c.id_actor = ?
UNION
SELECT 'Employee' as role, e.id as id, e.id_actor as actor
FROM employee e
left join person p on p.id = e.id_actor
left join organization o on o.id = e.id_actor
where e.id_actor = ?
I resolve the problem making a bidirectional relationship between Actor and Role class:
public abstract class Actor{ #OneToMany(mappedBy="actor") List<Role> roles }
public class Person extends Actor {}
public class Organization extends Actor {}
public abstract class Role{ #ManyToOne #JoinColumn(name="ID_ACTOR") Actor actor }
public class Customer extends Role{}
public class Employee extends Role{}
That way I could get the List<role> which plays a particular Actor:
public List<Role> getRoles(Actor actor) {
return actor.getRoles();
}
Initially I would like to avoid the bidirectional relationship, but I realized that in this particular case, there are direct benefits

How do I "get" a Scala case object from Java?

I created a hierarchy of case objects in Scala that looks like the following:
package my.awesome.package
sealed abstract class PresetShapeType(val displayName: String)
case object AccelerationSensor extends PresetShapeType("Acceleration Sensor")
case object DisplacementSensor extends PresetShapeType("Displacement Sensor")
case object ForceSensor extends PresetShapeType("Force Sensor")
case object PressureSensor extends PresetShapeType("Pressure Sensor")
case object StrainSensor extends PresetShapeType("Strain Sensor")
I also have a piece of Java code in which I'd like to access PressureSensor, but the following does not work:
package my.awesome.package.subpackage;
import my.awesome.package.PressureSensor;
// Do some stuff, then...
DVShape newshape = DVShapeFactory.createPresetShape(PressureSensor, new Point3f(0,0,0));
So, how do I reference the PressureSensor case object from Java? I decompiled the byte code for both the PressureSensor and PressureSensor$ classes, which yielded the following:
Compiled from "DVShapeFactory.scala"
public final class org.nees.rpi.vis.PressureSensor extends java.lang.Object{
public static final java.lang.Object productElement(int);
public static final int productArity();
public static final java.lang.String productPrefix();
public static final int $tag();
public static final java.lang.String displayName();
}
Compiled from "DVShapeFactory.scala"
public final class org.nees.rpi.vis.PressureSensor$ extends org.nees.rpi.vis.PresetShapeType implements scala.ScalaObject,scala.Product,java.io.Serializable{
public static final org.nees.rpi.vis.PressureSensor$ MODULE$;
public static {};
public org.nees.rpi.vis.PressureSensor$();
public java.lang.Object readResolve();
public java.lang.Object productElement(int);
public int productArity();
public java.lang.String productPrefix();
public final java.lang.String toString();
public int $tag();
}
But that didn't yield any great insight.
from Java, say:
my.awesome.package.PressureSensor$.MODULE$
PressureSensor$.MODULE$ should give you the instance of the case object.
This is still a hack, but in my opinion a bit more readable in Java. Just add a method to explicitly return the reference to the singleton instance (it shows up as a static method on the class):
sealed abstract class PresetShapeType(val displayName: String)
case object AccelerationSensor extends PresetShapeType("Acceleration Sensor") { def instance = this }
case object DisplacementSensor extends PresetShapeType("Displacement Sensor") { def instance = this }
case object ForceSensor extends PresetShapeType("Force Sensor") { def instance = this }
case object PressureSensor extends PresetShapeType("Pressure Sensor") { def instance = this }
case object StrainSensor extends PresetShapeType("Strain Sensor") { def instance = this }
And then in Java:
import my.awesome.package.PressureSensor;
DVShape newshape = DVShapeFactory.createPresetShape(PressureSensor.instance(), new Point3f(0,0,0));