forked from UMC-EWHA/umc-spring-3rd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
324 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>jpa-basic</groupId> | ||
<artifactId>ex1-hello-jpa</artifactId> | ||
<version>1.0.0</version> | ||
|
||
|
||
|
||
|
||
|
||
<dependencies> | ||
|
||
|
||
|
||
|
||
<dependency> | ||
<groupId>org.hibernate</groupId> | ||
<artifactId>hibernate-entitymanager</artifactId> | ||
<version>5.3.10.Final</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
<version> 2.1.214 </version> | ||
</dependency> | ||
|
||
|
||
|
||
|
||
|
||
|
||
</dependencies> | ||
<properties> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
|
||
</properties> | ||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package hellojpa; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.EntityTransaction; | ||
import javax.persistence.Persistence; | ||
import java.util.List; | ||
|
||
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 { | ||
//tx.begin(); //트랜잭션 시작 | ||
logic(em); //비지니스 로직 실행 | ||
for(int i=0;i<100;i++) { | ||
Member member = new Member(); | ||
member.setUsername("member1"+i); | ||
member.setAge(i); | ||
em.persist(member); | ||
} | ||
em.flush(); | ||
em.clear(); | ||
|
||
|
||
List<Member> result = em.createQuery("select m from Member m order by m.age desc", Member.class) | ||
.setFirstResult(1) | ||
.setMaxResults(10) | ||
.getResultList(); | ||
System.out.println("result = "+ result.size()); | ||
for(Member member1 : result){ | ||
System.out.println("member1 ="+member1); | ||
} | ||
|
||
|
||
tx.commit(); //트랜잭션 커밋 | ||
/** //저장 | ||
Team team = new Team(); | ||
team.setName("TeamA"); | ||
// team.getMembers().add(member); | ||
em.persist(team); | ||
Member member = new Member(); | ||
member.setUsername("member1"); | ||
member.setTeam(team); | ||
em.persist(member); | ||
em.flush(); | ||
em.clear(); | ||
Member findMember = em.find(Member.class, member.getId()); | ||
List<Member> members = findMember.getTeam().getMembers(); | ||
for(Member m:members){ | ||
System.out.println("m= "+m.getUsername()); | ||
} | ||
Team findTeam = findMember.getTeam(); | ||
System.out.println("findTeam = "+ findTeam.getName());**/ | ||
|
||
} catch (Exception e){ | ||
tx.rollback(); //트랜잭션 롤백 | ||
} finally { | ||
em.close(); | ||
} | ||
|
||
emf.close(); | ||
|
||
} | ||
|
||
private static void logic(EntityManager em){ | ||
String id ="id1"; | ||
Member member = new Member(); | ||
member.setId(id); | ||
member.setUsername("윤진"); | ||
member.setAge(2); | ||
|
||
em.persist(member); | ||
|
||
|
||
member.setAge(20); | ||
Member findMember = em.find(Member.class, id); | ||
System.out.println("findMember="+findMember.getUsername()+", age="+ findMember.getAge()); | ||
|
||
|
||
em.remove(member); | ||
|
||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package hellojpa; | ||
import javax.persistence.*; | ||
import java.util.Date; | ||
|
||
|
||
@Entity | ||
public class Member { | ||
|
||
@Id @GeneratedValue | ||
@Column(name = "MEMBER_ID") | ||
private String id; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
@Column(name = "USERNAME") | ||
private String username; | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
|
||
|
||
//@Column(name = "TEAM_ID") | ||
// | ||
//private Long teamId; | ||
|
||
@ManyToOne | ||
@JoinColumn(name ="Team_ID") | ||
private Team team; | ||
|
||
public Team getTeam() { | ||
return team; | ||
} | ||
|
||
public void setTeam(Team team) { | ||
this.team = team; | ||
} | ||
@Column(name = "AGE") | ||
private int age; | ||
public void setAge(int age) { | ||
this.age = age; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
//public Long getTeamId() { | ||
// return teamId; | ||
// } | ||
|
||
//public void setTeamId(Long teamId) { | ||
// this.teamId = teamId; | ||
//} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return "Member{" + | ||
"id='" + id + '\'' + | ||
", username='" + username + '\'' + | ||
", age=" + age + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package hellojpa; | ||
|
||
public enum RoleType { | ||
USER, ADMIN | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package hellojpa; | ||
|
||
import javax.persistence.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Entity | ||
public class Team { | ||
@Id | ||
@GeneratedValue | ||
@Column(name = "TEAM_ID") | ||
private Long id; | ||
private String name; | ||
|
||
@OneToMany(mappedBy = "team") | ||
private List<Member> members = new ArrayList<>(); | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
|
||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public List<Member> getMembers() { | ||
return members; | ||
} | ||
|
||
public void setMembers(List<Member> members) { | ||
this.members = members; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<persistence version="2.2" | ||
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"> | ||
<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="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.jdbc.batch_size" value="10"/> | ||
<property name="hibernate.hbm2ddl.auto" value="create" /> | ||
</properties> | ||
</persistence-unit> | ||
</persistence> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<persistence version="2.2" | ||
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"> | ||
<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="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.jdbc.batch_size" value="10"/> | ||
<property name="hibernate.hbm2ddl.auto" value="create" /> | ||
</properties> | ||
</persistence-unit> | ||
</persistence> |