Skip to main content

Posts

Showing posts with the label JPA

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 (...

JPA : One to Many Mapping using Hibernate Spring Boot

STEP 1 :: Add the dependencies You need to add the following dependencies in your build.gradle file. compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-jdbc') runtime('mysql:mysql-connector-java') STEP 2 :: Make Entity classes You need to create two entity QUESTION and TOPIC. Here the Topic Entity will have an annotation @OneToMany , this indicates the following This one topic can have many questions. All the questions will be deleted or updated when topic is deleted/updated. Its mapping is defined by Entity Question by the mapping of topic variable. It will give a list of all question of a topic when it is fetched If you do not need questions when topic is fetched then make fetch type as LAZY @OneToMany(cascade = CascadeType.ALL,mappedBy = "topic",fetch = FetchType.EAGER) private List questions; Here the Question entity will have the annotation @ManyToOne , ...

Spring Boot Gradle MVC sample CRUD project

This is a sample Spring Boot Application that uses  JDBC Mysql JPA MVC Gradle Create Basic CRUD for Person Entity with JPA and Mysql Download from GitHub Project Structure  build.gradle View Maven Dependencies (pom.xml) here. buildscript { ext { springBootVersion = '1.2.5.RELEASE' } repositories { maven { url "http://repo.spring.io/libs-milestone" } mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") classpath("io.spring.gradle:dependency-management-plugin:0.5.1.RELEASE") } } apply plugin: 'java' apply plugin: 'eclipse-wtp' apply plugin: 'idea' apply plugin: 'spring-boot' apply plugin: 'io.spring.dependency-management' apply plugin: 'war' war { baseName = 'springboot' version = '0.0.1-SNAPSHOT' } sourceCompatibility = 1.7 targetCompatibility = 1.7 rep...