#30 jpa basic: embedded

This commit is contained in:
haerong22
2023-02-02 01:09:53 +09:00
parent 3c35236261
commit 6a75b0faa0
5 changed files with 169 additions and 2 deletions

View File

@@ -0,0 +1,44 @@
package com.hello.jpa.valuetype.ex00;
import javax.persistence.Embeddable;
@Embeddable
public class Address {
private String city;
private String street;
private String zipcode;
public Address() {
}
public Address(String city, String street, String zipcode) {
this.city = city;
this.street = street;
this.zipcode = zipcode;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String address) {
this.street = address;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}

View File

@@ -0,0 +1,35 @@
package com.hello.jpa.valuetype.ex00;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class JpaMain {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
Member member = new Member();
member.setName("name");
member.setHomeAddress(new Address("city", "address", "zipcode"));
member.setWorkPeriod(new Period());
em.persist(member);
tx.commit();
} catch (Exception e) {
tx.rollback();
} finally {
em.close();
}
emf.close();
}
}

View File

@@ -0,0 +1,60 @@
package com.hello.jpa.valuetype.ex00;
import javax.persistence.*;
@Entity
public class Member {
@Id @GeneratedValue
@Column(name = "MEMBER_ID")
private Long id;
@Column(name = "USERNAME")
private String name;
@Embedded
private Period workPeriod;
@Embedded
private Address homeAddress;
@Embedded
@AttributeOverrides({
@AttributeOverride(name="city", column = @Column(name = "work_city")),
@AttributeOverride(name="street", column = @Column(name = "work_street")),
@AttributeOverride(name="zipcode", column = @Column(name = "work_zipcode"))
})
private Address workAddress;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Period getWorkPeriod() {
return workPeriod;
}
public void setWorkPeriod(Period workPeriod) {
this.workPeriod = workPeriod;
}
public Address getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
}

View File

@@ -0,0 +1,27 @@
package com.hello.jpa.valuetype.ex00;
import javax.persistence.Embeddable;
import java.time.LocalDateTime;
@Embeddable
public class Period {
private LocalDateTime startDate;
private LocalDateTime endDate;
public LocalDateTime getStartDate() {
return startDate;
}
public void setStartDate(LocalDateTime startDate) {
this.startDate = startDate;
}
public LocalDateTime getEndDate() {
return endDate;
}
public void setEndDate(LocalDateTime endDate) {
this.endDate = endDate;
}
}

View File

@@ -32,8 +32,9 @@
<!-- <class>com.hello.jpa.inheritance.ex01.Team</class>-->
<!-- <class>com.hello.jpa.proxy.ex00.Member</class>-->
<!-- <class>com.hello.jpa.proxy.ex00.Team</class>-->
<class>com.hello.jpa.proxy.ex01.Parent</class>
<class>com.hello.jpa.proxy.ex01.Child</class>
<!-- <class>com.hello.jpa.proxy.ex01.Parent</class>-->
<!-- <class>com.hello.jpa.proxy.ex01.Child</class>-->
<class>com.hello.jpa.valuetype.ex00.Member</class>
<properties>
<!-- 필수 속성 -->