Created
March 13, 2016 13:55
-
-
Save arthurportas/eb3f722c3c8194b52e78 to your computer and use it in GitHub Desktop.
Sample spring-data jpa query with querydsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Primary; | |
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; | |
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | |
import org.springframework.jdbc.datasource.DriverManagerDataSource; | |
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | |
import org.springframework.orm.jpa.vendor.Database; | |
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; | |
import org.springframework.transaction.annotation.EnableTransactionManagement; | |
import javax.persistence.EntityManager; | |
import javax.sql.DataSource; | |
import java.util.Properties; | |
@Configuration | |
@EnableJpaRepositories(basePackages = {"com.arthurportas.repositories"}) | |
@EnableTransactionManagement | |
public class ApplicationConfig { | |
//injected property values | |
@Value("${datasource.jdbcUrl}") | |
private String URL; | |
@Value("${datasource.user}") | |
private String USER; | |
@Value("${datasource..password}") | |
private String PASSWORD; | |
@Bean | |
public DataSource dataSource() { | |
DriverManagerDataSource dataSource = new DriverManagerDataSource(); | |
dataSource.setUrl(URL); | |
dataSource.setUsername(USER); | |
dataSource.setPassword(PASSWORD); | |
return dataSource; | |
} | |
@Bean | |
public LocalContainerEntityManagerFactoryBean entityManagerFactory() { | |
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); | |
lef.setDataSource(dataSource()); | |
lef.setPackagesToScan("com.arthurportas.entities"); | |
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); | |
hibernateJpaVendorAdapter.setDatabase(Database.POSTGRESQL); | |
lef.setJpaVendorAdapter(hibernateJpaVendorAdapter); | |
lef.setJpaProperties(additionalProperties()); | |
lef.setPersistenceUnitName("postgresPU"); | |
return lef; | |
} | |
Properties additionalProperties() { | |
Properties properties = new Properties(); | |
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect"); | |
properties.setProperty("hibernate.id.new_generator_mappings", "true"); | |
return properties; | |
} | |
@Bean | |
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { | |
return new PersistenceExceptionTranslationPostProcessor(); | |
} | |
@Bean | |
public EntityManager manager() { | |
return entityManagerFactory().getObject().createEntityManager(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.arthurportas</groupId> | |
<artifactId>com.arthurportas.data.persistence</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>SPRING_DATA_JPA_QUERY_DSL</name> | |
<description></description> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.3.3.RELEASE</version> | |
<relativePath/> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<java.version>1.8</java.version> | |
<querydsl.version>4.0.7</querydsl.version> | |
<querydsl.apt-maven-plugin.version>1.1.3</querydsl.apt-maven-plugin.version> | |
<com.mysema.querydsl.version>3.7.1</com.mysema.querydsl.version> | |
<postgresql.version>9.4-1201-jdbc41</postgresql.version> | |
</properties> | |
<dependencies> | |
<!-- Querydsl --> | |
<dependency> | |
<groupId>com.mysema.querydsl</groupId> | |
<artifactId>querydsl-apt</artifactId> | |
<version>${com.mysema.querydsl.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>com.mysema.querydsl</groupId> | |
<artifactId>querydsl-jpa</artifactId> | |
<version>${com.mysema.querydsl.version}</version> | |
<exclusions> | |
<exclusion> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-api</artifactId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
<!--I've left some commented code to make explicit dependencies artifacts difference to the ones currently compatible with spring-data--> | |
<!--<dependency>--> | |
<!--<groupId>com.querydsl</groupId>--> | |
<!--<artifactId>querydsl-apt</artifactId>--> | |
<!--<version>${querydsl.version}</version>--> | |
<!--</dependency>--> | |
<!--<dependency>--> | |
<!--<groupId>com.querydsl</groupId>--> | |
<!--<artifactId>querydsl-jpa</artifactId>--> | |
<!--<version>${querydsl.version}</version>--> | |
<!--</dependency>--> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-jpa</artifactId> | |
</dependency> | |
<!--database--> | |
<dependency> | |
<groupId>org.postgresql</groupId> | |
<artifactId>postgresql</artifactId> | |
<version>${postgresql.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<!-- Generates the JPA static meta model --> | |
<!--I'm only activating JPAAnnotationProcessor--> | |
<plugin> | |
<groupId>com.mysema.maven</groupId> | |
<artifactId>apt-maven-plugin</artifactId> | |
<version>${querydsl.apt-maven-plugin.version}</version> | |
<executions> | |
<execution> | |
<id>process-common-model</id> | |
<goals> | |
<goal>process</goal> | |
</goals> | |
<configuration> | |
<!--target folder with generated metamodels Qxxxx.java--> | |
<outputDirectory>target/generated-sources/apt</outputDirectory> | |
<processors> | |
<!--<processor>com.mysema.query.apt.hibernate.HibernateAnnotationProcessor</processor>--> | |
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor> | |
<!--<processor>com.querydsl.apt.hibernate.HibernateAnnotationProcessor</processor>--> | |
<!--<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>--> | |
</processors> | |
<!--<options>--> | |
<!--<querydsl.entityAccessors>true</querydsl.entityAccessors>--> | |
<!--<querydsl.createDefaultVariable>true</querydsl.createDefaultVariable>--> | |
<!--<querydsl.packageSuffix>.qdsl</querydsl.packageSuffix>--> | |
<!--</options>--> | |
</configuration> | |
</execution> | |
</executions> | |
<dependencies> | |
<dependency> | |
<groupId>com.mysema.querydsl</groupId> | |
<artifactId>querydsl-apt</artifactId> | |
<version>${com.mysema.querydsl.version}</version> | |
</dependency> | |
</dependencies> | |
</plugin> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.base.Objects; | |
import java.io.Serializable; | |
import javax.persistence.Entity; | |
import javax.persistence.SequenceGenerator; | |
import javax.persistence.Table; | |
@Entity | |
@SequenceGenerator(name = User.USER_SEQUENCE_NAME, sequenceName = "users_sequence_id_seq", allocationSize = 1, initialValue = 1) | |
@Table(name = "users") | |
public class User implements Serializable { | |
private static final long serialVersionUID = 20160313L; | |
protected static final String USER_SEQUENCE_NAME = "Users_SEQ"; | |
private String name; | |
private String email; | |
//accessor methods + equals + hashcode + toString | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.data.querydsl.QueryDslPredicateExecutor; | |
import org.springframework.data.repository.CrudRepository; | |
import com.arthurportas.entities.User; | |
import java.util.List; | |
public interface UserRepository extends CrudRepository<User, Long>, UserRepositoryCustom { | |
User findById(long id); | |
List<User> findAll(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.arthurportas.User; | |
import java.util.List; | |
public interface UserRepositoryCustom { | |
Iterable<User> findAllUsers(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.data.jpa.repository.support.QueryDslRepositorySupport; | |
import com.arthurportas.entities.QUser; | |
import java.util.List; | |
public class UserRepositoryImpl extends QueryDslRepositorySupport implements UserRepositoryCustom { | |
public UserRepositoryImpl() { | |
super(User.class); | |
} | |
@Override | |
public Iterable<User> findAllUsers() { | |
QUser user = QUser.user; | |
//@formatter:off | |
return from(user) | |
.list(userPermission); | |
//@formatter:on | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment