Merge pull request #10720 from polomos/BAEL-4900_attribute_overwrite

BAEL-4900 AttributeOverwrite explained
This commit is contained in:
Eric Martin 2021-05-16 14:01:40 -05:00 committed by GitHub
commit cfb8fe77b5
7 changed files with 234 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<Owner, Address> 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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<Car, Integer> {
}

View File

@ -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;
}
}