BAEL-21730: Migrate querydsl to com.baeldung
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.dao;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.baeldung.entity.Person;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
public Person save(Person person);
|
||||
|
||||
public List<Person> findPersonsByFirstnameQueryDSL(String firstname);
|
||||
|
||||
public List<Person> findPersonsByFirstnameAndSurnameQueryDSL(String firstname, String surname);
|
||||
|
||||
public List<Person> findPersonsByFirstnameInDescendingOrderQueryDSL(String firstname);
|
||||
|
||||
public int findMaxAge();
|
||||
|
||||
public Map<String, Integer> findMaxAgeByName();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.dao;
|
||||
|
||||
import com.baeldung.entity.Person;
|
||||
import com.baeldung.entity.QPerson;
|
||||
import com.querydsl.core.group.GroupBy;
|
||||
import com.querydsl.jpa.impl.JPAQuery;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Repository
|
||||
public class PersonDaoImpl implements PersonDao {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Override
|
||||
public Person save(final Person person) {
|
||||
em.persist(person);
|
||||
return person;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Person> findPersonsByFirstnameQueryDSL(final String firstname) {
|
||||
final JPAQuery<Person> query = new JPAQuery<>(em);
|
||||
final QPerson person = QPerson.person;
|
||||
|
||||
return query.from(person).where(person.firstname.eq(firstname)).fetch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Person> findPersonsByFirstnameAndSurnameQueryDSL(final String firstname, final String surname) {
|
||||
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))).fetch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Person> findPersonsByFirstnameInDescendingOrderQueryDSL(final String firstname) {
|
||||
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()).fetch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int findMaxAge() {
|
||||
final JPAQuery<Person> query = new JPAQuery<>(em);
|
||||
final QPerson person = QPerson.person;
|
||||
|
||||
return query.from(person).select(person.age.max()).fetchFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Integer> findMaxAgeByName() {
|
||||
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,69 @@
|
||||
package com.baeldung.entity;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column
|
||||
private String firstname;
|
||||
|
||||
@Column
|
||||
private String surname;
|
||||
|
||||
@Column
|
||||
private int age;
|
||||
|
||||
Person() {
|
||||
}
|
||||
|
||||
public Person(final String firstname, final String surname) {
|
||||
this.firstname = firstname;
|
||||
this.surname = surname;
|
||||
}
|
||||
|
||||
public Person(final String firstname, final String surname, final int age) {
|
||||
this(firstname, surname);
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstname() {
|
||||
return firstname;
|
||||
}
|
||||
|
||||
public void setFirstname(final String firstname) {
|
||||
this.firstname = firstname;
|
||||
}
|
||||
|
||||
public String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
public void setSurname(final String surname) {
|
||||
this.surname = surname;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(final int age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* (c) Центр ИТ, 2016. Все права защищены.
|
||||
*/
|
||||
package com.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;
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* (c) Центр ИТ, 2016. Все права защищены.
|
||||
*/
|
||||
package com.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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user