#30 jpa basic: entity mapping

This commit is contained in:
haerong22
2023-01-14 21:26:13 +09:00
parent dbca3e6644
commit e78fc39151
9 changed files with 106 additions and 10 deletions

View File

@@ -1,4 +1,4 @@
package com.hello.jpa;
package com.hello.jpa.context;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

View File

@@ -1,4 +1,4 @@
package com.hello.jpa;
package com.hello.jpa.context;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

View File

@@ -1,4 +1,4 @@
package com.hello.jpa;
package com.hello.jpa.context;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

View File

@@ -1,9 +1,8 @@
package com.hello.jpa;
package com.hello.jpa.context;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
//@Entity
public class Member {
@Id

View File

@@ -1,4 +1,4 @@
package com.hello.jpa;
package com.hello.jpa.context;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

View File

@@ -0,0 +1,36 @@
package com.hello.jpa.entitymapping.ex00;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class JpaMain {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
Member member = new Member("name");
Member member2 = new Member("name2");
em.persist(member);
em.persist(member2);
tx.commit();
em.createQuery("select m from Member m").getResultList()
.forEach(System.out::println);
} catch (Exception e) {
tx.rollback();
} finally {
em.close();
}
emf.close();
}
}

View File

@@ -0,0 +1,54 @@
package com.hello.jpa.entitymapping.ex00;
import javax.persistence.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
@Entity
@Table(name = "Member2")
public class Member {
@Id
@GeneratedValue
private Long id;
@Column(name = "name", nullable = false, length = 10)
private String username;
private Integer age;
@Enumerated(EnumType.STRING)
private RoleType roleType;
@Column(updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date createdDate;
@Temporal(TemporalType.TIMESTAMP)
private Date lastModifiedDate;
private LocalDate localDate;
private LocalDateTime localDateTime;
@Lob
private String description;
@Transient
private int temp;
public Member() {
}
public Member(String username) {
this.username = username;
}
@Override
public String toString() {
return "Member{" +
"id=" + id +
", username='" + username + '\'' +
'}';
}
}

View File

@@ -0,0 +1,6 @@
package com.hello.jpa.entitymapping.ex00;
public enum RoleType {
USER, ADMIN
}

View File

@@ -3,20 +3,21 @@
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="hello">
<class>com.hello.jpa.Member</class>
<class>com.hello.jpa.context.Member</class>
<class>com.hello.jpa.entitymapping.ex00.Member</class>
<properties>
<!-- 필수 속성 -->
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/test"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:~/testdb"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<!-- 옵션 -->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
<!--<property name="hibernate.hbm2ddl.auto" value="create" />-->
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>