class Member1{
private Long id;
private Long teamid;
}
class Team1{
private Long id;
}
class Member1{
private Long id;
private Long teamid;
}
class Team1{
private Long id;
}
@Entity
public class Member {
@Id @GeneratedValue
@Column(name="MEMBER_ID")
private Long id;
@ManyToOne
@JoinColumn(name = "TEAM _ID")
private Team team;
}
@Entity
public class Team {
@Id @GeneratedValue
@Column(name="TEAM_ID")
private Long id;
}
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em =emf.createEntityManager();
EntityTransaction tx=em.getTransaction();
tx.begin();
try{
Team team =new Team(); //team 을 먼저 생성해야 member를 생성할 때 사용가능
em.persist(team);
Member member=new Member();
member.setTeam(team);
em.persist(member);
tx.commit();
}catch(Exception e) {
e.printStackTr
tx.rollback();
}finally {
em.close();
}
emf.close();
}
}
create table Member (
MEMBER_ID bigint not null,
TEAM_ID bigint,
primary key (MEMBER_ID)
)
Hibernate: create table Team (
TEAM_ID bigint not null,
primary key (TEAM_ID)
)
Hibernate: alter table Member
add constraint FKl7wsny760hjy6x19kqnduasbm
foreign key (TEAM_ID)
references Team
아래와 같이 각각의 값들이 jpa가 생성한 sql 문으로 db에 저장되는 것을 볼 수 있다.
Hibernate:
call next value for hibernate_sequence
Hibernate:
call next value for hibernate_sequence
Hibernate:
/* insert jpabook.jpashop.domain.Team
*/ insert
into
Team
(TEAM_ID)
values
(?)
Hibernate:
/* insert jpabook.jpashop.domain.Member
*/ insert
into
Member
(TEAM_ID, MEMBER_ID)
values
(?, ?)