This blog is just for revision .
MySqlDialect , Oracle Dialect ,Db2 Dialect.
When hibernate loads an entity it keeps a copy of it in the memory, on tx.commit it compares the entity and the copy . If there are any changes, update statement is executed in the database.
What is the difference between session.save and session.saveorupdate
save will insert the record if the Id is not available and throw exception if the Id is available. Saveorupdate will insert the record if the Id is not available, else it will update it.
commit commits the record to the database .
cr.list();
Cr.add( Restrictions.eq("id" , 10));
Cr.setProjection( Projections.max("id"));
Entities :
@ oneToMany cascade = Cascade Type.ALL
@oneToMany fetch = Fetch type.Lazy
2 phase commit can be handled using JTA only .
1. Update : multiple rows needs to be updated to update a data .
2. Insert : cannot insert a row without supplementary data.
3. Delete : unable to delete a data as this will cause the important information to be deleted.
Normalization is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly.
- What is ORM ?
- Explain Core Classes in hibernate .
- Configuration : load xml file .
- SessionFactory : thread safe
- Session : get connection with database , not thread safe , used to perform CRUD operations .
- Transaction : commit and rollback operations . unit of work.
- Query : common query language for any type of database.
- Criteria : add restrictions and projections
- Explain Hibernate configuration file
contains database details used to initialize session factory .
- Explain Hibernate Mapping file
contains table and column details also contains the entity bean mapping .
- Explain state of the entity in the persistence context .
- transient : newly created object
- persistent : associated with session ( session.save)
- detached : session closed
- How to use hibernate in spring ?
- the datasource bean DriverManagerDataSource
- specify the username /password or jndi
- sessionfactory bean LocalSessionFactoryBean
- transaction bean HibernateTransactionManager
- tx:annotation driven
- What is hibernate dialect.
MySqlDialect , Oracle Dialect ,Db2 Dialect.
- What are the different consistency anomalies in hibernate .
- dirty read .reading uncommitted data
- non repeatable read : second read returns different values for a row
- phantom read : second read returns different number of rows
- Explain isolation level .
- Read uncommitted
- Read committed :
8. synchronized : does not allow concurrency
Isolation level is set using property hibernate.connection.isolation
- Explain the locking strategy based on isolation level .
- Read uncommitted : no lock
- Read committed : lock on committed data
4. Repeatable Read : lock on a block of SQL
8. Synchronized : full lock on table .
- In which scenario repeatable read will return different data .
- What is Caching in hibernate ?
- First level Catch : session cache of hibernate , enabled by default
- Second Level Cache : optional , eg : Ehcache
- Query Level Cache : cached query results . set hibernate.cache.use_query_cache property to true.
- What are the concurrency strategies for second level cache?
- Transactional : synchronized with the database .
- Read-write : if the transaction is not committed , the entity is fetched from database and referred from the cache .
- non strict read write : cache updated after data committed
- read only : used when data never gets changed .
- What is dirty checking?
When hibernate loads an entity it keeps a copy of it in the memory, on tx.commit it compares the entity and the copy . If there are any changes, update statement is executed in the database.
- Explain session.get
- Hits the database immediately.
- If record is not available in the database it returns null.
- Explain session.load
- Returns proxy object without hitting the database.
- If the record is not available and when we try to access it through the proxy object we get Object Not Found Exception.
- What is the difference between get and load ?
- What is the difference between save and persist ?
What is the difference between session.save and session.saveorupdate
save will insert the record if the Id is not available and throw exception if the Id is available. Saveorupdate will insert the record if the Id is not available, else it will update it.
- What is the difference between update and merge ?
- What is the difference between session.flush and transaction.commit ?
commit commits the record to the database .
- Explain Criteria
cr.list();
- Explain Restrictions
Cr.add( Restrictions.eq("id" , 10));
- Explain Projections
Cr.setProjection( Projections.max("id"));
- What is Declarative transaction management ?
using @Transactional :
- What is Programmatic transaction management ?
- Explain Different transaction manager used in spring .
- DataSourceTransactionManager : uses datasource
- HibernateTransactionManager : uses sessionfactory
- JpaTransactionManager : uses entitiymanagerfactory
- Explain transaction propagation ?
- REQUIRED : (default) if transaction is available use it otherwise create a new one.
- REQUIRES_NEW : create a new transaction , commit / rollback is independent .
- NESTED : uses outer transaction but creates a savepoint to allow partial rollback and commit .
- What is readOnly in @Transactional ?
- What are Value Types .
- Basic
- Composite / Embedded
- Collection : for one to many .
- Explain Many to Many .
Entities :
Person : on projects set attribute add
- @ManyToMany
- @JoinTable ( name=PersonProject , joinColums=@JoinColum , inverseJoinColumns=@JoinColum )
- @ManyToMany(mappedBy = "projects")
- Explain One to Many
Entities :
- Explain Cascade type :
@ oneToMany cascade = Cascade Type.ALL
- All
- Persist
- Merge
- Refresh
- Remove
- Detach
- How to lazy load the mapped classes
@oneToMany fetch = Fetch type.Lazy
- What are the fetching strategies ?
- Join Fetching
- Select Fetching
- Sub select Fetching
- Batch Fetching
- How to handle multiple databases ?
- @Table (schema= "")
- create two sessionfactory , use @Qualifier
- How to handle distributed databases ?
- What is the difference between jdbc and JTA?
2 phase commit can be handled using JTA only .
- Explain different ways of creating EntityManager ?
- Container Managed :
EntityManager entityManager;
2. Application Managed
EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.model.student");
- Explain Anomalies in DBMS
1. Update : multiple rows needs to be updated to update a data .
2. Insert : cannot insert a row without supplementary data.
3. Delete : unable to delete a data as this will cause the important information to be deleted.
- Explain normalization.
Normalization is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly.
Terminology :
1. attribute : column
2. candidate key : primary key ( simple or composite )
3. prime attribute : column which is part of primary key
4. non prime attribute : column not part of primary key .
5. super key : any set of attributes which identifies a unique record .
6. functional dependency : B depends on A
7. transitive dependency : B is dependent of A . C is dependent of B . So C has a transitive dependency on A .
1NF : attribute should be atomic.
1NF : attribute should be atomic.
2NF : non prime attribute should not depend on a subset of primary key
3NF : non prime attribute should not have transitive dependency with the super key .
BCNF : any functional dependency should be a super key .