Skip to content

Extended SoftDelete support to QueryDslPredicateExecutors #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 13 additions & 30 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version>
<version>1.5.0.DATAJPA-307-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand All @@ -23,14 +23,13 @@
<dist.key>DATAJPA</dist.key>

<eclipselink>2.4.0</eclipselink>
<hibernate>3.6.10.Final</hibernate>
<hsqldb1>1.8.0.10</hsqldb1>
<hibernate>4.1.10.Final</hibernate>
<jpa>2.0.0</jpa>
<openjpa>2.2.1</openjpa>
<springdata.commons>1.7.0.BUILD-SNAPSHOT</springdata.commons>
<springdata.commons>1.7.0.DATACMNS-293-SNAPSHOT</springdata.commons>

</properties>

<profiles>
<profile>
<id>hibernate-41</id>
Expand Down Expand Up @@ -128,6 +127,14 @@
<scope>test</scope>
</dependency>

<!-- Do not upgrade to 173 as this drops support for indexes on BLOBs (which OpenJPA apparently turns -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.172</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
Expand Down Expand Up @@ -161,18 +168,6 @@
<artifactId>openjpa-persistence-jdbc</artifactId>
<version>${openjpa}</version>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<!-- Transitive dependency pulls in JUnit 3.8.1 -->
<!-- http://sourceforge.net/tracker/?func=detail&aid=2572567&group_id=31479&atid=402282 -->
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- QueryDsl -->
Expand Down Expand Up @@ -241,12 +236,6 @@
<version>${openjpa}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsqldb1}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<configuration>
<excludes>
Expand Down Expand Up @@ -318,12 +307,6 @@
<include>**/OpenJpa*Tests.java</include>
</includes>
<argLine>-javaagent:${settings.localRepository}/org/apache/openjpa/openjpa/${openjpa}/openjpa-${openjpa}.jar</argLine>
<classpathDependencyExcludes>
<classpathDepencyExclude>org.hsqldb:hsqldb</classpathDepencyExclude>
</classpathDependencyExcludes>
<additionalClasspathElements>
<additionalClasspathElement>${settings.localRepository}/org/hsqldb/hsqldb/${hsqldb1}/hsqldb-${hsqldb1}.jar</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</execution>
</executions>
Expand Down Expand Up @@ -417,7 +400,7 @@
<repositories>
<repository>
<id>spring-libs-snapshot</id>
<url>http://repo.springsource.org/libs-snapshot-local</url>
<url>http://repo.springsource.org/libs-snapshot</url>
</repository>
</repositories>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository.augment;

import java.lang.annotation.Annotation;

import org.springframework.data.jpa.repository.support.JpaCriteriaQueryContext;
import org.springframework.data.jpa.repository.support.JpaQueryContext;
import org.springframework.data.jpa.repository.support.JpaUpdateContext;
import org.springframework.data.repository.augment.AnnotationBasedQueryAugmentor;

/**
* @author Oliver Gierke
*/
public abstract class AbstractJpaAnnotationBasedQueryAugmentor<T extends Annotation> extends
AnnotationBasedQueryAugmentor<T, JpaCriteriaQueryContext<?, ?>, JpaQueryContext, JpaUpdateContext<?>> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository.augment;


import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;

import org.springframework.beans.BeanWrapper;
import org.springframework.data.jpa.repository.support.JpaCriteriaQueryContext;
import org.springframework.data.jpa.repository.support.JpaQueryContext;
import org.springframework.data.jpa.repository.support.JpaUpdateContext;
import org.springframework.data.repository.SoftDelete;
import org.springframework.data.repository.augment.AbstractSoftDeleteQueryAugmentor;
import org.springframework.data.repository.augment.QueryContext.QueryMode;

