1 대 1 단방향, 주테이블에외래키실습 http://ojcedu.com, http://ojc.asia STS -> Spring Stater Project name : onetoone-1 SQL : JPA, MySQL 선택 http://ojc.asia/bbs/board.php?bo_table=lecspring&wr_id=524 ( 마리아 DB 설치는위 URL 에서참조 ) application.properties spring.datasource.platform=mysql spring.datasource.url=jdbc:mysql://localhost/onetoone_1?createdatabaseifnotexist=true spring.datasource.username=root spring.datasource.password=1111 spring.datasource.driver-class-name=com.mysql.jdbc.driver spring.datasource.sql-script-encoding=utf-8 spring.jpa.hibernate.ddl-auto=create spring.jpa.show-sql=true demo.model.emp.java @Entity public class Emp { @Id @GeneratedValue private Long empno; private String ename; /* PK 를마치외래키처럼이용해서 Addr 테이블과조인하려면 @PrimaryKeyJoinColumn 를 사용하면된다. @JoinColumn 처럼 addr_id 칼럼이생성되지않음 */ //JoinColumn 의 name 속성을생략하면 addr_addr 이름으로칼럼이만들어진다.
@OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "addr_id") private Addr addr; public Emp(String ename, Addr addr) { this.ename = ename; this.addr = addr; public Long getempno() { return empno; public void setempno(long empno) { this.empno = empno; public String getename() { return ename; public void setename(string ename) { this.ename = ename; public Addr getaddr() { return addr; public void setaddr(addr addr) { this.addr = addr; public String tostring() { return String.format( "Emp[empno=%d, ename='%s', address='%s']", empno, ename, addr.getaddress()); demo.model.addr.java @Entity public class Addr { @Id
@GeneratedValue private Long id; private String address; public Addr(String address) { this.address = address; public Long getid() {return id; public void setid(long id) { this.id = id; public String getaddress() { return address; public void setaddress(string address) { this.address = address; demo.repository.emprepository.java package demo.repository; import org.springframework.data.jpa.repository.jparepository; import demo.model.emp; public interface EmpRepository extends JpaRepository<Emp, Long> { demo.onetoone1application.java @SpringBootApplication public class Onetoone1Application implements CommandLineRunner{ public static void main(string[] args) { SpringApplication.run(Onetoone1Application.class, args); @Autowired EmpRepository emprepository; @Transactional
public void run(string...args) { List<Emp> emps = new ArrayList(); emps.add(new Emp(" 김길동 ", new Addr(" 서울 "))); emps.add(new Emp(" 나길동 ", new Addr(" 제주 "))); emps.add(new Emp(" 다길동 ", new Addr(" 뉴욕 "))); emprepository.save(emps); for(emp e : emprepository.findall()) { System.out.println(e.toString()); //1번사원이름을 " 김길동 " 의이름을 " 박길동 " 으로변경 // 주소를 " 서울 " 에서 " 하와이 " 로변경 // 아래두개의 DML이실행된다. //insert into addr (address) values (?) //update emp set addr_id=?, ename=? where empno=? Emp e1 = emprepository.findone(1l); Addr addr = new Addr(" 하와이 "); e1.setaddr(addr); e1.setename(" 박길동 "); //2 번사원 " 나길동 " 삭제, Addr 에서 2 번 " 제주 " 도삭제됨 //2 번사원은영속성컨텍스트에이미있으므로 DB 에쿼리하지않음 Emp e2 = emprepository.findone(2l); //2개의 delete DML이만들어진다.(emp, addr) //delete from emp where empno=? //delete from addr where id=? emprepository.delete(e2); 데이터확인하기
[ 실행결과 ] Hibernate: alter table emp drop foreign key FK_b1bolhhce7t698wamy15o3j47 Hibernate: drop table if exists addr Hibernate: drop table if exists emp Hibernate: create table addr (id bigint not null auto_increment, address varchar(255), primary key (id)) Hibernate: create table emp (empno bigint not null auto_increment, ename varchar(255), addr_id bigint, primary key (empno)) Hibernate: alter table emp add constraint FK_b1bolhhce7t698wamy15o3j47 foreign key (addr_id) references addr (id) Hibernate: insert into emp (addr_id, ename) values (?,?) Hibernate: insert into emp (addr_id, ename) values (?,?) Hibernate: insert into emp (addr_id, ename) values (?,?) Hibernate: select emp0_.empno as empno1_1_, emp0_.addr_id as addr_id3_1_, emp0_.ename as ename2_1_ from emp emp0_ // 메인출력 Emp[empno=1, ename=' 김길동 ', address=' 서울 '] Emp[empno=2, ename=' 나길동 ', address=' 제주 '] Emp[empno=3, ename=' 다길동 ', address=' 뉴욕 '] Hibernate: update emp set addr_id=?, ename=? where empno=? Hibernate: delete from emp where empno=? Hibernate: delete from addr where id=?