Skip to main content

Posts

Showing posts with the label SpringDataJPA

SpringDataJPA : One to Many Mapping in Spring Boot Hibernate JPA with Spring Data

Also Read One To One Mapping Let's create two entities/domains Employee and Department such that they have the following relation between them. Department has-many Employees which means an Employee can belong to only one Department and a Department can have many Employee and Department is the owner of the relation between the two. Owner of the relation means that Owner can exist without the dependent entity but dependent entity cannot stay without the owner entity . Dependent Entity of Relationship will containes the 'foreign key' ID of the Owner entity . In this case, Address will contian the Employee Id in >its table as shown in the table structure below. So a Department can exist without Employee but an Employee cannot be there without a Department. If you on deleting an Employee, the Employee-Department mapping will be removed on deleting a Department, all the employees in the department should be deleted. Employee.java package com.ekiras.domain; import jav...

SpringDataJPA : One to one Mapping in Spring Boot Hibernate JPA with Spring Data

Also Read One To Many Mapping Let's create two classes Employee and Address , such that they have the following relation between them. Employee has-a Address which means a one-to-one mapping between the two and Employee is the owner of the relation. Owner of the relation means that Owner can exist without the dependent entity but dependent entity cannot stay without the owner entity. Dependent Entity of Relationship will containes the 'foreign key' ID of the Owner entity . In this case, Address will contian the Employee Id in its table as shown in the table structure below. This means that Employee can stay without an Address but Address cannot stay without the Employee. In even more simpler words, If employee is deleted his address should also be deleted, but if address is deleted employee should not be deleted. Employee.java package com.ekiras.domain; import javax.persistence.*; import java.util.Date; /** * @author ekiras */ @Entity public class Employee { ...

SpringDataJpa : How to override the domain mapping defined in Parent Entity class with MappedSuperclass

Points To Remember Your Parent class should be annotated with @MappedSuperclass . Follow the Tutorial : How to handle Inheritence with Entities to know how to wrap common properties of entities to a base class. To override any property you must a. Apply the @AttributeOverride annotation on the class that need to override the property b. set name property of @AttributeOverride as the name of the field in super class. c. set the column property of @AttributeOverride to override the column definition of the attribute. Let's say our Base class looks as follows package com.ekiras.domain.base; import javax.persistence.*; import java.util.Date; /** * @author ekiras */ @MappedSuperclass public abstract class BaseDomain { @Id @GeneratedValue (strategy = GenerationType.AUTO) protected long id; @Temporal (TemporalType.TIMESTAMP) protected Date dateCreated; @Temporal (TemporalType.TIMESTAMP) protected Date lastUpdated; @Override public String toString (...

SpringDataJpa : How to handle inheritance with Entities

Points to Remember Mark your Base Entity class with annotation @MappedSuperclass . Define all common fields and their getter setters in this class. Make the base class abstract. Make all fields as protected so that they can be accessed in inheriting class without getters and setter. You can also define @PrePersist and @PreUpdate in this class. Fields that should be in the Base Entity Your Base Entity class should have only those field that need to be common for all your entities that will inherit this class. Sample Base Entity that you should use might look as follows package com.ekiras.domain.base; import javax.persistence.*; import java.util.Date; /** * @author ekiras */ @MappedSuperclass public class BaseDomain { @Id @GeneratedValue (strategy = GenerationType.AUTO) protected long id; @Temporal (TemporalType.TIMESTAMP) protected Date dateCreated; @Temporal (TemporalType.TIMESTAMP) protected Date lastUpdated; @Override public String...