#30 jpa basic: value type collection
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package com.hello.jpa.valuetype.ex02;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
import java.util.Objects;
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
private void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
private void setStreet(String address) {
|
||||
this.street = address;
|
||||
}
|
||||
|
||||
public String getZipcode() {
|
||||
return zipcode;
|
||||
}
|
||||
|
||||
private void setZipcode(String zipcode) {
|
||||
this.zipcode = zipcode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof Address)) return false;
|
||||
Address address = (Address) o;
|
||||
return Objects.equals(city, address.city) && Objects.equals(street, address.street) && Objects.equals(zipcode, address.zipcode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(city, street, zipcode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.hello.jpa.valuetype.ex02;
|
||||
|
||||
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.getFavoriteFoods().add("치킨");
|
||||
member.getFavoriteFoods().add("피자");
|
||||
member.getFavoriteFoods().add("족발");
|
||||
|
||||
member.getAddressHistory().add(new Address("old", "old", "old"));
|
||||
member.getAddressHistory().add(new Address("old2", "old2", "old2"));
|
||||
|
||||
em.persist(member);
|
||||
|
||||
em.flush();
|
||||
em.clear();
|
||||
|
||||
System.out.println("============== START ==============");
|
||||
Member findMember = em.find(Member.class, member.getId());
|
||||
|
||||
findMember.getAddressHistory().forEach(System.out::println);
|
||||
findMember.getFavoriteFoods().forEach(System.out::println);
|
||||
|
||||
findMember.setHomeAddress(new Address("new city", "new address", "new zipcode"));
|
||||
|
||||
// 치킨 -> 한식
|
||||
findMember.getFavoriteFoods().remove("치킨");
|
||||
findMember.getFavoriteFoods().add("한식");
|
||||
|
||||
findMember.getAddressHistory().remove(new Address("old", "old", "old"));
|
||||
findMember.getAddressHistory().add(new Address("old3", "old3", "old3"));
|
||||
|
||||
tx.commit();
|
||||
} catch (Exception e) {
|
||||
tx.rollback();
|
||||
} finally {
|
||||
em.close();
|
||||
}
|
||||
|
||||
emf.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.hello.jpa.valuetype.ex02;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
public class Member {
|
||||
|
||||
@Id @GeneratedValue
|
||||
@Column(name = "MEMBER_ID")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "USERNAME")
|
||||
private String name;
|
||||
|
||||
@Embedded
|
||||
private Address homeAddress;
|
||||
|
||||
@ElementCollection
|
||||
@CollectionTable(name = "FAVORITE_FOOD", joinColumns = @JoinColumn(name = "MEMBER_ID"))
|
||||
@Column(name = "FOOD_NAME")
|
||||
private Set<String> favoriteFoods = new HashSet<>();
|
||||
|
||||
public Set<String> getFavoriteFoods() {
|
||||
return favoriteFoods;
|
||||
}
|
||||
|
||||
public void setFavoriteFoods(Set<String> favoriteFoods) {
|
||||
this.favoriteFoods = favoriteFoods;
|
||||
}
|
||||
|
||||
public List<Address> getAddressHistory() {
|
||||
return addressHistory;
|
||||
}
|
||||
|
||||
public void setAddressHistory(List<Address> addressHistory) {
|
||||
this.addressHistory = addressHistory;
|
||||
}
|
||||
|
||||
@ElementCollection
|
||||
@CollectionTable(name = "ADDRESS", joinColumns = @JoinColumn(name = "MEMBER_ID"))
|
||||
private List<Address> addressHistory = new ArrayList<>();
|
||||
|
||||
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 Address getHomeAddress() {
|
||||
return homeAddress;
|
||||
}
|
||||
|
||||
public void setHomeAddress(Address homeAddress) {
|
||||
this.homeAddress = homeAddress;
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,8 @@
|
||||
<!-- <class>com.hello.jpa.proxy.ex01.Parent</class>-->
|
||||
<!-- <class>com.hello.jpa.proxy.ex01.Child</class>-->
|
||||
<!-- <class>com.hello.jpa.valuetype.ex00.Member</class>-->
|
||||
<class>com.hello.jpa.valuetype.ex01.Member</class>
|
||||
<!-- <class>com.hello.jpa.valuetype.ex01.Member</class>-->
|
||||
<class>com.hello.jpa.valuetype.ex02.Member</class>
|
||||
|
||||
<properties>
|
||||
<!-- 필수 속성 -->
|
||||
|
||||
Reference in New Issue
Block a user