[μ°Έκ³ ] @Entity(μμκ΄κ³ 맡ν), @MappedSuperclass(μμ±λ§ μμ)λ‘ μ§μ λ ν΄λμ€λ§ JPAμμ μμ κ°λ₯
@MappedSuperclass //맡νμ λ³΄λ§ λ°λ λΆλͺ¨ν΄λμ€
public abstract class BaseEntity {
private String createdBy;
private String updatedBy;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
}
@Entity
public class MemberMapping extends BaseEntity {
@Id
@GeneratedValue
@Column(name = "MEMBER_ID")
private Long id;
@Column(name = "USERNAME")
private String username;
@ManyToOne(fetch = FetchType.LAZY) //쿼리 λΆλ¦¬
@JoinColumn(name = "TEAM_ID")
private TeamMapping team;
@OneToOne
@JoinColumn(name = "LOCKER_ID")
private Locker locker;
@OneToMany(mappedBy = "member")
private List<MemberProduct> memberProductList = new ArrayList<>();
}
public class jpaMainMapping {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
MemberMapping member = new MemberMapping();
member.setUsername("member1");
//baseEntity κ° μ¬μ©
member.setCreatedAt(LocalDateTime.now());
member.setCreatedBy("kim");
em.persist(member);
tx.commit();
} catch (Exception e) {
tx.rollback();
} finally {
em.close();
}
emf.close();
}
}