Querydsl JPA 샘플 (Sample)

JPA 2013. 12. 9. 14:42

다음은 Querydsl JPA 샘플이다.


pom.xml에 다음과 같이 디펜던시 (Dependency)와 플러그인 (Plugin)을 추가한다.


<properties>

<org.slf4j-version>1.7.5</org.slf4j-version>

<querydsl.version>3.2.4</querydsl.version>

</properties>


<dependencies>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>${org.slf4j-version}</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>${org.slf4j-version}</version>

</dependency>


<dependency>

<groupId>com.mysema.querydsl</groupId>

<artifactId>querydsl-apt</artifactId>

<version>${querydsl.version}</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>com.mysema.querydsl</groupId>

<artifactId>querydsl-jpa</artifactId>

<version>${querydsl.version}</version>

</dependency>

</dependencies>


<build>

<plugins>

<plugin>

<groupId>com.mysema.maven</groupId>

<artifactId>apt-maven-plugin</artifactId>

<version>1.0.9</version>

<executions>

<execution>

<goals>

<goal>process</goal>

</goals>

<configuration>

<outputDirectory>target/generated-sources/java</outputDirectory>

<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>

</configuration>

</execution>

</executions>

</plugin>

</plugins>

</build>


다음은 Event Entity 클래스이다.


package learningtest.com.mysema.query.jpa;


import java.util.Date;


import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.Table;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;


import org.hibernate.annotations.GenericGenerator;


@Entity(name = "learningtest.com.mysema.query.jpa.Event")

@Table(name = "EVENTS4")

public class Event {


private Long id;


private String title;

private Date date;


public Event() {

}


public Event(String title, Date date) {

this.title = title;

this.date = date;

}


@Id

@GeneratedValue(generator = "increment")

@GenericGenerator(name = "increment", strategy = "increment")

public Long getId() {

return id;

}


@SuppressWarnings("unused")

private void setId(Long id) {

this.id = id;

}


@Temporal(TemporalType.TIMESTAMP)

@Column(name = "EVENT_DATE")

public Date getDate() {

return date;

}


public void setDate(Date date) {

this.date = date;

}


public String getTitle() {

return title;

}


public void setTitle(String title) {

this.title = title;

}


}


다음은 META-INF/persistence.xml 파일이다.


<persistence 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"

             version="2.0">

<persistence-unit name="com.mysema.query.jpa">

<description>

Persistence unit for the JPA tutorial of the Querydsl Getting Started Guide

</description>

<class>learningtest.com.mysema.query.jpa.Event</class>

<properties>

<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />

<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE" />

<property name="javax.persistence.jdbc.user" value="sa" />

<property name="javax.persistence.jdbc.password" value="" />

<property name="hibernate.show_sql" value="true" />

<property name="hibernate.hbm2ddl.auto" value="create" />

</properties>

</persistence-unit>

</persistence>


다음과 같이 mvn eclipse:eclipse를 실행한다.


Run As -> Maven build...


Goals: eclipse:eclipse -> Run


다음 디렉토리에 Q 파일이 생성된다.


target/generated-sources/java


다음은 테스트 파일이다.


package learningtest.com.mysema.query.jpa;


import java.util.Date;

import java.util.List;


import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.persistence.Persistence;


import org.junit.After;

import org.junit.Before;

import org.junit.Test;


import com.mysema.query.jpa.impl.JPAQuery;


public class QuerydslJpaTest {


private EntityManagerFactory entityManagerFactory;


@Before

public void setUp() {

entityManagerFactory = Persistence

.createEntityManagerFactory("com.mysema.query.jpa");

}


@After

public void tearDown() {

entityManagerFactory.close();

}


@Test

public void test() {

EntityManager entityManager = entityManagerFactory

.createEntityManager();

entityManager.getTransaction().begin();

entityManager.persist(new Event("Our very first event!", new Date()));

entityManager.persist(new Event("A follow up event", new Date()));

entityManager.getTransaction().commit();


QEvent event = QEvent.event;

JPAQuery query = new JPAQuery(entityManager);

List<Event> list = query.from(event).list(event);

System.out.println(list);


entityManager.close();

}

}


테스트를 실행하면, 2개의 이벤트가 출력된다.


Reference:

http://www.querydsl.com/static/querydsl/3.2.4/reference/html/

Posted by izeye

댓글을 달아 주세요