group persistence modules (#2890)
* move security content from spring-security-rest-full * swagger update * move query language to new module * rename spring-security-rest-full to spring-rest-full * group persistence modules
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
dbeb5f8ba4
commit
26c50909be
@@ -0,0 +1,22 @@
|
||||
package org.baeldung.dao;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.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,68 @@
|
||||
package org.baeldung.dao;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import org.baeldung.entity.Person;
|
||||
import org.baeldung.entity.QPerson;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.querydsl.core.group.GroupBy;
|
||||
import com.querydsl.jpa.impl.JPAQuery;
|
||||
|
||||
@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 org.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 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;
|
||||
}
|
||||
}
|
||||
+55
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
|
||||
<!-- PersistenceUnit for datastore -->
|
||||
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
|
||||
<properties>
|
||||
<property name="hibernate.hbm2ddl.auto" value="update" />
|
||||
<property name="hibernate.show_sql" value="true" />
|
||||
<property name="hibernate.transaction.flush_before_completion"
|
||||
value="true" />
|
||||
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
|
||||
</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>
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user