/**
* JPA implementation of {@link AbstractSoftDeleteQueryAugmentor} to transparently turn delete calls into entity
* updates. Also filters queries accordingly.
*
* @author Oliver Gierke
*/
public class JpaSoftDeleteQueryAugmentor extends
AbstractSoftDeleteQueryAugmentor<JpaCriteriaQueryContext<?, ?>, JpaQueryContext, JpaUpdateContext<?>> {

/*
* (non-Javadoc)
* @see org.springframework.data.repository.augment.AnnotationBasedQueryAugmentor#prepareNativeQuery(org.springframework.data.repository.augment.QueryContext, java.lang.annotation.Annotation)
*/
@Override
protected JpaQueryContext prepareNativeQuery(JpaQueryContext context, SoftDelete expression) {

if (!context.getMode().in(QueryMode.FIND)) {
return context;
}

String string = context.getQueryString();
// TODO: Augment query;

return context.withQuery(string);
}

/*
* (non-Javadoc)
* @see org.springframework.data.jpa.repository.sample.JpaSoftDeleteQueryAugmentor#prepareQuery(org.springframework.data.jpa.repository.support.JpaCriteriaQueryContext, org.springframework.data.jpa.repository.support.GlobalFilter)
*/
@Override
protected JpaCriteriaQueryContext<?, ?> prepareQuery(JpaCriteriaQueryContext<?, ?> context, SoftDelete expression) {

CriteriaQuery<?> criteriaQuery = context.getQuery();
CriteriaBuilder builder = context.getCriteriaBuilder();

Predicate predicate = builder.equal(context.getRoot().get(expression.value()), expression.flagMode().activeValue());
Predicate restriction = criteriaQuery.getRestriction();

criteriaQuery.where(restriction == null ? predicate : builder.and(restriction, predicate));

return context;
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.augment.AbstractSoftDeleteQueryAugmentor#updateDeletedState(java.lang.Object)
*/
@Override
public void updateDeletedState(Object entity, JpaUpdateContext<?> context) {
context.getEntityManager().merge(entity);
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.augment.AbstractSoftDeleteQueryAugmentor#prepareBeanWrapper(org.springframework.data.repository.augment.UpdateContext)
*/
@Override
protected BeanWrapper createBeanWrapper(JpaUpdateContext<?> context) {
return new PropertyChangeEnsuringBeanWrapper(context.getEntity());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository.augment;

import javax.persistence.Query;

import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.support.JpaQueryContext;

/**
* @author Oliver Gierke
*/
public class LockModeQueryAugmentor extends AbstractJpaAnnotationBasedQueryAugmentor<Lock> {

/*
* (non-Javadoc)
* @see org.springframework.data.repository.augment.AnnotationBasedQueryAugmentor#prepareNativeQuery(org.springframework.data.repository.augment.QueryContext, java.lang.annotation.Annotation)
*/
@Override
protected JpaQueryContext prepareNativeQuery(JpaQueryContext context, Lock expression) {

Query query = context.getQuery();
query.setLockMode(expression.value());

return context;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository.augment;

import java.lang.reflect.Method;

import org.springframework.data.support.DirectFieldAccessFallbackBeanWrapper;
import org.springframework.util.ReflectionUtils;

/**
* Custom {@link DirectFieldAccessFallbackBeanWrapper} to hook in additional functionality when setting a property by
* field access.
*
* @author Oliver Gierke
*/
class PropertyChangeEnsuringBeanWrapper extends DirectFieldAccessFallbackBeanWrapper {

public PropertyChangeEnsuringBeanWrapper(Object entity) {
super(entity);
}

/**
* We in case of setting the value using field access, we need to make sure that EclipseLink detects the change.
* Hence we check for an EclipseLink specific generated method that is used to record the changes and invoke it if
* available.
*
* @see org.springframework.data.support.DirectFieldAccessFallbackBeanWrapper#setPropertyUsingFieldAccess(java.lang.String,
* java.lang.Object)
*/
@Override
protected void setPropertyUsingFieldAccess(String propertyName, Object value) {

Object oldValue = getPropertyValue(propertyName);
super.setPropertyUsingFieldAccess(propertyName, oldValue);
triggerPropertyChangeMethodIfAvailable(propertyName, oldValue, oldValue);
}

private void triggerPropertyChangeMethodIfAvailable(String propertyName, Object oldValue, Object value) {

Method method = ReflectionUtils.findMethod(getWrappedClass(), "_persistence_propertyChange", String.class,
Object.class, Object.class);

if (method == null) {
return;
}

ReflectionUtils.invokeMethod(method, getWrappedInstance(), propertyName, oldValue, value);
}
}
Loading