Monday, November 11, 2019

Hibernate Interview Questions - Part 1



TYPE OF ASSOCIATIONOPTIONS/ USAGE
One-to-oneEither end can be made the owner, but one (and only one) of them should be; if you don’t specify this, you will end up with a circular dependency.
One-to-manyThe many end must be made the owner of the association.
Many-to-oneThis is the same as the one-to-many relationship viewed from the opposite perspective, so the same rule applies: the many end must be made the owner of the association.
Many-to-manyEither end of the association can be made the owner.
https://stackoverflow.com/questions/33096466/generationtype-auto-vs-generationtype-identity-in-hibernate


Hibernate Basic Configuration with spring boot

Open application.properties file and add the following properties to it.


## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/notes_app?useSSL=false
spring.datasource.username = root
spring.datasource.password = root


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update


Mappings :

Hibernate one to one mapping with foreign key association

foreign key association one to one

EmployeeEntity.java
@OneToOne
@JoinColumn(name="ACCOUNT_ID")
private AccountEntity account;

AccountEntity.java
@OneToOne(mappedBy="account")
private EmployeeEntity employee;

In a bidirectional relationship, one of the sides (and only one) has to be the owner. The owner is responsible for the association column(s) update. To declare a side as not responsible for the relationship, the attribute mappedBy is used. ‘mappedBy’ refers to the property name of the association on the owner side.

Hibernate one to many mapping annotation example

1. Hibernate one to many mapping with foreign key association

one To Many association in hiberate using foreign key

EmployeeEntity.java
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="EMPLOYEE_ID")

private Set<AccountEntity> accounts;


AccountEntity.java
@ManyToOne
 private EmployeeEntity employee;


2. Hibernate one to many mapping with join table

one To Many association in hiberate using join table

EmployeeEntity.java
@OneToMany(cascade=CascadeType.ALL)
    @JoinTable(name="EMPLOYEE_ACCOUNT", joinColumns={@JoinColumn(name="EMPLOYEE_ID", referencedColumnName="ID")}
    , inverseJoinColumns={@JoinColumn(name="ACCOUNT_ID", referencedColumnName="ID")})
    private Set<AccountEntity> accounts;

AccountEntity.java
No changes required


Hibernate many to many mapping annotation example

many-to-many-hibernate-mapping

ReaderEntity.java
  @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name="READER_SUBSCRIPTIONS", joinColumns={@JoinColumn(referencedColumnName="ID")}
                                        , inverseJoinColumns={@JoinColumn(referencedColumnName="ID")})
    private Set<SubscriptionEntity> subscriptions;


SubscriptionEntity.java
 @ManyToMany(mappedBy="subscriptions")
    private Set<ReaderEntity> readers;




Useful Link

https://javarevisited.blogspot.com/2013/05/10-hibernate-interview-questions-answers-java-j2ee-senior.html

Hibernate Interview Questions and Answers


Ehcache - Second level caching

https://dzone.com/articles/spring-hibernate-ehcache-caching

https://www.devglan.com/spring-mvc/spring-ehcache-cacheable-example-javaconfig]

https://www.devglan.com/spring-mvc/spring-ehcache-cacheable-example-javaconfig


N+1 problem solution

https://dzone.com/articles/how-identify-and-resilve-n1

https://www.javacodemonk.com/what-is-n-1-problem-in-hibernate-how-will-you-identify-and-solve-it-894097b9



No comments:

Post a Comment