mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
we could also use set
This commit is contained in:
parent
aa4bdd4d6d
commit
65865cfb78
@ -260,7 +260,7 @@ public class JPQLSerializer extends SerializerBase<JPQLSerializer> {
|
||||
}
|
||||
}
|
||||
|
||||
public void serializeForInsert(QueryMetadata md, List<Path<?>> columns, SubQueryExpression<?> query) {
|
||||
public void serializeForInsert(QueryMetadata md, List<Path<?>> columns, SubQueryExpression<?> query, Map<Path<?>, Expression<?>> inserts) {
|
||||
append(INSERT);
|
||||
JoinExpression je = md.getJoins().get(0);
|
||||
final EntityPath<?> pe = (EntityPath<?>) je.getTarget();
|
||||
@ -276,7 +276,22 @@ public class JPQLSerializer extends SerializerBase<JPQLSerializer> {
|
||||
first = false;
|
||||
}
|
||||
append(")\n");
|
||||
serialize(query.getMetadata(), false, null);
|
||||
|
||||
if (inserts != null && inserts.entrySet().size() > 0) {
|
||||
first = true;
|
||||
for (Map.Entry<Path<?>, Expression<?>> entry : inserts.entrySet()) {
|
||||
if (!first) {
|
||||
append(", ");
|
||||
}
|
||||
handle(entry.getKey());
|
||||
append(" = ");
|
||||
handle(entry.getValue());
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
serialize(query.getMetadata(), false, null);
|
||||
}
|
||||
}
|
||||
|
||||
public void serializeForUpdate(QueryMetadata md, Map<Path<?>, Expression<?>> updates) {
|
||||
|
||||
@ -24,6 +24,7 @@ import org.hibernate.Query;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.StatelessSession;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.querydsl.core.JoinType;
|
||||
import com.querydsl.core.dml.InsertClause;
|
||||
import com.querydsl.core.support.QueryMixin;
|
||||
@ -31,6 +32,7 @@ import com.querydsl.core.types.EntityPath;
|
||||
import com.querydsl.core.types.Expression;
|
||||
import com.querydsl.core.types.Path;
|
||||
import com.querydsl.core.types.SubQueryExpression;
|
||||
import com.querydsl.core.types.dsl.Expressions;
|
||||
import com.querydsl.jpa.HQLTemplates;
|
||||
import com.querydsl.jpa.JPAQueryMixin;
|
||||
import com.querydsl.jpa.JPQLSerializer;
|
||||
@ -47,6 +49,8 @@ public class HibernateInsertClause implements
|
||||
|
||||
private final QueryMixin<?> queryMixin = new JPAQueryMixin<Void>();
|
||||
|
||||
private final Map<Path<?>, Expression<?>> inserts = Maps.newLinkedHashMap();
|
||||
|
||||
private final List<Path<?>> columns = new ArrayList<Path<?>>();
|
||||
|
||||
private SubQueryExpression<?> subQuery;
|
||||
@ -79,7 +83,7 @@ public class HibernateInsertClause implements
|
||||
@Override
|
||||
public long execute() {
|
||||
JPQLSerializer serializer = new JPQLSerializer(templates, null);
|
||||
serializer.serializeForInsert(queryMixin.getMetadata(), columns, subQuery);
|
||||
serializer.serializeForInsert(queryMixin.getMetadata(), columns, subQuery, inserts);
|
||||
Map<Object, String> constants = serializer.getConstantToLabel();
|
||||
|
||||
Query query = session.createQuery(serializer.toString());
|
||||
@ -115,7 +119,7 @@ public class HibernateInsertClause implements
|
||||
@Override
|
||||
public String toString() {
|
||||
JPQLSerializer serializer = new JPQLSerializer(templates, null);
|
||||
serializer.serializeForInsert(queryMixin.getMetadata(), columns, subQuery);
|
||||
serializer.serializeForInsert(queryMixin.getMetadata(), columns, subQuery, inserts);
|
||||
return serializer.toString();
|
||||
}
|
||||
|
||||
@ -126,20 +130,28 @@ public class HibernateInsertClause implements
|
||||
|
||||
@Override
|
||||
public <T> HibernateInsertClause set(Path<T> path, T value) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
if (value != null) {
|
||||
inserts.put(path, Expressions.constant(value));
|
||||
} else {
|
||||
setNull(path);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> HibernateInsertClause set(Path<T> path, Expression<? extends T> expression) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
if (expression != null) {
|
||||
inserts.put(path, expression);
|
||||
} else {
|
||||
setNull(path);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> HibernateInsertClause setNull(Path<T> path) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
inserts.put(path, Expressions.nullExpression(path));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -23,6 +23,7 @@ import javax.persistence.EntityManager;
|
||||
import javax.persistence.LockModeType;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.querydsl.core.JoinType;
|
||||
import com.querydsl.core.dml.InsertClause;
|
||||
import com.querydsl.core.support.QueryMixin;
|
||||
@ -30,6 +31,7 @@ import com.querydsl.core.types.EntityPath;
|
||||
import com.querydsl.core.types.Expression;
|
||||
import com.querydsl.core.types.Path;
|
||||
import com.querydsl.core.types.SubQueryExpression;
|
||||
import com.querydsl.core.types.dsl.Expressions;
|
||||
import com.querydsl.jpa.JPAQueryMixin;
|
||||
import com.querydsl.jpa.JPQLSerializer;
|
||||
import com.querydsl.jpa.JPQLTemplates;
|
||||
@ -46,6 +48,8 @@ public class JPAInsertClause implements InsertClause<JPAInsertClause> {
|
||||
|
||||
private final List<Path<?>> columns = new ArrayList<Path<?>>();
|
||||
|
||||
private final Map<Path<?>, Expression<?>> inserts = Maps.newLinkedHashMap();
|
||||
|
||||
private final List<Expression<?>> values = new ArrayList<Expression<?>>();
|
||||
|
||||
private final EntityManager entityManager;
|
||||
@ -70,7 +74,7 @@ public class JPAInsertClause implements InsertClause<JPAInsertClause> {
|
||||
@Override
|
||||
public long execute() {
|
||||
JPQLSerializer serializer = new JPQLSerializer(templates, entityManager);
|
||||
serializer.serializeForInsert(queryMixin.getMetadata(), columns, subQuery);
|
||||
serializer.serializeForInsert(queryMixin.getMetadata(), columns, subQuery, inserts);
|
||||
Map<Object,String> constants = serializer.getConstantToLabel();
|
||||
|
||||
Query query = entityManager.createQuery(serializer.toString());
|
||||
@ -89,7 +93,7 @@ public class JPAInsertClause implements InsertClause<JPAInsertClause> {
|
||||
@Override
|
||||
public String toString() {
|
||||
JPQLSerializer serializer = new JPQLSerializer(templates, entityManager);
|
||||
serializer.serializeForInsert(queryMixin.getMetadata(), columns, subQuery);
|
||||
serializer.serializeForInsert(queryMixin.getMetadata(), columns, subQuery, inserts);
|
||||
return serializer.toString();
|
||||
}
|
||||
|
||||
@ -119,20 +123,28 @@ public class JPAInsertClause implements InsertClause<JPAInsertClause> {
|
||||
|
||||
@Override
|
||||
public <T> JPAInsertClause set(Path<T> path, T value) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
if (value != null) {
|
||||
inserts.put(path, Expressions.constant(value));
|
||||
} else {
|
||||
setNull(path);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> JPAInsertClause set(Path<T> path, Expression<? extends T> expression) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
if (expression != null) {
|
||||
inserts.put(path, expression);
|
||||
} else {
|
||||
setNull(path);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> JPAInsertClause setNull(Path<T> path) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
inserts.put(path, Expressions.nullExpression(path));
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user