#819865 : added support for custom entity names

This commit is contained in:
Timo Westkämper 2011-08-03 06:17:18 +00:00
parent 30ff421701
commit b1fc2b6f42
3 changed files with 21 additions and 3 deletions

View File

@ -17,6 +17,7 @@ import java.util.Set;
import javax.annotation.Nullable;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
@ -91,11 +92,14 @@ public class JPQLSerializer extends SerializerBase<JPQLSerializer> {
if (je.getTarget() instanceof EntityPath<?>) {
EntityPath<?> pe = (EntityPath<?>) je.getTarget();
if (pe.getMetadata().getParent() == null) {
if (pe.getType().getPackage() != null){
Entity entityAnnotation = pe.getAnnotatedElement().getAnnotation(Entity.class);
if (entityAnnotation != null && entityAnnotation.name().length() > 0){
append(entityAnnotation.name());
} else if (pe.getType().getPackage() != null) {
String pn = pe.getType().getPackage().getName();
String typeName = pe.getType().getName().substring(pn.length() + 1);
append(typeName);
}else{
} else {
append(pe.getType().getName());
}
append(" ");

View File

@ -12,10 +12,23 @@ import org.junit.Test;
import com.mysema.query.DefaultQueryMetadata;
import com.mysema.query.JoinType;
import com.mysema.query.QueryMetadata;
import com.mysema.query.jpa.domain.Location;
import com.mysema.query.jpa.domain.QEmployee;
import com.mysema.query.types.EntityPath;
import com.mysema.query.types.path.EntityPathBase;
import com.mysema.query.types.path.NumberPath;
public class JPQLSerializerTest {
@Test
public void FromWithCustomEntityName() {
JPQLSerializer serializer = new JPQLSerializer(HQLTemplates.DEFAULT);
EntityPath<Location> entityPath = new EntityPathBase<Location>(Location.class, "entity");
QueryMetadata md = new DefaultQueryMetadata();
md.addJoin(JoinType.DEFAULT, entityPath);
serializer.serialize(md, false, null);
assertEquals("from Location2 entity", serializer.toString());
}
@Test
public void NormalizeNumericArgs() {
@ -39,3 +52,4 @@ public class JPQLSerializerTest {
assertEquals("delete from Employee employee\nwhere employee.lastName is null", serializer.toString());
}
}

View File

@ -11,7 +11,7 @@ import javax.persistence.Id;
/**
* The Class Location.
*/
@Entity
@Entity(name="Location2")
public class Location {
@Id
long id;