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.
- 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 the process of organizing data in a relational database to reduce data redundancy and eliminate insertion, update, and deletion anomalies by decomposing tables while preserving data integrity.
Terminology
- Attribute – A column in a table.
- Candidate Key – A minimal set of attributes that uniquely identifies a row. A table can have multiple candidate keys; one is chosen as the Primary Key.
- Prime Attribute – An attribute that is part of any candidate key (not just the primary key).
- Non-prime Attribute – An attribute that is not part of any candidate key.
- Super Key – Any set of one or more attributes that uniquely identifies a row. A candidate key is a minimal super key.
- Functional Dependency (A → B) – Attribute B is functionally dependent on A, meaning each value of Adetermines exactly one value of B.
- Transitive Dependency – If A → B and B → C, then A → C. Here, C is transitively dependent on A.
Normal Forms
1NF (First Normal Form)
- Every attribute contains atomic (indivisible) values.
- No repeating groups or arrays.
2NF (Second Normal Form)
- Must already be in 1NF.
- Every non-prime attribute must be fully functionally dependent on the entire candidate key (i.e., no partial dependency on part of a composite key).
3NF (Third Normal Form)
- Must already be in 2NF.
- No transitive dependency of a non-prime attribute on a candidate key.
BCNF (Boyce-Codd Normal Form)
- For every functional dependency X → Y, X must be a super key.
| Normal Form | Example |
|---|---|
| 1NF | Employee(EmployeeId, Name, PhoneNumbers) → PhoneNumbers = "9876,9123" violates 1NF. Store one phone number per row or in a separate table. |
| 2NF | Sales(OrderId, ProductId, ProductName, Quantity) with composite PK (OrderId, ProductId) → ProductName depends only on ProductId, not the full key, so move it to a Product table. |
| 3NF | Employee(EmployeeId, Name, DepartmentId, DepartmentName) → DepartmentName depends on DepartmentId, not directly on EmployeeId, so move DepartmentName to a Department table. |
| BCNF | EmployeeProject(EmployeeId, ProjectId, ManagerId) where ManagerId → ProjectId and EmployeeId + ProjectId → ManagerId → Since ManagerId is not a super key, this violates BCNF and should be decomposed. |
Easy way to remember
1NF: No multiple values in a column.
2NF: No partial dependency on a composite key.
3NF: No transitive dependency.
BCNF: Every determinant must be a super key.
No comments:
Post a Comment