Samples for the Intro to QueryDSL article, merged with existing querydsl module. Updated module dependencies. (#476)
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
010c6b3ace
commit
89373cd1eb
@@ -10,8 +10,8 @@ import org.baeldung.entity.Person;
|
||||
import org.baeldung.entity.QPerson;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.mysema.query.group.GroupBy;
|
||||
import com.mysema.query.jpa.impl.JPAQuery;
|
||||
import com.querydsl.core.group.GroupBy;
|
||||
import com.querydsl.jpa.impl.JPAQuery;
|
||||
|
||||
@Repository
|
||||
public class PersonDaoImpl implements PersonDao {
|
||||
@@ -27,39 +27,39 @@ public class PersonDaoImpl implements PersonDao {
|
||||
|
||||
@Override
|
||||
public List<Person> findPersonsByFirstnameQueryDSL(final String firstname) {
|
||||
final JPAQuery query = new JPAQuery(em);
|
||||
final JPAQuery<Person> query = new JPAQuery<>(em);
|
||||
final QPerson person = QPerson.person;
|
||||
|
||||
return query.from(person).where(person.firstname.eq(firstname)).list(person);
|
||||
return query.from(person).where(person.firstname.eq(firstname)).fetch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Person> findPersonsByFirstnameAndSurnameQueryDSL(final String firstname, final String surname) {
|
||||
final JPAQuery query = new JPAQuery(em);
|
||||
final JPAQuery<Person> query = new JPAQuery<>(em);
|
||||
final QPerson person = QPerson.person;
|
||||
|
||||
return query.from(person).where(person.firstname.eq(firstname).and(person.surname.eq(surname))).list(person);
|
||||
return query.from(person).where(person.firstname.eq(firstname).and(person.surname.eq(surname))).fetch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Person> findPersonsByFirstnameInDescendingOrderQueryDSL(final String firstname) {
|
||||
final JPAQuery query = new JPAQuery(em);
|
||||
final JPAQuery<Person> query = new JPAQuery<>(em);
|
||||
final QPerson person = QPerson.person;
|
||||
|
||||
return query.from(person).where(person.firstname.eq(firstname)).orderBy(person.surname.desc()).list(person);
|
||||
return query.from(person).where(person.firstname.eq(firstname)).orderBy(person.surname.desc()).fetch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int findMaxAge() {
|
||||
final JPAQuery query = new JPAQuery(em);
|
||||
final JPAQuery<Person> query = new JPAQuery<>(em);
|
||||
final QPerson person = QPerson.person;
|
||||
|
||||
return query.from(person).list(person.age.max()).get(0);
|
||||
return query.from(person).select(person.age.max()).fetchFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Integer> findMaxAgeByName() {
|
||||
final JPAQuery query = new JPAQuery(em);
|
||||
final JPAQuery<Person> query = new JPAQuery<>(em);
|
||||
final QPerson person = QPerson.person;
|
||||
|
||||
return query.from(person).transform(GroupBy.groupBy(person.firstname).as(GroupBy.max(person.age)));
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* (c) Центр ИТ, 2016. Все права защищены.
|
||||
*/
|
||||
package org.baeldung.querydsl.intro.entities;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class BlogPost {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String body;
|
||||
|
||||
@ManyToOne
|
||||
private User user;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String code) {
|
||||
this.title = code;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public void setBody(String body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* (c) Центр ИТ, 2016. Все права защищены.
|
||||
*/
|
||||
package org.baeldung.querydsl.intro.entities;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String login;
|
||||
|
||||
private Boolean disabled;
|
||||
|
||||
@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "user")
|
||||
private Set<BlogPost> blogPosts = new HashSet<>(0);
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setLogin(String name) {
|
||||
this.login = name;
|
||||
}
|
||||
|
||||
public Set<BlogPost> getBlogPosts() {
|
||||
return blogPosts;
|
||||
}
|
||||
|
||||
public void setBlogPosts(Set<BlogPost> blogPosts) {
|
||||
this.blogPosts = blogPosts;
|
||||
}
|
||||
|
||||
public Boolean getDisabled() {
|
||||
return disabled;
|
||||
}
|
||||
|
||||
public void setDisabled(Boolean disabled) {
|
||||
this.disabled = disabled;
|
||||
}
|
||||
}
|
||||
@@ -16,4 +16,17 @@
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
<!-- PersistenceUnit for Intro to QueryDSL -->
|
||||
<persistence-unit name="org.baeldung.querydsl.intro">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<properties>
|
||||
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
|
||||
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:test"/>
|
||||
<property name="hibernate.connection.username" value="sa"/>
|
||||
<property name="hibernate.connection.password" value=""/>
|
||||
<property name="hibernate.hbm2ddl.auto" value="update"/>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
</persistence>
|
||||
@@ -1,12 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
|
||||
<configuration>
|
||||
|
||||
<!-- Appenders -->
|
||||
<appender name="console" class="org.apache.log4j.ConsoleAppender">
|
||||
<param name="Target" value="System.out" />
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p: %c - %m%n" />
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||
<Pattern>%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p: %c - %m%n</Pattern>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
@@ -33,10 +30,8 @@
|
||||
<level value="warn" />
|
||||
</logger>
|
||||
|
||||
<!-- Root Logger -->
|
||||
<root>
|
||||
<priority value="warn" />
|
||||
<appender-ref ref="console" />
|
||||
<root level="warn">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
|
||||
</log4j:configuration>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user