does JPA use JavaBeans BeanInfo information? - jpa

According to the JPA 2.1 spec:
It is required that the entity class follow the method signature
conventions for JavaBeans read/write properties
(as defined by the JavaBeans Introspector class)
for persistent properties when property access is used.
In this case, for every persistent property property of type T
of the entity, there is a getter method, getProperty,
and setter method setProperty.
Does this imply that the methods must always be named getProperty
and setProperty
(per the design pattern "convention" in ยง8.3.1 of JavaBeans spec 1.0.1,
"[i]f we don't find explicit BeanInfo on a class");
or could a BeanInfo class be provided to direct the JPA implementation
to a different method
(per the full description of the Introspector class in that spec)?
Although I'm also curious about how Hibernate or other JPA implementations
implement this,
I'm instead really asking what implementation the JPA spec requires.

The methods MUST be named as per the Java Beans contract ... getXXX (or isXXX), setXXX. There is no BeanInfo hook used by any JPA implementation I know of

Related

Does BeanIO require I provide a setter

It appears BeanIO requires I provide setters for my fields. Is this necessary? I created a BeanIO writer, I intend only to write files, and I have getters for each field - why would I need setters?
If your beans are only meant to be serialized (i.e. used by a BeanWriter), you can declare your stream as mode="write". This will cause BeanIO to look for getters but not setters.
The reference guide states that:
By default, a stream mapping can be used for both reading input streams and writing output streams, called readwrite mode. Setting mode to read orwrite instead, respectively restricts usage to a BeanReader or a BeanWriter only, but relaxes some validations on the mapping configuration.
When mode is set to read, a bean class does not require getter methods.
When mode is set to write, a bean class may be abstract or an interface, and does not require setter methods.

Why JPA requires Entity classes to be non-final & fields non-final

Was reading about JPA here. Two of the requirements of an Entity class are that
The class must not be declared final. No methods or persistent instance variables must be declared final.
The class must have a public or protected, no-argument constructor.
Persistent instance variables must be declared private, protected, or package-private.
Was curious to know why are these conditions required ?
The class must not be declared final. No methods or persistent instance variables must be declared final.
JPA implementations use proxies in front of your entities to manage for example: Lazy loading. As a final class cannot be extended, a proxy cannot be built.
Some implementations as Hibernate can persist final classes but it can affect performance more info.
The class must have a public or protected, no-argument constructor.
These kind of frameworks and others in order to create new objects use ```Class.newInstance()`` that is the reason why a no arg constructor is needed.
Persistent instance variables must be declared private, protected, or package-private.
Being only accesible through accessor or business methods allow interception in proxies.
The reasons are (at least some of them):
JPA provider needs to create instances of the entity dynamically. If class would contain the only constructor which takes arbitrary arguments, JPA provider cannot figure out values for those arguments. That's why it must has a no-arg constructor.
JPA implementations deal with persisting instances of your entities classes. that's why the class, methods and variables cannot be final.
Because you don't want access to the variables from outside directly, in order to keep encapsulation - this is an OOP reason. another reason is that many frameworks of persistence are having a getter/setter method to identify POJO "properties".

Play framework controller test - No implementation for <classname> was bound

I would like to write test for a controller class. The controller class takes a service object as constructor parameter. Added the #Inject annotation to the constructor of the service class.
class AssociateService #Inject()(configuration: Configuation){...}
The constructor parameter of the service class is a custom configuration object also created for the application. I added the #Inject to the constructor of the config class as well. Now I'm getting these types of error messages:
No implementation for "className" was bound.
Could not find a suitable constructor in java.lang.Integer. Classes must have either one (and only one) constructor annotated with #Inject or a zero-argument constructor that is not private.
The configuration class has several constructor parameters, those are "basic" types (Int, Boolean) and one parameter is a custom class type (className).
How should I do this binding or is it just enough to annotate something else?
And why it says that constructor error message?
As far as I know, there are two ways with tests and guice, with trade offs:
Don't using field injections, using only constructor injections and fields assignment in constructor for injected parameters. This approach enables very simple solution for testing, just don't use dependency injection in tests. But all your classes must have ability to be created with new operator in test cases...
Ps. You can define optional constructor and use field injections, of course, but it is not very clear solution.
Creating correct module with injectable interfaces binding to its implementations for every test or group of similar tests. Sometimes this approach takes a lot of unnecessary working hours.
You must design your software to maintain testability. Sometimes not every line of code in project need to be tested, sometimes not every code is testable, you must separate it from important parts of your software, that requires testing. If you design your software with single responsibility principe so writing tests is much easer...

OPAL: Manually creating an annotated method

in the OPAL framework, is it possible to manually create an annotated method?
I currently have the following code:
Method(0, "signaturePolymorphicMethod",
MethodDescriptor(ObjectType("java/lang/Object"), VoidType), Seq())
and I want to add the annotation
#java.lang.invoke.MethodHandle$PolymorphicSignature
to this method. How can I do this?
Annotations are generally stored using the JVM's general "Attributes" mechanism.
In this case the annotation is a non-public inner class of MethodHandle with the "Runtime Retention Policy". Hence, to mark a method as having a "Polymorphic Signature" it is necessary to add the RuntimeVisibibleAnnotations_Attribute to the respective method's attributes table. However, given that the visibility of the annotation is limited to the java.lang.invoke package this is (in this specific case) probably rarely useful. Nevertheless, it is possible to query methods in the respective package

Typhoon: how to inject class instead of instance

I have a third-party library which is written in Swift. The library provides a class that has some class methods in it. Using Typhoon, I want to inject the class into one of my classes so that, under unit testing, I could inject a mock class that provides fake class methods. I'm new to Typhoon and I went though the documentation, but haven't figured out how to do it. Is this even doable with Typhoon?
Yes, in the User Guide the section on Injecting Configuration shows how to inject primitives, scalar values and so forth.
To inject a class:
[initializer injectParameterWith:[SomeClass class]];
This also applies to property injection and method injection.
To inject a selector:
[initializer injectParameterWith:NSValueFromPrimitive(#selector(selectorValue))];
Typhoon rules:
References to other definitions are resolved to the built instance.
Simple objects, primitives and scalar values are injected as-is (scalar values and primitives must be wrapped).
Collections (NSArray, NSSet, etc) that contain references to other definitions have those references resolved to the built instance. Any other values pass through as is.
There is also Typhoon Config, which allows storing configuration, simple objects and so forth in an external plist, json or properties file.