How to create an Index on your Entity Beans?

An index is a data structure such as B-Tree that improves the speed of data retrieval on a table at the cost of additional writes and storage to maintain it. When using plain SQL, to add an index for a column or a set of columns, you use the CREATE INDEX statement as follows: CREATE INDEX … Read more

How to lazy load your Entity relations ?

This tutorial will teach you how to configure and optimize the fetch type strategy used in your Jakarta EE / Hibernate applications. In JPA terms, the FetchType strategy defines strategies for fetching data from the database. The default FetchType depends on the cardinality of the relationship. Here is a quick summary of defaults: OneToOne: EAGER … Read more

How to disable updates for a field of an Entity EJB?

If you need to select which columns need update when executing a CMP statement you can use the “updatable” attribute on the @Column annotation. Setting “updatable” = false will assume that the column is not always included in the SQL update statement. Here is an example: import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import … Read more

One to One Hibernate/JPA Example

In this tutorial we will demonstrate how to set up a One-to-One Mapping using annotations and MySQL as database. We will use a Worker table which is mapped one-to-one with an Info table which contains the info about the workers. In a one-to-one relationship, each row in one database table is linked to 1 and … Read more

One to Many Hibernate/JPA example

Here’s an example which uses a One-to-Many mapping between the Table Customer (ONE) and Request (MANY).   The table has been created on a MySQL database: mysql> desc customer; +————–+————–+——+—–+———+—————-+ | Field        | Type         | Null | Key | Default | Extra          | +————–+————–+——+—–+———+—————-+ | id           | int(11)      | NO   | PRI | NULL    | … Read more

How to map an Oracle Sequence with JPA

This tutorial describes how to map an Oracle Sequence with JPA. Supposing that you have just created an Oracle sequence using the command: CREATE SEQUENCE AGENDA_SEQ; Then, you can use this sequence in your Entity as follows: @Entity public class Agenda {      @Id @GeneratedValue(strategy=GenerationType.AUTO, generator=”my_entity_seq_gen”) @SequenceGenerator(name=”my_entity_seq_gen”, sequenceName=”AGENDA_SEQ”, allocationSize = 1) private Long id; … Read more

How to map MySQL auto increment field

MySQL does not have the concept of sequence but uses AUTO_INCREMENT fields to emulate sequence behaviour. Supposing that you have just created this table: mysql> create table customer (id integer auto_increment, name varchar(25));Query OK, 0 rows affected (0.03 sec) Then you can map the id field using a Generation type IDENTITY: @Entity @Table public class … Read more

Hibernate Data Filters

Filtering data is one of the most common Task of an application. This is usually achieved by adding WHERE clauses to your queries. In case the filters are defined dynamically by the user it becomes a more complex issue. A very powerful option for creating dynamic conditions to your applications are Criteria API, however it’s … Read more

How to enable cascade delete/update?

One of the key features of relational database is referential integrity which s maintained by the system, by taking certain actions when referential constraints are violated. So for example what happens when: 1) A row with a referenced primary key is deleted from the referenced table2) A referenced primary key is updated in the referenced … Read more

Embedding Entities in other Entities

In this tutorial you will show how to map composite components using Hibernate/JPA Annotations.  Consider the following relationship between Worker and WorkerInfo Entity. According to the relationship each Worker should have a unique WorkerInfo. So, since the two Entities are strongly related (composition relation), it is better to store them in a single table (named … Read more