Using @ManyToOne annotation in JPA

Annotation @ManyToOne in JPA is used to express multiple-to-one relationships between two tables in a database. There are many records in table A related to a record in table B. For example: many different students may have the same class. In this tutorial, we will work together to find out more about this annotation.

To illustrate, I will take the relationship between students and classrooms as examples. The database structure of these two tables is as follows:

OK …

First, I will create a project to work with this example:

Using @ManyToOne annotation in JPA

with Hibernate dependency as follows:

Because in this example, I work with MySQL so I will also add dependency for MySQL Driver:

Next, I will create two entities that represent 2 tables clazz and student.

Using @ManyToOne annotation in JPA

The content of the Clazz entity is as follows:

and entity Student:

Now, we will use the @ManyToOne annotation to show the many – one relationship between the Student and Clazz entity.

Because, this is a multiple-view relationship from a student object, so we’ll put the @ManyToOne annotation in the Student entity.

I will add a new field in the Student entity to use with the @ManyToOne annotation as follows:

Here, I only use one annotation @ManyToOne without any other configuration. Therefore, the default configuration for this annotation will be used. I will tell you what these default values ​​are?

As you know, in any database, the concept of relationship means that a table in this database has a link to another table in the database. And when a column in this table references to a primary key of the other table, we call this is a foreign key column.

In JPA, if you only use the @ManyToOne annotation, the name of the foreign key column will be defined with a default name. This default name will be based on two entities. It is the name of the variable that defines the relationship between the two entities with the annotation @ManyToOne. In our example, the clazz variable in the Student entity, followed by the underscore and finally the name of the variable acting as primary key in the other entity. In this example, it is the id variable in the Clazz entity.

Thus, in our example, this foreign key column will be named clazz_id in the student table.

In case, the foreign key column in the student table does not match the default name defined by JPA, then you need to use another annotation named @JoinColumn to define the foreign key column. In this annotation, we have an attribute named name to define the name of the foreign key column. In our example, we can add @JoinColumn annotation as follows:

Currently, in the student table we have defined the foreign key column clazz_id is the same as the default name, so now we can use the two entities above.

To work with JPA, we need to add a configuration file for JPA, persistence.xml, located in the /src/main/resources/META-INF directory of the project.

Using @ManyToOne annotation in JPA

with the following content:

Now, I will write code to add a class with two students as follows:

Result:

Using @ManyToOne annotation in JPA

As you can see, when you insert data into the database, the id of class A is automatically set to the value of the clazz_id column in the student table.


Add Comment