What's the difference between #CreationTimestamp and #CreatedDate in Spring boot jpa? - spring

#Entity
public class Foo {
#CreatedDate
private LocalDateTime createdDateTime;
#CreationTimestamp
private LocalDateTime creationTimestamp;
}
In Spring boot entity class
I don't know which one to use
What's the different?

#CreatedDate is a Spring annotation and covered in the reference documentation. It is applicable to all stores covered by Spring Data: JPA, JDBC, R2DBC, MongoDb, Cassandra and so on.
#CreationTimestamp is a Hibernate annotation covered in the reference documentation. It is applicable to Hibernate only.
As far as what they actually do they are pretty much the same with the Spring variant supporting more data types.

Related

#Transient - Why data is getting saved in Database?

I have an entity variable annotated with #Transient like below. Which means it should not be stored in the Database.
#Transient
private String passwordConfirm;
But when I go to H2-Console, I can see the data is saved there.
Why so? and How can I avoid it?
You're probably using #org.springframework.data.annotation.Transient.
Change it to the right import: #javax.persistence.Transient
This will do the job.
#javax.persistence.Transient is used by the persistence provider (eg.: Hibernate). The persistence provider looks for JPA spec annotations. #org.springframework.data.annotation.Transient is not a part of the JPA spec, so, the persistence provider ignores it.
The #org.springframework.data.annotation.Transient is intended to be used when Spring Data is the actual ORM. Some examples are Spring Data Elasticsearch and Spring Data MongoDB. These Spring Data implementations use the #org.springframework.data.annotation.Transient just like Hibernate uses the #javax.persistence.Transient - not mapping the marked field into the database.

Spring Boot JPA 2.0.4 Auditing broken?

I upgraded my app from Boot 2.0.3 to 2.0.4 and now #CreatedDate and #LastModifiedDate from JPA Data Auditing are null rather than containing dates.
DB config contains #EnableJpaAuditing and the model contains
#EntityListeners(AuditingEntityListener.class)
...
#CreatedDate
private Date createdAt;
#LastModifiedDate
private Date updatedAt;
Worked as expected prior to the upgrade.
Any ideas?

How to autowire NamedParameterJdbcTemplate in Spring Boot

I understand that Spring Boot automatically configure JDBCTemplate for us. But i don't know exactly which way is correct when i want to autowire NamedParameterJdbcTemplate.
private NamedParameterJdbcTemplate template
or
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
P.S : i'm using Spring Boot 1.5.1 Release
I just found the answer by reading Spring Boot API, both ways are working and it does not depend on what we name it. Problem was solved.

Objects initialization in spring framework

i am studying spring framework i have some doubts to confirm:
I see that class objects are created as beans in xml file ... but my doubt is ... only pojo class beans need to be defined in xml for instatiation or all classes
eg: my custom code class EncryptionUtil class that helps in encrypting data and so on custom logic classes also need to be instantiated using beans?This is my primary concern
what about cases like i use
JSONObject j = new JSONOBJect() (External libs);
ArrayList<String> a = new ArrayList<String>();
( java default object and collections)
do these classes also need to have bean in xml?
i exactly dont know if spring ioc will instantiate each and every object or we need to instantiate only some objects
also, in spring app does "new " keyword work for creating objects
What should we use to instantiate bean in spring mvc ?
ie : like i used ApplicationContext in my spring app , should i get bean everywhere i need
will there be any problem if i use multiple annotations ie : of spring as well as hibernate on same class at same time?
eg: something like this
#Id #GeneratedValue
#Column(name = "id")
private int id;
but if i want id to be autowired too ...
#Autowired
#Id #GeneratedValue
#Column(name = "id")
private int id;
will this work?
Declare only classes as Spring Beans which you don't want to create with new. Further Spring Beans are normally singletons. This means there is only one instance of this class. In most cases Spring Beans are not POJOs. E.g mostly i declare DAOs, Service, Controller classes as Spring Beans.
No there is no need to declare these as Spring Beans.
Yes, new works. But these instances are not managed by the spring container. And you should only instantiate non spring beans with new. Spring Beans itself are instantiated by the Spring IOC container.
Inject beans to other beans with xml config or by annotation config (#Autowired).
You can mix spring annotations with annotations of other frameworks. But your example with the id doesn't work and makes no sense, since entity id's wont' be injected. Also your Hibernate entity must be also a spring bean (in this case declared as prototype). You can inject values and beans only to spring beans.

When to use Spring #Autowire annotation

Recently I had discussion with my friend regarding usage of Spring #Autowire annotation on entity(JPA) classes.
In our project we are using #Autowire annotaion to inject Entity but my friend suggesting not to use #Autowire annotaions on entity classes. When I asked why? He dont have the proper answer for that. So i just wanted to know are there any disadvantages using #Autowire annotaion on entity classes.
Also please explain when to go for #Autowire annotaion or not with example.
Thank in advance.
#Entity and #Autowire are not interchangeable.
#Entity annotation indicates that the JavaBean is a persistent entity.This is actually a JPA annotation and not a Spring Annotation.
#Entity will be used in the sessionFactory by the packagesToScan poroerty.
#Autowired: inject a resource by-type, i.e. by the class or by the interface of the annotated field or contractor. See my answer Inject and Resource and Autowired annotations
#Autowired is used to inject dependencies as an alternative to setting it via xml configurations
Maybe this answer will help you understand
Hibernate - spring annotated entities not scanned from within jar
UPDATE:
Following the comment bellow:
Company is your domain object, so you don't need to use spring in this case.
<bean id="company" class="xxx.Company"/>
The above will return the same instance with #autowire.
Even if you switch to scope="prototype" I don't see any reason to use spring for that.
You should have a service that will be used to CRUD company e.g.
CompanyService, this service will be a single tone so you will use #Autowire to inject it to the controller and it will use your JPA framework to implement CRUD's
To create a new company you will use:
Company c = new Company //this probably will be binded from your ui form
companyServic.saveOrUpdate(c);
See the following answer spring rest service - hibernate dao - annotations - pojo - namedqueries.
For common practice of DAO and services.
#Autowire is an annotation used to perform a dependency injection, its almost similar to the standard #Inject you can take a look at the spring reference manual to see the difference between those two annotations.
#Entity is a part of the jpa framework its used to mark a class as persistent, spring does not implement an equivalent annotation.

Resources