- How do you use many-to-one mapping to maintain a one-to-one relationship ?
The many-to-one mapping can be used to represent a one-to-one mapping, in this case a foreign key is used with the unique contraint as true. This way the foreign key can take only uquiue value so a one-to-one relation is maintained.
- What is bi-directional and uni-directional relationship between entities and how do you mention it in your classes ?
Both bi-directional and uni-directional is a technquie used to locate related row of entity given. If we can get the related row of one entity for the given entity only in one direction then it is called uni-directional, But if we can locate a related entity for a given row in both the direction then it is called bi-directional. These relationship helps us to locate an item with easy. For example if we have two entities related in bi-directional then we can save any entity to the database ( since they are related , both of the entity will be saved) , and we can retrive the other entity we did not save ( in coding wise ) and retrive the saved entity. For example take that X and Y entities are related in bi-directional way , then we can do like session.save(xRef); and retrive session.get(Y.class,1) , which gives us the Y entity , but by using yRef.getX() we can retrive the X.
- Why is it required to have a no-arg constructor to have atleast default access specifier ?
Hibernate uses reflection to instantiate your object , having no default constructor in your pojo would make the construction of the pojo object impossible by using reflection API. Hibernate also creates a proxy object to improve the performance of the system.
- How do you create many-to-many relationship between two tables in a database ?
Many database management systems , you can't create a many-to-many relationship without introducing a bridge or a link table some also call it a cross reference table. This table contains atleast two primary keys from both the tables.
- How/where do you configure hibernate (create the session factory ) in a web application ?
- What is the deployment structure of hibernate with a web application ( consider any plain servlet/jsp , or struts or spring mvc) ?
- What is detached objects in hibernate ?
- Is it mandatory to have the hibernate objects implement Serializable ? Justify your answers.
Hibernate doesn't require the entity to be serializable , however your application may need it, while transferring the entity object from one JVM to other, for example when entity classes treated as DTO ojects, RMI value passing to the client or storing the value in the httpsession object.
- What is the implecations having two entities estalished with bidirectional relationships ?
- Why it is considered a best practice to have the setter of the id to be private ?
- Are sessions in hibernate thread safe ?
- How many different ways can you configure mappings ?
The following three ways are possible.
> Hibernate legacy mapping, the regular *.hbm.xml file
> Java annotations JPA based.
> JPA based deployment descriptor ( xml )
- What is the differnece between persist and save method ?
- What is the difference between one-to-many vs many-to-one ?
- How many SessionFactory objects are needed if you use more than one database in your application ?
You need to create more than one SessionFactory,each can be cofigured in differnt cfg files.
- Do you need to override the equals and hashCode method in your entity classes?