What the difference between #Transient annotation and transient modifier - interface

Let's say i have an entity like this. What the difference between field a and b?
public class Human implements Serializable{
public transient String a;
#Transient
public String b;
}

transient is about not serializing the field when #Transient is about not persisting the field

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 ?

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!

Scala: Get value of child from parent?

Trying to get values of all fields of child from parent class like this:
for (field <- this.getClass.getDeclaredFields) {
Logger.debug(field.getName)
field.get(this)
}
and got error
Exception: Class models.Model$$anonfun$4 can not access a member of
class models.Good with modifiers "private"
at line
field.get(this)
In Good class I don't have private fields:
class Good(id: Option[String]) extends Model[realGood](id){
lazy val title: String = this.load[String](realObject.get.title)
lazy val cost: Double = this.load[Double](realObject.get.cost)
}
What's wrong with this code?
As hinted in the comments, Scala's conversion to java bytecode isn't always straightforward (though it's usually pretty predictable, once you get the hang of it). In particular, public fields in Scala compile to a private field with a public getter in java bytecode:
fukaeri:~ dlwh$ cat zzz.scala
class Good(id: Option[String]) {
lazy val title: String = ???
lazy val cost: Double = ???
}
fukaeri:~ dlwh$ scalac zzz.scala
fukaeri:~ dlwh$ javap -private Good
Compiled from "zzz.scala"
public class Good {
private java.lang.String title;
private double cost;
private volatile byte bitmap$0;
private java.lang.String title$lzycompute();
private double cost$lzycompute();
public java.lang.String title();
public double cost();
public Good(scala.Option<java.lang.String>);
}
You can see that Good has private fields for each of your declared public fields, in addition to public getters. Because the fields are lazy val, they also have computation methods for initialization, and there's a bitmap$0 field to ensure that the lazy vals are initialized only once.
In your loop, you can use field.setAccessible(true) to fix your exception.

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.

Scala object MODULE$

What's the purpose of Scala object MODULE$?
The following Scala object:
object TestScalaObject {
val TEST_SYMBOL = "*"
def testMethod(x : String) : String = x
}
compiles into two bytecode files TestScalaObject.class and TestScalaObject$.class which if I disassemble to get the equivalent Java code I get:
TestScalaObject.class:
public final class TestScalaObject extends java.lang.Object{
public static final java.lang.String testMethod(java.lang.String);
public static final java.lang.String TEST_SYMBOL();
}
TestScalaObject$.class:
public final class TestScalaObject$ extends java.lang.Object implements scala.ScalaObject{
public static final TestScalaObject$ MODULE$;
public static {};
public java.lang.String TEST_SYMBOL();
public java.lang.String testMethod(java.lang.String);
}
I can see a public static final TestScalaObject$.MODULE$ but what is it used for if I can access everything I need through TestScalaObject.TEST_SYMBOL and TestScalaObject.testMethod() if I ever wanted to do that from Java
MODULE$ holds an instance of the instantiated class. See the Singleton pattern in Java. I don't know of a good source for it, so here's the Wikipedia entry for Singleton.