diff --git a/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/override/entity/Address.java b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/override/entity/Address.java new file mode 100644 index 0000000000..95a3708173 --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/override/entity/Address.java @@ -0,0 +1,25 @@ +package com.baeldung.attribute.override.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/override/entity/Brand.java b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/override/entity/Brand.java new file mode 100644 index 0000000000..9918cbaa70 --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/override/entity/Brand.java @@ -0,0 +1,29 @@ +package com.baeldung.attribute.override.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/override/entity/Car.java b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/override/entity/Car.java new file mode 100644 index 0000000000..5421090e58 --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/override/entity/Car.java @@ -0,0 +1,54 @@ +package com.baeldung.attribute.override.entity; + +import javax.persistence.AttributeOverride; +import javax.persistence.AttributeOverrides; +import javax.persistence.Column; +import javax.persistence.ElementCollection; +import javax.persistence.Embedded; +import javax.persistence.Entity; +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/override/entity/Owner.java b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/override/entity/Owner.java new file mode 100644 index 0000000000..28ef6c7974 --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/override/entity/Owner.java @@ -0,0 +1,25 @@ +package com.baeldung.attribute.override.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/override/entity/Vehicle.java b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/override/entity/Vehicle.java new file mode 100644 index 0000000000..2d4c0c04b3 --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/override/entity/Vehicle.java @@ -0,0 +1,38 @@ +package com.baeldung.attribute.override.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/override/repository/CarRepository.java b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/override/repository/CarRepository.java new file mode 100644 index 0000000000..17cea77d89 --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations/src/main/java/com/baeldung/attribute/override/repository/CarRepository.java @@ -0,0 +1,7 @@ +package com.baeldung.attribute.override.repository; + +import com.baeldung.attribute.override.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/override/AttributeOverrideIntegrationTest.java b/persistence-modules/spring-data-jpa-annotations/src/test/java/com/baeldung/attribute/override/AttributeOverrideIntegrationTest.java new file mode 100644 index 0000000000..4cc599c7de --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations/src/test/java/com/baeldung/attribute/override/AttributeOverrideIntegrationTest.java @@ -0,0 +1,56 @@ +package com.baeldung.attribute.override; + +import com.baeldung.Application; +import com.baeldung.attribute.override.entity.Address; +import com.baeldung.attribute.override.entity.Brand; +import com.baeldung.attribute.override.entity.Car; +import com.baeldung.attribute.override.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 AttributeOverrideIntegrationTest { + + 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; + } +}