[java] JPA에서의 엔티티 매핑 설정과 MyBatis에서의 resultMap 설정 비교
소개
JPA(Java Persistence API)와 MyBatis는 둘 다 Java 언어용 ORM(Object-Relational Mapping) 프레임워크를 지원합니다. 오늘은 JPA와 MyBatis에서의 엔티티 매핑 설정 방식을 비교해보겠습니다.
JPA의 엔티티 매핑 설정
JPA에서의 엔티티 매핑은 @Entity
와 @OneToMany
, @ManyToOne
같은 어노테이션을 사용하여 설정됩니다. 또한, 데이터베이스 스키마와의 매핑은 @Table
과 같은 어노테이션을 통해 지정할 수 있습니다.
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy = "employee")
private List<Project> projects;
// getter, setter, 등등
}
MyBatis의 resultMap 설정
반면에 MyBatis에서는 resultMap
을 사용하여 엔티티와 데이터베이스 결과를 매핑합니다. 아래는 MyBatis의 resultMap
설정 예제입니다.
<resultMap id="employeeResultMap" type="Employee">
<id property="id" column="employee_id" />
<result property="name" column="employee_name" />
<collection property="projects" ofType="Project" column="projectId" select="getProjectsByEmployeeId" />
</resultMap>
결론
JPA와 MyBatis는 엔티티 매핑을 위한 각기 다른 방식을 제공합니다. JPA는 어노테이션 기반의 매핑을, MyBatis는 XML 기반의 resultMap
을 사용하여 매핑을 정의합니다.
JPA는 객체와 관계형 데이터베이스 간 매핑을 좀 더 간단하게 할 수 있지만, MyBatis는 SQL 쿼리를 더 강력하게 제어할 수 있다는 장점이 있습니다.
이처럼, 각 프레임워크는 개발자의 취향과 프로젝트의 요구에 따라 선택되어져야 합니다.