This course covers the Java Persistence API, or JPA, which is designed to persist data between Java objects and relational databases.
- Learn the fundamentals of the Java Persistence API
- Learn about entities, entity inheritance, and JPA queries
This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java Enterprise Edition.
Basic knowledge of Java programming.
Hello dear friends. In this video, we will continue examining entities, let's begin. Finally, we learnt about the use of embedded classes. Let's take a look at the element collection now. For example, an employee may have more than one phone number, and you have to hold them in another table. There are two ways. First, you can create another entity or you can create a new table in this entity with element collection. Of course, we will make it like the second one. First, we need a string list to hold our phone numbers, like this. With element collection annotation, we will create our new table. But also, we have to set these tables primary key to our new table. So, we will use collection table. As you can see in this code with collection table annotation, we can give new tables name and also with join columns, we can add primary key column of employee entity. With these entities, our database will be like this. And look, first employees ID is 1, and in phone table we can see this ID.
Now, for example, you want to add a field and entity, but you don't want to add this to the table. To make this, you can use transient annotation. If we add this new field with transient annotation, we won't see new field in our database like this, done. Now, let's go on with mapping. For example, we have two entities as employee and university. Each employee must have a university. So, we will adjust a one-to-one relationship. We have to add annotations in two entities. First, in employee. We will define a university instance. Now we need an annotation and we will join column for university ID. Now in university, we have to define an employee instance with one-to-one annotation, but we have to join column name from employee table. As you mean, these two names must be the same. Now after that, we have to add one-to-one annotation in employee also.
Now as you know, we cannot add primary keys. To make this addable, we have to use fetch property with lazy. After that, we can add. After these changes, our tables will be like this. And in employee table, we have a university ID that is the same with ID in university table. Entities that use relationships often have dependencies on the existence of other entity in the relationship. For example, a line item is part of an order. If the order is deleted, the line item also should be deleted. This is called a cascade-delete relationship. The Java X persistence cascade type enumerated type defines the cascade operations that are applied in the cascade element of the relationship annotations. Cascade operations for entities are detach, merge, persist, refresh, remove, all. If the parent entity is detached from the persistence context, the related entity will also be detached.
If the parent entity is merged into the persistence context, the related entity will also be merged. If the parent entity is persisted into the persistence context, the related entity will also be persisted. If the parent entity is refreshed in the current persistence context, the related entity will also be refreshed. The most important is if the parent entity is removed from the current persistence context, the related entity will also be removed. With all, all cascade operations will be applied to the parent entities, related entity. All is equivalent to specifying cascade, detach, merge, persist, refresh, and remove. For these operations, only we need cascade property in one-to-one annotation. For example, for remove, I will add cascade remove. That will be enough. There are four relationships between tables as one-to-one, one-to-many, many-to-many, and many-to-one. There is no difference in the usage of the annotations, so I don't need to show them, done. We have now finished entities. In the next video, we will look into entity inheritance. See you in the next video.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.