본문으로 바로가기

gradle로 jpa hibernate 빌드하기 with mysql

category Language/JAVA 2016.09.19 15:45

gradle로 jpa hibernate 빌드하기 with mysql



project 구조

c:\practice\hibernate_gradle_01 에 만들었다.

project명은 hibernate_gradle_01 로 정했다.




build.gradle

group 'hibernate_gradle_01'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.5

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
// https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager
compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.2.2.Final'

// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'

}

org.hibernate와 mysql jdbc connector를 import했다.



persistence.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="jpabook">
        <class>Member</class>
        <properties>
            <property name="javax.persistence.jdbc.Driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/krkim" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="password" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>
cs



db구조

db create문

1
2
3
4
5
6
7
8
9
10
CREATE TABLE `member` (
    `id` VARCHAR(50NOT NULL COLLATE 'utf16_bin',
    `name` VARCHAR(50NULL DEFAULT NULL COLLATE 'utf16_bin',
    `age` VARCHAR(50NULL DEFAULT NULL COLLATE 'utf16_bin',
    PRIMARY KEY (`id`)
)
COLLATE='utf16_bin'
ENGINE=InnoDB
;
 
cs




Member.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
 
/**
 * Created by grkim on 2016-08-22.
 */
 
@Entity
@Table(name="member")
public class Member {
 
    @Id
    @Column(name="id")
    private String id;
 
    @Column(name = "name")
    private String username;
    private Integer age;
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public Integer getAge() {
        return age;
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
}
 
cs


Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.List;
 
/**
 * Created by grkim on 2016-08-22.
 */
public class Main {
 
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpabook");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
 
        try {
            tx.begin();
            logic(em);
            tx.commit();
        } catch (Exception e) {
            System.out.println(e);
            tx.rollback();
        }
    }
 
    public static void logic(EntityManager em){
        String id = "id3";
        Member member = new Member();
        member.setId(id);
        member.setUsername("kyeongrok");
        member.setAge(2);
 
        em.persist(member);
 
        Member findMember = em.find(Member.class, id);
        System.out.println(findMember.getUsername() + findMember.getAge());
 
        member.setAge(20);
 
        findMember = em.find(Member.class, id);
        System.out.println(findMember.getUsername() + findMember.getAge());
 
        em.remove(member);
 
 
    }
 
}
 
cs


end.






댓글을 달아 주세요