Hibernate

What is joined-subclass element in Hibernate ?

If you don’t want to use the recommended inheritance mapping, you can choose the table-per subclass mapping. Each subclass must have its own <joined-subclass> element. You include all the usual elements like <property>, <key>, and so on for this subclass. The format of the element is as follows:

<joined-subclass name="name"
    proxy="interface" [optional]
    lazy="true | false" [optional]
    dynamic-update="true | false" [optional - defaults to false]
    dynamic-insert="true | false" [optional - defaults to false]
    <key/>
    <property/>
/>

What is collection element in Hibernate ?

If you’re using Java collections of the type Map, Set, SortedMap, SortedSet, List, Collection, and arrays, Hibernate can persist them for you like other Java objects. For all the collections that can be mapped, there are appropriate elements: <map>, <set>, <list>, <array>, and <primitive-array>. The format of the elements is:

<map | list | bag name="name"
    table="table" [optional - defaults to name value]
    schema="name" [optional]
    lazy="true | false" [optional - defaults to false]
    inverse="true | false" [optional - defaults to false]
    cascade="all | none | save-update | delete | all-delete-orphan"
    [optional - defaults to none"
    sort="unsorted I natural I class" [optional]
    order-by="name asc | desc" [optional]
    where="sql where string" [optional]
    outer join="true ( false I auto" [optional]
    batch-size=="#" [optional -defaults to 1]
    access="field I property I class" [optional - defaults to property]
    <key/>
    <index/>
    <element/>
/>

How does Hibernate determine to access attributes of Java class ?

Hibernate uses reflection to determine how to access attributes of the Java class.

How to create a persistence class in Hibernate ?

Hibernate will only persist attributes specified in the mapping document; therefore you can include in your class as many temporary attributes as needed when you do not want to save attribute’s value to the permanent storage.

All attributes that will be persisted should be declared private and have setter/getter methods defined in the JavaBean style.

Hibernate can handle mapping from just about any data type. If you are using Java primitives and JDK types, use the type, and refer to the Hibernate types.

If an attribute type is a collection like a Map, List, or Set, you’ll need to do additional work when creating the mapping.

Mapped classes can use the concept of composition, where an attribute’s type is another class.

All classes should contain an ID in order to allow easy identification of your objects within Hibernate and the database.

Interfaces, final classes, and some inner classes can be mapped to permanent storage using Hibernate.

All Java classes that will be persisted need a default constructor.

How to make a class immutable or read-only in Hibernate ?

Set attribute mutable=”false” to the class element in the mapping document.

For example,

<hibernate-mapping>
    <class name="com.roytuts.Products"
        table="products" mutable="false">
        ...
    </class>
</hibernate-mapping>

What is inheritance in Hibernate ?

Please read https://roytuts.com/inheritance-strategy-in-hibernate/

What are the lock modes available in Hibernate ?

Hibernate provides the lock() method to perform the reattachment of the object with a new session.

The available lock modes are as follows:

LockMode.NONE: No lock occurs with the row in the database. Thus any thread can make a change to the object.

LockMode.READ: A read lock occurs on the object.

LockMode.UPGRADE: A lock occurs on the object row, and the in-memory object is updated with the object information in the database table.

LockMode.UPGRADE_NOWAIT: An upgrade lock is attempted using a SELECT FOR UPDATE NOWAIT.

LockMode.WRITE: A write lock is obtained when the object is either inserted (for a new object) or updated.

Some locks are more expensive than others, and you should take care when obtaining a lock. Depending on your choice, some threads using the same object may be blocked from updating the database.

What is flush() method in Hibernate ?

At any time during the processing of objects, you can call the flush() method on the Session object.

During the execution of the flush, all queued SQL statements are executed. When you persist or update an object, the work does not automatically occur in the database; this is part of Hibernate’s caching mechanism.

When the flush() method executes, all the SQL statements are executed against the database.

You can call flush method at any time. Hibernate executes the flush method automatically when a commit() method is called as well as during the find() and iterate() methods.

What is refresh() method in Hibernate ?

At any time during the execution of your application, you have the option of reloading or refreshing the object from the database. The refresh() command does the job:

public void refresh(Object object);
public void refresh(Object object, LockMode mode);

You could use the refresh in a multiuser situation where another user of the system might be updating the same object and you are not part of a transaction. In this type of situation, two threads of an application have objects based on the same row in the database. If one thread updates the object in the database, the other thread is out of sync. The thread can reload the object to be sure it has the latest information.

What is a scalar query in Hibernate ?

In Hibernate the result of a query is an object or a list of objects. Hibernate lets you return not only a complete object previously stored in the database but also individual attributes or groups of attributes. This kind of query is called a scalar query.

What is uniqueResult() method in Hibernate ?

You can use this method to test the database and whether objects based on a query are available. The method returns a null value if no results are available for a particular query or if a single object is returned.

What is Named Query in Hibernate ?

We can define query string in the mapping document in the following way:

<query name="find.users.access.greaterthan.50">
    <! [CDATA[from users user where user > 50]
    ]>
</query>

We use the CDATA section designator to contain our query string: This is required if your query contains any characters that would be considered XML markup.

Once the query has been named in the mapping document, we need to use it in the application code. This is accomplished as follows:

Session session = SessionFactory.openSession();
Query query = session.getNamedQuery("find.users.access.greaterthan.50");
List users = query.list();
SessionFactory.closeSession();

Or

<sql-query name = "empdetails">
   <return alias="emp" class="com.test.Employee"/>
      SELECT emp.EMP_ID AS {emp.empid},
                 emp.EMP_ADDRESS AS {emp.address},
                 emp.EMP_NAME AS {emp.name}
      FROM Employee EMP WHERE emp.NAME LIKE :name
</sql-query>

Invoke Named Query :

List people = session.getNamedQuery("empdetails")
             .setString("John", name)
             .setMaxResults(50)
             .list();

What is Named Parameter in Hibernate ?

You can also replace the placeholders with named parameters. Named parameters are string values starting with a colon character :. For example, here’s a named query using the named parameter:

<query name="findnusers.access.greaterthan">
    <! [CDATA
    [from users user where user > :accesses]
    ]>
</query>

Instead of putting in an actual value or using the ? placeholder, we have added the named parameter :accesses to the query. The code to use the query is:

Session session = SessionFactory.openSession();
Query query = session.getNamedQuery("find.users.access.greaterthan.50");
query.setlnt("accesses", 100);
List users = query.list();
SessionFactory.closeSession();