How to add conditionally annotations on jpa entity hibernate - spring

Have a situation where I need to use single JPA Entity class for different databases.
I have used #Nationalized annotation on fields to support I18N. I need this #Nationalized annotation applicable only when I use oracle as my database. If I use same code for other databases it should not be consider(eg: derby)
is there any mechanism to achieve this in hibernate

Related

Hibernate session factory and repository confusion

So there are two ways to persist an entity:
Using Hibernate's session factory where we get the current session and call save(), get(), update() methods.
Extending JPA's repository interfaces
I have the following questions:
How are these two methods different in the context of using Hibernate. As far as I understand, Hibernate is an implementation of JPA API. So when I say I want to use Hibernate, does it mean that I can use both of the above methods?
What is the preferred way of the two based on convenience, flexibility and optimisation?
JPA repository behind the scenes uses Hibernate or JPA APIs to implement its functionality and tries to "abstract" it or provide convenience methods on top of it. You don't have to use JPA repositories though, and can always switch to the Hibernate or JPA APIs when needed. Think though, if you really gain any benefit by using the Spring Data JPA repository concept.

Prevent specific #Entities to generate table - hibernate JPA

Is it possible to stop spring JPA from generating table for a specific entity ?
Reason:
I want to put all my native sql queries into a separate object using #NamedNativeQuery. But #NameNativeQuery requires #Entity annotation. Due to that, and unwanted table is generated automatically.
Is it possible to add an #Entity, and stop at the same time spring boot to generate table for that entity ?
P.S: I don't want to put the queries into the parent #entity, because there are too many queries. I want to have a good model structure.
My settings :
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=none
Spring Boot version : 2.3.3
Hibernate version : 5.6.7.Final

Object/Entity lifecycles when using spring-data-jpa with hibernate as jpa provider?

When using spring-data-jpa with hibernate as jpa provider, are the Object/Entity lifecycles same as when using hibernate directly or as defined by hibernate (or might be jpa spec itself).
Hibernate defines these lifecycles to entities - Transient, Persistent, Detached, Removed.
Are these same life cycles applicable when using spring-data-jpa too.
If so how does below the methods provided by Hibernate map with the methods of spring jpa crud repository.
//below methods in hibernate move an entity to persistent state
save(e),
persist(e);
update(e);
saveOrUpdate(e);
lock(e);
merge(e);
and
//below methods in hibernate move an entity to detached state
detach(e);
evict(e);
For the first part of the question:
Spring Data JPA just offers some comfortable mechanics on top of JPA.
The persistence, mapping and life cycle is still managed by JPA or its implementation, i.e. Hibernate in your case.
This means the life cycle is the same.
As for the mapping between Spring Data JPAs methods and Hibernates/JPA methods see the following table.
Spring Data
JPA
CrudRepository.save*
for new entities EntityManager.persist, EntityManager.merge otherwise
CrudRepository.delete*
EntityManager.remove
CrudRepository.findById
EntityManager.find*
JpaRepository.*flush
EntityManager.flush
JpaRepository.getById
EntityManager.getReference
Other query methods predefined in interfaces or otherwise use various types of queries.
Spring Data Jpa is only an abstraction layer and not provide a lifecycle management. Therefore, if you are using hibernate as a jpa implementation your object's lifecycle will regulated according to hibernate's lifecycle management.
Also, you can find some other explanations here and here as well.

Is Repository the only way to save entity data on DB?

I'm studying Spring boot, and i have create several entity, my problem now is to retrieve the data from controller to save it on db.
Surfing on the web I have learned that i have to use JPARepositery or CrudRepositery in this way:
An example with User entity
public interface UserRepository extends CrudRepository<User, Integer> {
}
and to save
User user = new User();
userRepository.save(user);
But if I have many entities, Do I need to create a repository for each entity?
I have read about Session, FactorySession and Transaction they are compatible with Spring boot? How it works? and #Transactional tag how it works?
Thanks in advance
Yes, usually you need to create one Repository per Entity. This is also a good practice because you are placing operations and queries related to the same Entity in the same Repository.
Yes, you can obtain a Session and a FactorySession object (Hibernate), but I would advice you against using Hibernate directy. Instead, it is better to use Spring Data or JPA mechanisms to access your database (just as you do using a Repository). The reason is because Hibernate is an implementation of the JPA standard and today we use JPA to access databases (because it masks the exact implementation of the ORM). This way you can later (potentially) replace Hibernate with another JPA implementation (such as EclipseLink) without the need to change your code (in theory). In most projects you will find Hibernate being used, however.
Yes, Transaction is a Spring annotation and an important database transaction mechanism in Spring Boot, too.

how to retrieve values from existing table using hibernate

I am using Hibernate & Spring In my project. I need to access already existing table values from DB using hibernate.
I just have an entity class & I use Constructor injection here.
I am Using Hibernate & spring in same file.
Hibernate allows to generate beans and mapping configuration out of existing databases. This is called `Reverse Engineering'.
You can find the documentation here:
http://docs.jboss.org/tools/latest/en/hibernatetools/html/reverseengineering.html

Resources