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

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".

Related

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...

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.

does JPA use JavaBeans BeanInfo information?

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

IoC DI, How to I resolve deep within the core?

Everyone says that an IOC should not be static or global in any way and should be created at root. But how do I get at it deep within my code?
Lets say I have an entry point ClassA. In here I can create an instance of an IOC and register all my interfaces to concrete types etc. But what then? Do I now start passing the IoC around ? Surely this would violate DIP ? None of the articles I have read discuss getting access to the Ioc and I would have thought it fundamental. I'm sure I must be missing something very simple here :)
Lets say ClassA creates a ClassB which creates A ClassC. Class C needs access to my resolved IDatabase. How does it get it ? Do I have to pass the IoC all the way in ?
You should be passing in the dependencies that a class needs. If Class C needs access to IDatabase then you must allow your IoC Container to pass an implementation of IDatabase into Class C, for example by having an IDatabase parameter on the Constructor for Class C.
If the IoC framework has the responsibility for creating Class C, then it also has the responsibility for giving Class C the dependencies it needs. As long as IDatabase has been registered in the IoC Container against a class which implements IDatabase, then an IDatabase parameter on Class C's constructor should be passed an implementation of IDatabase automatically.
To look at your example of ClassA creating ClassB which creates ClassC. Class A should not be 'newing' up an instance of ClassB using the 'new' keyword (I'm using C# or Java syntax here).
Rather, ClassA, ClassB and ClassC should all be registered with your IoC Container. If ClassA needs an instance of ClassB, then give ClassA a constructor which takes a parameter of ClassB. Your IoC Container should automatically resolve an instance of ClassB and pass it into ClassA. The same applies all the way down the chain as far as you need to go.
Using constructors (aka 'Constructor Injection') is not the only option here. You can often also use Property Injection, whereby public property setters are automatically set by your IoC Container (and there is also the much rarer 'Interface Injection'). But Constructor Injection is often the best way to go because it is then very clear exactly what dependencies a class needs before it can be instantiated; Property Injection can be unclear because a property may or may not be set by the IoC Container, depending on whether or not the property type has been registered or not.

ADO.NET fails to generate parameterless constructor

I've created an ADO.NET model called EF and added a DbContext generator, which populates my /Model folder with an EF.tt and .cs files, one for each entity.
in general the system creates classes with parameterless constructors... for some reason I can't fathom I have an entity that's missing this constructore. It is not an abstract class, has not base type and has public access. I have tons of other such classes but they all have parameterless constructors. I've googled and looked around VS trying to figure what's special about this one, and how I can make it generate the constructor, but find no answer.
I can always create this in a partial definition but I'd rather figure it out. Also, if I right-mouse click over the EF.tt I see a choice in the menu called "Run Custom Tool" but when I select it nothing seems to happen. How does one regenerate the .cs files?
p.s. yes, I have cleaned and rebuilt the solution in case it just got messed up but still problem
In C# (are you using C#?):
When you define no constructor in your class, a parameterless constructor will be created by compiler by default
When you define parameterless constructor (and maybe some others) the parameterless constructor will also be present as you defined it
When you define more than zero constructors, but no parameterless one, the compiler does not create a parameterless constructor for you. In this case it's your responsibility to define it (in partial class or not).
Default constructor exists by default, it is not generated. If class doesn't have any explicitly defined constructor it always have default parameterless constructor. If you specify any constructor elsewhere (partial class) default parameterless constructor doesn't exist any more and you have to create it yourselves if you want to use it (EF always wants to use it).