Skip to main content

Posts

Showing posts from September, 2016

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

Java : How to create a Jar with single or multiple files

How to create a jar file. We can make a jar file with a single class using the following commands Here can be replaced by the name of the jar file you want. 1. Create a jar file with single file jar --create --file=<FileName> <file-1> 2. Create a jar file with multiple files jar --create --file=<FileName> <file-1> <file-2> 3. Create a jar file with all files in a directory jar --create --file="<FileName> -C /path/to/dir/ ." Here -C specifies the directory and . specifies that it need to include all the files in te directory. Some of the shorthands for the above commands Option Shorthand --create -c --file -f --module-path -p --verbose -v --list -t --extract -x How to extract a jar file jar -xf <FileName> How to list the contents of a jar file jar -tf <FileName>

Design Patterns : Builder Pattern

Points To Remember It is a Creational Design Pattern It must be used when you have multiple overloaded constructors or setters Its main purpose it to hide from the user, how the object is to be created . It is advised to make your constructors private when you are using Builder Design Pattern. What is Builder Design Pattern It is a creational design pattern. So it is only responsible for how an object must be created. There may be scenarios where there is a class with many instance variables that may be needed to create an object of the class. For this you might have to create many overloaded constructors. For Example we have a class NutritionFacts that has following instance variables private int servingSize; private int servings; private int calories; private int fat; private int sodium; private int carbohydrate; Then to create the object of the class we can have constructors as follows NutritionFacts cocaCola = new NutritionFacts( 240 , 8 ,