From 2435e66439c95c2dd36baf0ad89e2bcc07671b92 Mon Sep 17 00:00:00 2001 From: sghosh Date: Mon, 4 Jan 2016 00:15:43 +0530 Subject: [PATCH] Source code for Intro to QueryDSL article. --- querydsl/pom.xml | 187 ++++++++++++++++++ .../main/java/org/baeldung/dao/PersonDao.java | 12 ++ .../java/org/baeldung/dao/PersonDaoImpl.java | 30 +++ .../main/java/org/baeldung/entity/Person.java | 50 +++++ .../main/resources/META-INF/persistence.xml | 19 ++ querydsl/src/main/resources/log4j.xml | 42 ++++ .../java/org/baeldung/dao/PersonDaoTest.java | 33 ++++ querydsl/src/test/resources/db.properties | 6 + querydsl/src/test/resources/test-context.xml | 20 ++ querydsl/src/test/resources/test-db.xml | 49 +++++ 10 files changed, 448 insertions(+) create mode 100644 querydsl/pom.xml create mode 100644 querydsl/src/main/java/org/baeldung/dao/PersonDao.java create mode 100644 querydsl/src/main/java/org/baeldung/dao/PersonDaoImpl.java create mode 100644 querydsl/src/main/java/org/baeldung/entity/Person.java create mode 100644 querydsl/src/main/resources/META-INF/persistence.xml create mode 100644 querydsl/src/main/resources/log4j.xml create mode 100644 querydsl/src/test/java/org/baeldung/dao/PersonDaoTest.java create mode 100644 querydsl/src/test/resources/db.properties create mode 100644 querydsl/src/test/resources/test-context.xml create mode 100644 querydsl/src/test/resources/test-db.xml diff --git a/querydsl/pom.xml b/querydsl/pom.xml new file mode 100644 index 0000000000..4efb5dcb8b --- /dev/null +++ b/querydsl/pom.xml @@ -0,0 +1,187 @@ + + + 4.0.0 + + org.baeldung + querydsl + 0.1-SNAPSHOT + jar + + querydsl + http://maven.apache.org + + + UTF-8 + 1.6 + 4.10 + 3.1.0.RELEASE + 4.1.1.Final + 2.5.0 + 1.5.10 + + + + + + com.mysema.querydsl + querydsl-core + ${querydsl.version} + + + + com.mysema.querydsl + querydsl-jpa + ${querydsl.version} + + + + com.mysema.querydsl + querydsl-apt + ${querydsl.version} + provided + + + + + org.hibernate + hibernate-entitymanager + ${hibernate.version} + compile + + + + org.hibernate.javax.persistence + hibernate-jpa-2.0-api + 1.0.0.Final + compile + + + + commons-dbcp + commons-dbcp + 1.4 + jar + compile + + + + commons-pool + commons-pool + 1.5.5 + jar + compile + + + + + org.hsqldb + hsqldb-j5 + 2.2.4 + + + + + org.springframework + spring-context + ${spring.version} + + + + org.springframework + spring-webmvc + ${spring.version} + + + + org.springframework + spring-orm + ${spring.version} + jar + compile + + + + org.springframework + spring-aop + ${spring.version} + + + + org.springframework + spring-test + ${spring.version} + jar + test + + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.slf4j + jcl-over-slf4j + ${slf4j.version} + runtime + + + org.slf4j + slf4j-log4j12 + ${slf4j.version} + runtime + + + log4j + log4j + 1.2.16 + + + + + junit + junit + ${junit.version} + test + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + ${java.version} + ${java.version} + -proc:none + + + + + + com.mysema.maven + maven-apt-plugin + 1.0.3 + + + + process + + + target/metamodel + com.mysema.query.apt.jpa.JPAAnnotationProcessor + + + + + + + \ No newline at end of file diff --git a/querydsl/src/main/java/org/baeldung/dao/PersonDao.java b/querydsl/src/main/java/org/baeldung/dao/PersonDao.java new file mode 100644 index 0000000000..cc193de44b --- /dev/null +++ b/querydsl/src/main/java/org/baeldung/dao/PersonDao.java @@ -0,0 +1,12 @@ +package org.baeldung.dao; + +import org.baeldung.entity.Person; + +import java.util.List; + +public interface PersonDao { + + public Person save(Person person); + + public List findPersonsByFirstnameQueryDSL(String firstname); +} \ No newline at end of file diff --git a/querydsl/src/main/java/org/baeldung/dao/PersonDaoImpl.java b/querydsl/src/main/java/org/baeldung/dao/PersonDaoImpl.java new file mode 100644 index 0000000000..82ee4c16cd --- /dev/null +++ b/querydsl/src/main/java/org/baeldung/dao/PersonDaoImpl.java @@ -0,0 +1,30 @@ +package org.baeldung.dao; + +import com.mysema.query.jpa.impl.JPAQuery; +import org.baeldung.entity.Person; +import org.baeldung.entity.QPerson; +import org.springframework.stereotype.Repository; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; + +@Repository +public class PersonDaoImpl implements PersonDao { + + @PersistenceContext + private EntityManager em; + + public Person save(Person person) { + em.persist(person); + return person; + } + + public List findPersonsByFirstnameQueryDSL(String firstname) { + JPAQuery query = new JPAQuery(em); + QPerson person = QPerson.person; + + return query.from(person).where(person.firstname.eq(firstname)).list(person); + } + +} \ No newline at end of file diff --git a/querydsl/src/main/java/org/baeldung/entity/Person.java b/querydsl/src/main/java/org/baeldung/entity/Person.java new file mode 100644 index 0000000000..d9b0a5a97d --- /dev/null +++ b/querydsl/src/main/java/org/baeldung/entity/Person.java @@ -0,0 +1,50 @@ +package org.baeldung.entity; + +import javax.persistence.*; + +@Entity +public class Person { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column + private String firstname; + + @Column + private String surname; + + Person() { + } + + public Person(String firstname, String surname) { + this.firstname = firstname; + this.surname = surname; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getFirstname() { + return firstname; + } + + public void setFirstname(String firstname) { + this.firstname = firstname; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + +} \ No newline at end of file diff --git a/querydsl/src/main/resources/META-INF/persistence.xml b/querydsl/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000000..111d7933c3 --- /dev/null +++ b/querydsl/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/querydsl/src/main/resources/log4j.xml b/querydsl/src/main/resources/log4j.xml new file mode 100644 index 0000000000..a7f96b38a4 --- /dev/null +++ b/querydsl/src/main/resources/log4j.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/querydsl/src/test/java/org/baeldung/dao/PersonDaoTest.java b/querydsl/src/test/java/org/baeldung/dao/PersonDaoTest.java new file mode 100644 index 0000000000..29d4dd98e9 --- /dev/null +++ b/querydsl/src/test/java/org/baeldung/dao/PersonDaoTest.java @@ -0,0 +1,33 @@ +package org.baeldung.dao; + +import junit.framework.Assert; +import org.baeldung.entity.Person; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.transaction.TransactionConfiguration; +import org.springframework.transaction.annotation.Transactional; + + +@ContextConfiguration("/test-context.xml") +@RunWith(SpringJUnit4ClassRunner.class) +@Transactional +@TransactionConfiguration(defaultRollback=true) +public class PersonDaoTest { + + @Autowired + private PersonDao personDao; + + @Test + public void testCreation() { + personDao.save(new Person("Erich", "Gamma")); + Person person = new Person("Kent", "Beck"); + personDao.save(person); + personDao.save(new Person("Ralph", "Johnson")); + + Person personFromDb = personDao.findPersonsByFirstnameQueryDSL("Kent").get(0); + Assert.assertEquals(person.getId(), personFromDb.getId()); + } +} \ No newline at end of file diff --git a/querydsl/src/test/resources/db.properties b/querydsl/src/test/resources/db.properties new file mode 100644 index 0000000000..efee3669ce --- /dev/null +++ b/querydsl/src/test/resources/db.properties @@ -0,0 +1,6 @@ +#In memory db +db.username=sa +db.password= +db.driver=org.hsqldb.jdbcDriver +db.url=jdbc:hsqldb:mem:app-db +db.dialect=org.hibernate.dialect.HSQLDialect \ No newline at end of file diff --git a/querydsl/src/test/resources/test-context.xml b/querydsl/src/test/resources/test-context.xml new file mode 100644 index 0000000000..13d823a857 --- /dev/null +++ b/querydsl/src/test/resources/test-context.xml @@ -0,0 +1,20 @@ + + + + + + + + + \ No newline at end of file diff --git a/querydsl/src/test/resources/test-db.xml b/querydsl/src/test/resources/test-db.xml new file mode 100644 index 0000000000..7f85630b6b --- /dev/null +++ b/querydsl/src/test/resources/test-db.xml @@ -0,0 +1,49 @@ + + + + + + + classpath:db.properties + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file