mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-27 21:01:15 +08:00
#596094 : added null support in update clauses
This commit is contained in:
parent
668f501094
commit
79e00ea47e
@ -22,6 +22,7 @@ import com.mysema.query.hql.JPQLTemplates;
|
||||
import com.mysema.query.types.Expr;
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.expr.EBoolean;
|
||||
import com.mysema.query.types.path.NullExpr;
|
||||
import com.mysema.query.types.path.PEntity;
|
||||
|
||||
/**
|
||||
@ -66,7 +67,11 @@ public class HibernateUpdateClause implements
|
||||
|
||||
@Override
|
||||
public <T> HibernateUpdateClause set(Path<T> path, T value) {
|
||||
metadata.addProjection(path.asExpr().eq(value));
|
||||
if (value != null){
|
||||
metadata.addProjection(path.asExpr().eq(value));
|
||||
}else{
|
||||
metadata.addProjection(path.asExpr().eq(new NullExpr<T>(path.getType())));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -74,7 +79,12 @@ public class HibernateUpdateClause implements
|
||||
@Override
|
||||
public HibernateUpdateClause set(List<? extends Path<?>> paths, List<?> values) {
|
||||
for (int i = 0; i < paths.size(); i++){
|
||||
metadata.addProjection(((Expr)paths.get(i).asExpr()).eq(values.get(i)));
|
||||
if (values.get(i) != null){
|
||||
metadata.addProjection(((Expr)paths.get(i).asExpr()).eq(values.get(i)));
|
||||
}else{
|
||||
metadata.addProjection(((Expr)paths.get(i).asExpr()).eq(new NullExpr(paths.get(i).getType())));
|
||||
}
|
||||
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -21,6 +21,7 @@ import com.mysema.query.hql.JPQLTemplates;
|
||||
import com.mysema.query.types.Expr;
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.expr.EBoolean;
|
||||
import com.mysema.query.types.path.NullExpr;
|
||||
import com.mysema.query.types.path.PEntity;
|
||||
|
||||
/**
|
||||
@ -60,7 +61,12 @@ public class JPAUpdateClause implements UpdateClause<JPAUpdateClause>{
|
||||
|
||||
@Override
|
||||
public <T> JPAUpdateClause set(Path<T> path, T value) {
|
||||
metadata.addProjection(path.asExpr().eq(value));
|
||||
if (value != null){
|
||||
metadata.addProjection(path.asExpr().eq(value));
|
||||
}else{
|
||||
metadata.addProjection(path.asExpr().eq(new NullExpr<T>(path.getType())));
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -68,7 +74,11 @@ public class JPAUpdateClause implements UpdateClause<JPAUpdateClause>{
|
||||
@Override
|
||||
public JPAUpdateClause set(List<? extends Path<?>> paths, List<?> values) {
|
||||
for (int i = 0; i < paths.size(); i++){
|
||||
metadata.addProjection(((Expr)paths.get(i).asExpr()).eq(values.get(i)));
|
||||
if (values.get(i) != null){
|
||||
metadata.addProjection(((Expr)paths.get(i).asExpr()).eq(values.get(i)));
|
||||
}else{
|
||||
metadata.addProjection(((Expr)paths.get(i).asExpr()).eq(new NullExpr(paths.get(i).getType())));
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -112,6 +112,19 @@ public class IntegrationTest extends ParsingTest {
|
||||
assertEquals(0l, query().from(cat).where(cat.name.eq("Bob")).count());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate_with_null(){
|
||||
session.save(new Cat("Bob",10));
|
||||
session.save(new Cat("Steve",11));
|
||||
|
||||
QCat cat = QCat.cat;
|
||||
long amount = update(cat).where(cat.name.eq("Bob"))
|
||||
.set(cat.name, null)
|
||||
.set(cat.alive, false)
|
||||
.execute();
|
||||
assertEquals(1, amount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete(){
|
||||
session.save(new Cat("Bob",10));
|
||||
@ -120,9 +133,7 @@ public class IntegrationTest extends ParsingTest {
|
||||
QCat cat = QCat.cat;
|
||||
long amount = delete(cat).where(cat.name.eq("Bob"))
|
||||
.execute();
|
||||
assertEquals(1, amount);
|
||||
|
||||
assertEquals(0l, query().from(cat).where(cat.name.eq("Bob")).count());
|
||||
assertEquals(1, amount);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@ -13,6 +13,7 @@ import com.mysema.query.dml.UpdateClause;
|
||||
import com.mysema.query.types.Expr;
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.expr.EBoolean;
|
||||
import com.mysema.query.types.path.NullExpr;
|
||||
|
||||
/**
|
||||
* UpdateClause implementation for JDO
|
||||
@ -48,14 +49,23 @@ public class JDOQLUpdateClause implements UpdateClause<JDOQLUpdateClause>{
|
||||
@Override
|
||||
public JDOQLUpdateClause set(List<? extends Path<?>> paths, List<?> values) {
|
||||
for (int i = 0; i < paths.size(); i++){
|
||||
metadata.addProjection(((Expr)paths.get(i).asExpr()).eq(values.get(i)));
|
||||
if (values.get(i) != null){
|
||||
metadata.addProjection(((Expr)paths.get(i).asExpr()).eq(values.get(i)));
|
||||
}else{
|
||||
metadata.addProjection(((Expr)paths.get(i).asExpr()).eq(new NullExpr(paths.get(i).getType())));
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> JDOQLUpdateClause set(Path<T> path, T value) {
|
||||
metadata.addProjection(path.asExpr().eq(value));
|
||||
if (value != null){
|
||||
metadata.addProjection(path.asExpr().eq(value));
|
||||
}else{
|
||||
metadata.addProjection(path.asExpr().eq(new NullExpr<T>(path.getType())));
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user