BAEL-4900 AttributeOverwrite explained
This commit is contained in:
parent
f2cb474afb
commit
d34fdfd40a
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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<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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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<Car, Integer> {
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue