![]() ![]() Detached: An instance is detached, once the Hibernate session is closed.Ī session instance becomes serializable if its persistent class or classes is/are serialised.Persistent: An instance becomes persistent when it is associated with a session, has database representation and has an identifier value.Transient: When an instance of a persistent class is not associated with a session, has no database representation and has no identifier value, such an instance is considered to be in the transient state.The instances may be in one of the following states at any given point. The main functionality of Session objects is to provide create, read and delete operations to the instances of the mapped entity classes. It also provides factory methods for Transaction, Query and Criteria. The interface provides methods to insert, update and delete the object. Sessions hold a first-level cache of data. These objects should be created as needed and should be destroyed if not in use. Sessions are not thread safe and so should not be kept open for long. It is light-weight and is instantiated every time a database connection is required. ![]() The session object is used to connect with a database. If the row is not found, Hibernate throws an ObjectNotFoundException Hibernate will fire a query to the database for information on the student with primary key 1 and populates the object dia with the properties from the corresponding row. However, when a method is called on dia, String firstName = dia.getFirstName() Hibernate will create an uninitialized proxy for this entity with the id we have assigned it and will contain no other property as we have not yet communicated with the database. When the session.load() method is called to instantiate the Student, Student dia = session.load(Student.class, newLong(1)) Let us also assume that initially, this entity has no connection or relation with any other tables except the Student table. Let us assume that there is an entity called Student. When an entity method is called, the entity is loaded and the proxy becomes an initialized proxy object. The proxy implementation delegates all property methods except for the to the session which will in turn populate the instance. ![]() The session.load() method creates an uninitialized proxy object for our desired entity class. When a load() method is called on session, a proxy is returned and this proxy contains the actual method to load the data. ![]() Ī proxy is defined as a function which acts as a substitute to another function. When using a map for mapping, a map-key has to be provided along with the key column. There are different associations for mapping the class with the corresponding table. The Hibernate mapping element used for mapping a collection depends upon the type of interface. To map these collections, the type of collection must be declared from one of the following: There are five types of collections in Hibernate. Collection instances are like value types and are automatically persisted when referenced by a persistent object and deleted when unreferenced. The Hibernate collections behave like HashMap, HashSet, TreeMap, TreeSet or ArrayList. This is a frequently asked question in hibernate interview questions for freshers. The hbm mapping for the above class would be: In the above example, the annotation refers to the MONTHLY_SALARY column in the database table and not the monthlySalary property in the class. A simple example of a POJO class is as below: public class Student Use of POJOs instead of simple java classes results in an efficient and well – constructed code. An ID is mandatory for each object of these classes as they will be mapped to the primary column of the table. These classes should be non-final or have an interface with all the public methods declared. These classes also have a default constructor. These classes have proper getter and setter methods for every property. Such classes whose objects are stored in a database are known as persistent classes. Hibernate is based on the concept of taking values from Java class attributes and storing them in a database. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |