@AttributeOverride
@MappedSuperclass๋ฅผ ํตํด ์์ ๋ฐ์ ๊ฒฝ์ฐ๋,
@Embedded๋ฅผ ํตํด ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํ๋์ ์ ์ธํ ๊ฒฝ์ฐ ํด๋น ์ํฐํฐ์์๋ ๋ค๋ฅธ ์ปฌ๋ผ๋ช ์ ์ฌ์ฉํ๊ณ ์ถ์ ๋๊ฐ ์์ต๋๋ค.
์ด๋ฌํ ๊ฒฝ์ฐ์ ์ฌ์ฉํ ์ ์๋ ์ด๋ ธํ ์ด์ ์ ๋๋ค.
@AttributeOverride ํน์ @AttributeOverrides๋ฅผ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ์ฌ์ฉ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
@AttributeOverride(name = "๋ถ๋ชจ ํด๋์ค ํ๋๋ช ", column = @Column(name = "์์์์ ์ฌ์ฉํ ์ปฌ๋ผ๋ช "))
@MappedSuperclass
public class Vehicle {
@Id
@GeneratedValue
private Integer id;
private String identifier;
private Integer numberOfWheels;
// standard getters and setters
}
@Entity
@AttributeOverride(name = "identifier", column = @Column(name = "VIN"))
public class Car extends Vehicle {
private String model;
private String name;
// standard getters and setters
}
๋ถ๋ชจ ํด๋์ค(Vehicle)์ identifier ์์ฑ์ ๋ํ ์ปฌ๋ผ๋ช ์ VIN์ผ๋ก ์ฌ์ ์ํ์ฌ ์ฌ์ฉํ์์ต๋๋ค.
@Entity
@AttributeOverrides({
@AttributeOverride(name = "id", column = @Column(name = "CAR_ID"))
@AttributeOverride(name = "identifier", column = @Column(name = "VIN"))
})
public class Car extends Vehicle {
private String model;
private String name;
// standard getters and setters
}
์์ ๊ฐ์ด ์ฌ๋ฌ ํ๋๋ฅผ ์ฌ์ ์ํ ์ ์์ต๋๋ค.
์๋ฒ ๋๋ ํ์ ์ ์๋ฒ ๋๋ ํ์ ์์ฑ ์ฌ์ ์ํ๊ธฐ
์๋ฒ ๋๋ ํ์ ์์ ์๋ฒ ๋๋ ํ์ ์ด ๋ค์ค์ผ๋ก ๊ฑธ์ณ ์กด์ฌํ๋ ๊ฒฝ์ฐ๋ ์์ต๋๋ค.
์ด๋ด ๊ฒฝ์ฐ ์๋์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์ ์๊ฐ ๊ฐ๋ฅํฉ๋๋ค.
@Embeddable
public class Address {
private String city;
private String street;
@Embedded
private Zipcode zipcode;
}
@Embeddable
public class Zipcode {
private String zip;
private String plusFour;
}
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Setter
@Getter
public class Member{
@Id @GeneratedValue
@Column(name = "MEMBER_ID")
private Long id;
private String name;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "city", column = @Column(name = "HOME_CITY")),
@AttributeOverride(name = "street", column = @Column(name = "HOME_STREET")),
@AttributeOverride(name = "zipcode.zip", column = @Column(name = "HOME_ZIP")),
@AttributeOverride(name = "zipcode.plusFour", column = @Column(name = "HOME_PLUS_FOUR")),
})
private Address homeAddress;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "city", column = @Column(name = "COMPANY_CITY")),
@AttributeOverride(name = "street", column = @Column(name = "COMPANY_STREET")),
@AttributeOverride(name = "zipcode.zip", column = @Column(name = "COMPANY_ZIP")),
@AttributeOverride(name = "zipcode.plusFour", column = @Column(name = "COMPANY_PLUS_FOUR")),
})
private Address companyAddress;
}
name์ .์ ํตํด ์ค์ ํ ์ ์์ต๋๋ค.
๐ Reference
https://www.baeldung.com/jpa-attributeoverride
[์๋ฐ ORM ํ์ค JPA ํ๋ก๊ทธ๋๋ฐ - ๊น์ํ]
'๐๏ธ Spring > JPA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JPA] JPQL ์ฌ์ฉ๋ฒ (0) | 2021.12.17 |
---|---|
[JPA] JPA์ ๋ค์ํ ์ฟผ๋ฆฌ ๋ฐฉ๋ฒ (JPQL, JdbcTemplate) (0) | 2021.12.16 |
[JPA] ์์์ฑ ์ ์ด(CASCADE)์ ๊ณ ์ ๊ฐ์ฒด (0) | 2021.12.15 |
[JPA] ์ฆ์ ๋ก๋ฉ๊ณผ ์ง์ฐ ๋ก๋ฉ ( + PersistentBag) (0) | 2021.12.15 |
[JPA] JPA์์์ ํ๋ก์ (0) | 2021.12.15 |