From d34fdfd40af7843eea911e1a6c8acf8f196c16a8 Mon Sep 17 00:00:00 2001 From: Jacek Polom Date: Thu, 29 Apr 2021 21:20:09 +0200 Subject: [PATCH] BAEL-4900 AttributeOverwrite explained --- .../attribute/overwrite/entity/Address.java | 26 +++++++++ .../attribute/overwrite/entity/Brand.java | 29 ++++++++++ .../attribute/overwrite/entity/Car.java | 51 +++++++++++++++++ .../attribute/overwrite/entity/Owner.java | 25 +++++++++ .../attribute/overwrite/entity/Vehicle.java | 38 +++++++++++++ .../overwrite/repository/CarRepository.java | 7 +++ .../AttributeOverwriteIntegrationTest.java | 56 +++++++++++++++++++ 7 files changed, 232 insertions(+) create mode 100644 persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Address.java create mode 100644 persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Brand.java create mode 100644 persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Car.java create mode 100644 persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Owner.java create mode 100644 persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Vehicle.java create mode 100644 persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/repository/CarRepository.java create mode 100644 persistence-modules/spring-data-jpa-annotations/src/test/java/com/baeldung/attribute/overwrite/AttributeOverwriteIntegrationTest.java diff --git a/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Address.java b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Address.java new file mode 100644 index 0000000000..b1d7c60acd --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Address.java @@ -0,0 +1,26 @@ +package com.baeldung.attribute.overwrite.entity; + +import javax.persistence.Embeddable; + +@Embeddable +public class Address { + private String name; + private String city; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } +} diff --git a/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Brand.java b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Brand.java new file mode 100644 index 0000000000..7541b52a4d --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Brand.java @@ -0,0 +1,29 @@ +package com.baeldung.attribute.overwrite.entity; + +import javax.persistence.Embeddable; +import javax.persistence.Embedded; +import java.time.LocalDate; + +@Embeddable +public class Brand { + private String name; + private LocalDate foundationDate; + @Embedded + private Address address; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public LocalDate getFoundationDate() { + return foundationDate; + } + + public void setFoundationDate(LocalDate foundationDate) { + this.foundationDate = foundationDate; + } +} diff --git a/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Car.java b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Car.java new file mode 100644 index 0000000000..6213927e50 --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Car.java @@ -0,0 +1,51 @@ +package com.baeldung.attribute.overwrite.entity; + + +import javax.persistence.*; +import java.util.Map; + +@Entity +@AttributeOverride(name = "identifier", column = @Column(name = "VIN")) +public class Car extends Vehicle { + + private String model; + private String name; + @Embedded + @AttributeOverrides({ + @AttributeOverride(name = "name", column = @Column(name = "BRAND_NAME", length = 5)), + @AttributeOverride(name = "address.name", column = @Column(name = "ADDRESS_NAME")) + }) + private Brand brand; + @ElementCollection + @AttributeOverrides({ + @AttributeOverride(name = "key.name", column = @Column(name = "OWNER_NAME")), + @AttributeOverride(name = "key.surname", column = @Column(name = "OWNER_SURNAME")), + @AttributeOverride(name = "value.name", column = @Column(name = "ADDRESS_NAME")), + }) + Map owners; + + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Brand getBrand() { + return brand; + } + + public void setBrand(Brand brand) { + this.brand = brand; + } +} diff --git a/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Owner.java b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Owner.java new file mode 100644 index 0000000000..134bac75ee --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Owner.java @@ -0,0 +1,25 @@ +package com.baeldung.attribute.overwrite.entity; + +import javax.persistence.Embeddable; + +@Embeddable +public class Owner { + private String name; + private String surname; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } +} diff --git a/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Vehicle.java b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Vehicle.java new file mode 100644 index 0000000000..99cb4c58e4 --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/entity/Vehicle.java @@ -0,0 +1,38 @@ +package com.baeldung.attribute.overwrite.entity; + +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; + +@MappedSuperclass +public class Vehicle { + @Id + @GeneratedValue + private Integer id; + private String identifier; + private Integer numberOfWheels; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getIdentifier() { + return identifier; + } + + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + + public Integer getNumberOfWheels() { + return numberOfWheels; + } + + public void setNumberOfWheels(Integer numberOfWheels) { + this.numberOfWheels = numberOfWheels; + } +} diff --git a/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/repository/CarRepository.java b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/repository/CarRepository.java new file mode 100644 index 0000000000..1152de2d7f --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/overwrite/repository/CarRepository.java @@ -0,0 +1,7 @@ +package com.baeldung.attribute.overwrite.repository; + +import com.baeldung.attribute.overwrite.entity.Car; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CarRepository extends JpaRepository { +} diff --git a/persistence-modules/spring-data-jpa-annotations/src/test/java/com/baeldung/attribute/overwrite/AttributeOverwriteIntegrationTest.java b/persistence-modules/spring-data-jpa-annotations/src/test/java/com/baeldung/attribute/overwrite/AttributeOverwriteIntegrationTest.java new file mode 100644 index 0000000000..57a880ba16 --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations/src/test/java/com/baeldung/attribute/overwrite/AttributeOverwriteIntegrationTest.java @@ -0,0 +1,56 @@ +package com.baeldung.attribute.overwrite; + +import com.baeldung.Application; +import com.baeldung.attribute.overwrite.entity.Address; +import com.baeldung.attribute.overwrite.entity.Brand; +import com.baeldung.attribute.overwrite.entity.Car; +import com.baeldung.attribute.overwrite.repository.CarRepository; +import org.assertj.core.api.Assertions; +import org.jetbrains.annotations.NotNull; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDate; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = {Application.class}) +public class AttributeOverwriteIntegrationTest { + + private static final LocalDate FORD_FOUNDATION_DATE = LocalDate.parse("1903-06-16"); + @Autowired + CarRepository carRepository; + + @Test + @Transactional + public void whenInsertingCar_thenEmbeddedAndMappedFieldsArePopulated() { + + Car fordMustang = createMustang(); + + carRepository.save(fordMustang); + Car actualCar = carRepository.getOne(fordMustang.getId()); + + Assertions.assertThat(actualCar).isEqualTo(fordMustang); + } + + @NotNull + private Car createMustang() { + Address address = new Address(); + address.setName("Ford United States"); + address.setCity("Dearborn"); + + Brand ford = new Brand(); + ford.setName("Ford"); + ford.setFoundationDate(FORD_FOUNDATION_DATE); + + Car fordMustang = new Car(); + fordMustang.setIdentifier("WP1AB29P88LA47599"); + fordMustang.setModel("Ford"); + fordMustang.setName("My car"); + fordMustang.setBrand(ford); + return fordMustang; + } +}