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

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

Related

Jakarta #Path inheritance from superclass

What I would like to achieve is to create a base class with a common #Path, and then extend it and having all the derived class starting from that base path.
In other words:
import javax.ws.rs.GET
import javax.ws.rs.Path
#Path("/base-path")
abstract class MyBaseController
#Path("/specialized-path")
class MySpecializedController : MyBaseController() {
// I would like to call this endpoint using http://domain/base-path/specialized-path
#GET
fun saySomething() = println("something")
}
Is this somehow possible?

Eclipselink class extractor

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!

autofac Register closed subclasses of abstract open generic

public class Sub1 : Base<SomeClass>
{
// for sake of getting injection to work, not injecting anything in ctor
public Sub1() {}
....
}
public class Sub2 : Base<SomeOtherClass>
{
// for sake of getting injection to work, not injecting anything in ctor
public Sub2() {}
....
}
public abstract class Base<T>
{
// abstract, so no ctor
....
}
I tried the following but I'm getting the exception listed below. Notice it the exception references the Base abstract class. What am I missing?
builder.RegisterAssemblyTypes(typeof(Base<>).Assembly)
.Where(t => t.IsSubclassOf(typeof(Base<>))).AsClosedTypesOf(typeof(Base<>)).InstancePerDependency();
No constructors on type 'Base`1[SomeClass]' can be found with the constructor finder 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'.
I think you mixed with generic and non generic abstract class registrations.
You have generic abstract class. Therefore your register should be like this:
builder.RegisterAssemblyTypes(typeof(Base<>).Assembly).AsClosedTypesOf(typeof(Base<>)).InstancePerDependency();

How to implement a Java abstract interface in Scala

How do you implement a Java abstract interface in scala?
Abstract interface :
public abstract interface KeyIndex<K>
extends Serializable
{
public abstract long toIndex(K paramK);
public abstract Seq<Tuple2<Object, Object>> indexRanges(Tuple2<K, K> paramTuple2);
}
It seems, that your KeyIndex class is written in Java. In Scala every field not labeled private or protected is public. There is no public keyword in Scala.
But implementing a Java class in Scala is possible:
class KeyIndexImpl extends KeyIndex[geotrellis.spark.SpatialKey]{
override def toIndex(paramK: geotrellis.spark.SpatialKey): Long =
1l
override def indexRanges(paramTuple2: (geotrellis.spark.SpatialKey, geotrellis.spark.SpatialKey)): Seq[(AnyRef, AnyRef)] =
Seq((paramTuple2, paramTuple2))
}

Understanding Case class and Traits in Scala

I have a simple trait as defined below:
trait MyTrait {
def myStringVal: String
}
My case class which implements this trait is as below:
case class MyCaseClass(myStringVal: String) extends MyTrait {
...
...
}
Coming from a Java world, I find it a bit difficult to fathom the fact that MyCaseClass actually implements this just by defining a parameter to MyCaseClass. I understand that thy byte code would actually write the getter and setter. But how is this possible without any var or val?
My understanding is that if there is no var or val, then there is no getter or setter method generated. In that case how is the above case class MyCaseClass implementing myStringVal method?
Sometime too much of this Scala magic is difficult to understand especially with legacy code.
You might want to check out this blog article covering what case classes exactly are and why they are so useful.
In your example, the trait MyTrait has no use, except being able to function like a java interface. Note, that the default visibility in scala is public. By default case class parameters are immutable so in your example val is automatically inferred by the compiler for the myStringVal argument.
What magic do case classes do?!
Convert all constructor parameters to public readonly (val) by default fields
Generate the toString(), equals() and hashcode() methods using all constructor params for each method
Generate companion object with the same name containing an appropriate apply() and unapply() method, which are basically just a convenience constructor allowing to instantiate without using the new keyword and an extractor which by default generates an option-wrapped tuple of the case class parameters.
EDIT: Sample compiler output for (case) classes (copied from scalatutorial.de)
A simple scala class definition like
class A1(v1: Int, v2: Double)
gets compiled to the java code
public class A1 extends java.lang.Object implements scala.ScalaObject {
public A1(int, double);
}
the analogous case class
case class A2(v1: Int, v2: Double)
gets compiled to the following java classes
public class A2 extends java.lang.Object implements
scala.ScalaObject,scala.Product,java.io.Serializable {
public static final scala.Function1 tupled();
public static final scala.Function1 curry();
public static final scala.Function1 curried();
public scala.collection.Iterator productIterator();
public scala.collection.Iterator productElements();
public double copy$default$2();
public int copy$default$1();
public int v1();
public double v2();
public A2 copy(int, double);
public int hashCode();
public java.lang.String toString();
public boolean equals(java.lang.Object);
public java.lang.String productPrefix();
public int productArity();
public java.lang.Object productElement(int);
public boolean canEqual(java.lang.Object);
public A2(int, double);
}
public final class A2$ extends scala.runtime.AbstractFunction2
implements scala.ScalaObject {
public static final A2$ MODULE$;
public static {};
public scala.Option unapply(A2);
public A2 apply(int, double);
public java.lang.Object apply(java.lang.Object, java.lang.Object);
}
Scala case classes have a plenty of boilerplate implemented for you, and having all the constructor parameters automatically exposed as vals is one of these things.
If you try avoiding vals in a regular class, like that:
trait MyTrait {
def myVal: String
}
class MyClass(myVal: String) extends MyTrait
Compiler will show you the error message, that MyClass has to be abstract, as it does't override myVal method, but adding val or var to the class constructor parameter will solve the issue.
Case classes are different -- some default methods are generated for them. This includes val getters for the parameters. Think of case classes as POJOs -- this is a useful bit of syntactic sugar, since they don't need private members.
Some other useful methods are generated too, for example copy, toString, apply and unapply.