수색…
매개 변수
기둥 | 기둥 |
---|---|
@ 테이블 생성기 | 자동 ID 생성을 위해 테이블 생성기 전략 사용 |
@ 생성 값 | 필드에 적용된 값이 생성 된 값임을 지정합니다. |
@신분증 | 식별자를 필드에 주석으로 붙입니다. |
@ManyToOne | 직원과 부서 간 다 대일 관계를 지정합니다. 이 주석은 많은면에 표시되어 있습니다. 즉, 여러 직원이 단일 부서에 속합니다. 그래서 Department는 Employee 엔티티에서 @ManyToOne으로 주석 처리됩니다. |
@JoinColumn | 관련 엔티티의 외래 키를 저장하는 데이터베이스 테이블 열을 지정합니다. |
직원 대 부서 ManyToOne 관계
직원 실체
@Entity
public class Employee {
@TableGenerator(name = "employee_gen", table = "id_gen", pkColumnName = "gen_name", valueColumnName = "gen_val", allocationSize = 1)
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "employee_gen")
private int idemployee;
private String firstname;
private String lastname;
private String email;
@ManyToOne
@JoinColumn(name = "iddepartment")
private Department department;
// getters and setters
// toString implementation
}
부서 법인
@Entity
public class Department {
@Id
private int iddepartment;
private String name;
// getters, setters and toString()
}
테스트 클래스
public class Test {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("JPAExamples");
EntityManager em = emf.createEntityManager();
EntityTransaction txn = em.getTransaction();
Employee employee = new Employee();
employee.setEmail("someMail@gmail.com");
employee.setFirstname("Prasad");
employee.setLastname("kharkar");
txn.begin();
Department department = em.find(Department.class, 1);//returns the department named vert
System.out.println(department);
txn.commit();
employee.setDepartment(department);
txn.begin();
em.persist(employee);
txn.commit();
}
}
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow