#656847 : added support for embedded types

This commit is contained in:
Timo Westkämper 2010-10-09 20:52:46 +00:00
parent f73236c018
commit c2e30fc248
2 changed files with 49 additions and 20 deletions

View File

@ -48,14 +48,14 @@ import com.sun.xml.internal.ws.util.StringUtils;
*/
public class HibernateDomainExporter {
// TODO : support embeddables properly
private static final Logger logger = LoggerFactory.getLogger(HibernateDomainExporter.class);
private static final List<String> propertyFields = Arrays.asList(
"property","dynamic-component","properties","any","map","set","list","bag","idbag","array","primitive-array");
private static final List<String> entityFields = Arrays.asList("component","many-to-one","one-to-one");
private static final List<String> entityFields = Arrays.asList("many-to-one","one-to-one");
private static final List<String> embeddableFields = Arrays.asList("component");
private final XMLInputFactory inFactory = XMLInputFactory.newInstance();
@ -153,7 +153,7 @@ public class HibernateDomainExporter {
try{
String packageName = null;
Stack<EntityType> types = new Stack<EntityType>();
Class<?> cl = null;
Stack<Class<?>> classes = new Stack<Class<?>>();
while (true) {
int event = reader.next();
if (event == START_ELEMENT) {
@ -163,32 +163,43 @@ public class HibernateDomainExporter {
}else if (name.endsWith("class")){
String className = reader.getAttributeValue(null, "name");
cl = Class.forName(packageName == null ? className : packageName + "." + className);
classes.push(Class.forName(packageName == null ? className : packageName + "." + className));
if (name.equals("mapped-superclass")){
types.push(createSuperType(cl));
types.push(createSuperType(classes.peek()));
}else{
types.push(createEntityType(cl));
types.push(createEntityType(classes.peek()));
}
}else if (propertyFields.contains(name)){
String propertyName = reader.getAttributeValue(null, "name");
Type propertyType = getType(cl, propertyName);
Map<Class<?>,Annotation> annotations = getAnnotations(cl, propertyName);
Type propertyType = getType(classes.peek(), propertyName);
Map<Class<?>,Annotation> annotations = getAnnotations(classes.peek(), propertyName);
Property property = createProperty(types.peek(), propertyName, propertyType, annotations);
types.peek().addProperty(property);
}else if (entityFields.contains(name)){
String propertyName = reader.getAttributeValue(null, "name");
Type propertyType = getType(cl, propertyName);
propertyType = createEntityType(Class.forName(propertyType.getFullName()));
Map<Class<?>,Annotation> annotations = getAnnotations(cl, propertyName);
Type propertyType = createEntityType(Class.forName(getType(classes.peek(), propertyName).getFullName()));
Map<Class<?>,Annotation> annotations = getAnnotations(classes.peek(), propertyName);
Property property = createProperty(types.peek(), propertyName, propertyType, annotations);
types.peek().addProperty(property);
}
}else if (embeddableFields.contains(name)){
String propertyName = reader.getAttributeValue(null, "name");
Class<?> clazz = Class.forName(getType(classes.peek(), propertyName).getFullName());
EntityType propertyType = createEmbeddableType(clazz);
Map<Class<?>,Annotation> annotations = getAnnotations(classes.peek(), propertyName);
Property property = createProperty(types.peek(), propertyName, propertyType, annotations);
types.peek().addProperty(property);
types.push(propertyType);
classes.push(clazz);
}
}else if (event == END_ELEMENT){
String name = reader.getLocalName();
if (name.equals("class") || name.endsWith("subclass")){
if (name.endsWith("class") || embeddableFields.contains(name)){
types.pop();
classes.pop();
}
} else if (event == END_DOCUMENT) {
break;
@ -216,6 +227,10 @@ public class HibernateDomainExporter {
private EntityType createEntityType(Class<?> cl) {
return createEntityType(cl, entityTypes);
}
private EntityType createEmbeddableType(Class<?> cl) {
return createEntityType(cl, embeddableTypes);
}
private EntityType createEntityType(Class<?> cl, Map<String,EntityType> types) {
if (types.containsKey(cl.getName())){

View File

@ -12,6 +12,7 @@ import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
@ -26,8 +27,6 @@ import com.mysema.query.codegen.SimpleSerializerConfig;
import com.mysema.query.jpa.domain.Domain;
public class HibernateDomainExporterTest {
// TODO : support embeddables properly
private SerializerConfig serializerConfig = SimpleSerializerConfig.getConfig(Domain.class.getPackage().getAnnotation(Config.class));
@ -97,12 +96,17 @@ public class HibernateDomainExporterTest {
for (Field field : cl.getDeclaredFields()){
if (field.getAnnotation(Transient.class) != null) continue;
if (Modifier.isTransient(field.getModifiers())) continue;
String propertyElement = "property";
if (field.getType().getAnnotation(Entity.class) != null){
propertyElement = "many-to-one";
}
String propertyElement = getPropertyElement(field.getType());
writer.begin(propertyElement);
writer.attribute("name", field.getName());
if (field.getType().getAnnotation(Embeddable.class) != null){
for (Field cfield : field.getType().getDeclaredFields()){
String cproperty = getPropertyElement(cfield.getType());
writer.begin(cproperty);
writer.attribute("name", cfield.getName());
writer.end(cproperty);
}
}
writer.end(propertyElement);
}
writer.end(classElement);
@ -111,6 +115,16 @@ public class HibernateDomainExporterTest {
w.close();
}
private String getPropertyElement(Class<?> cl){
if (cl.getAnnotation(Entity.class) != null){
return "many-to-one";
}else if (cl.getAnnotation(Embeddable.class) != null){
return "component";
}else{
return "property";
}
}
private static void assertContains(File file, String... strings) throws IOException{
assertTrue(file.getPath() + " doesn't exist", file.exists());
String result = FileUtils.readFileToString(file, "UTF-8");