mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
Merge pull request #928 from querydsl/i926
Add suport Table/Column annotations in JPA native queries
This commit is contained in:
commit
af897570c6
@ -13,15 +13,18 @@
|
||||
*/
|
||||
package com.mysema.query.jpa;
|
||||
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Column;
|
||||
import java.util.*;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.mysema.query.JoinExpression;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
import com.mysema.query.sql.*;
|
||||
import com.mysema.query.types.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* NativeSQLSerializer extends the SQLSerializer class to extract referenced entity paths and change
|
||||
* some serialization formats
|
||||
@ -44,6 +47,33 @@ public final class NativeSQLSerializer extends SQLSerializer {
|
||||
this.wrapEntityProjections = wrapEntityProjections;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void appendAsColumnName(Path<?> path) {
|
||||
if (path.getAnnotatedElement().isAnnotationPresent(Column.class)) {
|
||||
SQLTemplates templates = getTemplates();
|
||||
Column column = path.getAnnotatedElement().getAnnotation(Column.class);
|
||||
append(templates.quoteIdentifier(column.name()));
|
||||
} else {
|
||||
super.appendAsColumnName(path);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleJoinTarget(JoinExpression je) {
|
||||
SQLTemplates templates = getTemplates();
|
||||
Class<?> type = je.getTarget().getType();
|
||||
if (type.getAnnotation(Table.class) != null && templates.isSupportsAlias()) {
|
||||
Table table = type.getAnnotation(Table.class);
|
||||
if (!table.schema().isEmpty() && templates.isPrintSchema()) {
|
||||
appendSchemaName(table.schema());
|
||||
append(".");
|
||||
}
|
||||
appendTableName(table.name());
|
||||
append(templates.getTableAlias());
|
||||
}
|
||||
super.handleJoinTarget(je);
|
||||
}
|
||||
|
||||
private boolean isAlias(Expression<?> expr) {
|
||||
return expr instanceof Operation && ((Operation<?>)expr).getOperator() == Ops.ALIAS;
|
||||
}
|
||||
@ -77,6 +107,8 @@ public final class NativeSQLSerializer extends SQLSerializer {
|
||||
aliases.put(args[i], alias);
|
||||
args[i] = ExpressionUtils.as(args[i], alias);
|
||||
modified = true;
|
||||
} else if (path.getAnnotatedElement().isAnnotationPresent(Column.class)) {
|
||||
aliases.put(path, path.getAnnotatedElement().getAnnotation(Column.class).name());
|
||||
} else {
|
||||
aliases.put(path, ColumnMetadata.getName(path));
|
||||
}
|
||||
@ -86,7 +118,12 @@ public final class NativeSQLSerializer extends SQLSerializer {
|
||||
for (int j = 0; j < fargs.size(); j++) {
|
||||
if (fargs.get(j) instanceof Path) {
|
||||
Path<?> path = (Path<?>) fargs.get(j);
|
||||
String columnName = ColumnMetadata.getName(path);
|
||||
String columnName;
|
||||
if (path.getAnnotatedElement().isAnnotationPresent(Column.class)) {
|
||||
columnName = path.getAnnotatedElement().getAnnotation(Column.class).name();
|
||||
} else {
|
||||
columnName = ColumnMetadata.getName(path);
|
||||
}
|
||||
if (!used.add(columnName)) {
|
||||
String alias = "col__"+(i+1)+"_"+(j+1);
|
||||
aliases.put(path, alias);
|
||||
|
||||
@ -1,23 +1,15 @@
|
||||
package com.mysema.query;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.jpa.AbstractSQLQuery;
|
||||
import com.mysema.query.jpa.domain.Cat;
|
||||
import com.mysema.query.jpa.domain.Color;
|
||||
import com.mysema.query.jpa.domain.QCat;
|
||||
import com.mysema.query.jpa.domain.QCompany;
|
||||
import com.mysema.query.jpa.domain.sql.SAnimal;
|
||||
import com.mysema.query.sql.SQLSubQuery;
|
||||
import com.mysema.query.types.ConstructorExpression;
|
||||
@ -27,6 +19,9 @@ import com.mysema.query.types.SubQueryExpression;
|
||||
import com.mysema.query.types.expr.DateExpression;
|
||||
import com.mysema.query.types.expr.Wildcard;
|
||||
import com.mysema.testutil.ExcludeIn;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public abstract class AbstractSQLTest {
|
||||
|
||||
@ -159,6 +154,12 @@ public abstract class AbstractSQLTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void EntityQueries7() {
|
||||
QCompany company = QCompany.company;
|
||||
query().from(company).list(company.officialName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void In() {
|
||||
assertEquals(6l, query().from(cat).where(cat.dtype.in("C", "CX")).count());
|
||||
|
||||
@ -13,16 +13,9 @@
|
||||
*/
|
||||
package com.mysema.query.jpa.domain;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.batoo.jpa.annotations.Index;
|
||||
|
||||
/**
|
||||
@ -51,4 +44,7 @@ public class Company {
|
||||
public int id;
|
||||
|
||||
public String name;
|
||||
|
||||
@Column(name="official_name")
|
||||
public String officialName;
|
||||
}
|
||||
|
||||
@ -79,7 +79,7 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
this.dml = dml;
|
||||
}
|
||||
|
||||
private void appendAsColumnName(Path<?> path) {
|
||||
protected void appendAsColumnName(Path<?> path) {
|
||||
String column = ColumnMetadata.getName(path);
|
||||
if (path.getMetadata().getParent() instanceof RelationalPath) {
|
||||
RelationalPath<?> parent = (RelationalPath<?>)path.getMetadata().getParent();
|
||||
@ -92,11 +92,11 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
return configuration.getOverride(path.getSchemaAndTable());
|
||||
}
|
||||
|
||||
private void appendSchemaName(String schema) {
|
||||
protected void appendSchemaName(String schema) {
|
||||
append(templates.quoteIdentifier(schema));
|
||||
}
|
||||
|
||||
private void appendTableName(String table) {
|
||||
protected void appendTableName(String table) {
|
||||
append(templates.quoteIdentifier(table));
|
||||
}
|
||||
|
||||
@ -162,7 +162,7 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
handleTemplate(TemplateFactory.DEFAULT.create(template), Arrays.asList(args));
|
||||
}
|
||||
|
||||
private void handleJoinTarget(JoinExpression je) {
|
||||
protected void handleJoinTarget(JoinExpression je) {
|
||||
// type specifier
|
||||
if (je.getTarget() instanceof RelationalPath && templates.isSupportsAlias()) {
|
||||
final RelationalPath<?> pe = (RelationalPath<?>) je.getTarget();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user