Skip to content

Commit

Permalink
UMC-EWHA#31 Feat : Upload JpaMain
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjin-21 committed Nov 19, 2022
1 parent 7960565 commit e152d90
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 0 deletions.
45 changes: 45 additions & 0 deletions pom.xml
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>
112 changes: 112 additions & 0 deletions src/main/java/hellojpa/JpaMain.java
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);



}












}
75 changes: 75 additions & 0 deletions src/main/java/hellojpa/Member.java
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 +
'}';
}
}
5 changes: 5 additions & 0 deletions src/main/java/hellojpa/RoleType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package hellojpa;

public enum RoleType {
USER, ADMIN
}
43 changes: 43 additions & 0 deletions src/main/java/hellojpa/Team.java
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;
}
}
22 changes: 22 additions & 0 deletions src/main/resources/META-INF/persistence.xml
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>
22 changes: 22 additions & 0 deletions target/classes/META-INF/persistence.xml
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>

0 comments on commit e152d90

Please sign in to comment.