- What is Business key equality ?
The set of fields in the Class ( the corresponding keys in the table) that would uniquely identify an instance ( a row in db or an entity object managed by hibernate)
- Is it a good idea to base your equals and hashCode on the field id ( primary key corresponds in the table) ?
No, the hashCode and equals should not be based on the field id , or identifier of the class , since hibernate only assigns the id ( generated primary id ) only when it is saved. Explain more....
- Will two objects retrived from two different session be ever evaluate to true if == is invoked on them ?
- Why hibernate mandates the collections to be conforming to program to interface , for example private Set students , instead of private HashSet students ?
- Does hibernate needs a public mutators ( setters/getters ) ?
No, Hibernate doesn't mandate the classes to have a setter/getters to be public, by making the access="field" we can tell hibernate that always use fields directly.But as a good design , and convinient you should provide the public setters/getters.This way if you need to access the fields once hibernate loads from database. Hibernate uses reflection to get access to the property incase access="field" is mentioned in the property tag.
- What is a derived property and how do you write it ?
A derived property is a field in your class file( Entity) ,which is not mapped to any column , but it has the ability to do calculation or a derived value from existing columns, for example salary to exepeience ratio in an employee table,where salary and experience is two columns and salToExperienceRatio is derived by deviding the salary and experience. The following mapping illustrates this.
<property name="salToExperienceRatio" formula="SALARY/EXPERIENCE" type="big_decimal" />
while the name maps to an non-exiting column in the database , it is present in the entity class,and the formula is an sql expression (funtions , subselect is possible) taking columns and operand. Since the column doesn't exists in the database , this field doesn't take part in the update or insert into the database,but only the select/quering.
- It is a good idea to private scope the identifier fields mutators (setId and getId) ?
No, there is a good chance your application would need the persisted id of the entitty. But on the other hand setId field can be marked private , so that hibernate is responsible for setting this id when it is persisting it.
- How is database identitfy is different from java identitfy.
Java identity comes in two versions Object identity which is == and equality which is equals method. However database identity refers to the primary key identity , which in ORM world is obj.getId().equals(obj2.getId()). The other apporach to do database identity is to let hibernate manage it by telling hibernate <id column="STUDENT_ID">
</id>, here we have not mentioned the id field as name , which tells hibernate to manage the id internally, and get the identifier by Long catId = (Long) session.getIdentifier(student); but this has a drawback that hibernate would not handle the detached object effectively.
- What is the difference between natual primary key and a surrogate key ?
The definition of primary key says that it uqniquely identifies a row, can't be null, and must be unique. For example a phone number , can be a primary key, roll number of a student can be a primary key. With the above cases , as you can see a natural primary key, which has a meaning to each of the rows in the table. A phone number can identify a row as well as it has a meaning to the student entry ( row) , and most of the time it is not generated by the database. But a surrogate key on the other hand doesn't mean anything to the row or entity. It is a key generated by the database.
- When to use a component mapping ? What is a component , and why would you use it ?
A component is composition relation between the entity and the composed object.
- What is value type in hibernate ?
- How do you mean by association , aggregation and composition in ORM/hibernate and how do you maintain it in tables ?
- How do you maintain a bidirectional association in composition ?
- Which of the mapping type would you use if you have a column of fixed character length ?
When a fixed character lenght is choosen , better to user character as type with length attribute, however alternative is a string type which maps to varchar , but it will not an effective storage for fixed character length.