diff --git a/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/codegen/HibernateDomainExporter.java b/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/codegen/HibernateDomainExporter.java index 728a14847..674877eed 100644 --- a/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/codegen/HibernateDomainExporter.java +++ b/querydsl-jpa-codegen/src/main/java/com/mysema/query/jpa/codegen/HibernateDomainExporter.java @@ -285,7 +285,7 @@ public class HibernateDomainExporter { handleProperty(entityType, msc.getMappedClass(), (org.hibernate.mapping.Property) properties.next()); } } - + // entity classes Iterator classMappings = configuration.getClassMappings(); while (classMappings.hasNext()) { @@ -328,13 +328,21 @@ public class HibernateDomainExporter { } else if (propertyType.getCategory() == TypeCategory.ENTITY) { propertyType = createEntityType(Class.forName(propertyType.getFullName())); } else if (p.getValue() instanceof org.hibernate.mapping.Collection) { - org.hibernate.mapping.Collection collection = (org.hibernate.mapping.Collection)p.getValue(); + org.hibernate.mapping.Collection collection = (org.hibernate.mapping.Collection)p.getValue(); if (collection.getElement() instanceof OneToMany) { String entityName = ((OneToMany)collection.getElement()).getReferencedEntityName(); if (entityName != null) { Type componentType = typeFactory.create(Class.forName(entityName)); propertyType = new SimpleType(propertyType, componentType); } + } else if (collection.getElement() instanceof Component) { + Component component = (Component)collection.getElement(); + Class embeddedClass = Class.forName(component.getComponentClassName()); + EntityType embeddedType = createEmbeddableType(embeddedClass); + Iterator properties = component.getPropertyIterator(); + while (properties.hasNext()) { + handleProperty(embeddedType, embeddedClass, (org.hibernate.mapping.Property)properties.next()); + } } } diff --git a/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/domain5/DomainExporterTest.java b/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/domain5/DomainExporterTest.java index 9d47e58a9..709c56799 100644 --- a/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/domain5/DomainExporterTest.java +++ b/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/domain5/DomainExporterTest.java @@ -17,7 +17,7 @@ import com.mysema.util.FileUtils; public class DomainExporterTest { @Test - public void Execute_MyEntity() throws IOException { + public void Execute() throws IOException { File gen = new File("target/" + getClass().getSimpleName()); FileUtils.delete(gen); Configuration config = new Configuration(); diff --git a/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/domain6/Contact.java b/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/domain6/Contact.java new file mode 100644 index 000000000..bf9b839e2 --- /dev/null +++ b/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/domain6/Contact.java @@ -0,0 +1,34 @@ +package com.mysema.query.jpa.domain6; + +import java.util.List; + +public class Contact { + + private long id; + private String name; + private List phoneNumbers; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getPhoneNumbers() { + return phoneNumbers; + } + + public void setPhoneNumbers(List phoneNumbers) { + this.phoneNumbers = phoneNumbers; + } +} \ No newline at end of file diff --git a/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/domain6/DomainExporterTest.java b/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/domain6/DomainExporterTest.java new file mode 100644 index 000000000..2c1a5e76f --- /dev/null +++ b/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/domain6/DomainExporterTest.java @@ -0,0 +1,43 @@ +package com.mysema.query.jpa.domain6; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; + +import org.hibernate.cfg.Configuration; +import org.junit.Test; + +import com.google.common.base.Charsets; +import com.google.common.io.Files; +import com.mysema.query.jpa.codegen.HibernateDomainExporter; +import com.mysema.util.FileUtils; + +public class DomainExporterTest { + + @Test + public void Execute() throws IOException { + File gen = new File("target/" + getClass().getSimpleName()); + FileUtils.delete(gen); + Configuration config = new Configuration(); + config.addFile(new File("src/test/resources/com/mysema/query/jpa/domain6/Contact.hbm.xml")); + HibernateDomainExporter exporter = new HibernateDomainExporter("Q", gen, config); + exporter.execute(); + + File targetFile = new File(gen, "com/mysema/query/jpa/domain6/QContact.java"); + assertContains(targetFile, "ListPath phoneNumbers"); + + targetFile = new File(gen, "com/mysema/query/jpa/domain6/QPhoneNumber.java"); + assertContains(targetFile, "QPhoneNumber extends BeanPath", + "StringPath number = createString(\"number\")"); + } + + private static void assertContains(File file, String... strings) throws IOException { + assertTrue(file.getPath() + " doesn't exist", file.exists()); + String result = Files.toString(file, Charsets.UTF_8); + for (String str : strings) { + assertTrue(str + " was not contained", result.contains(str)); + } + } + +} diff --git a/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/domain6/PhoneNumber.java b/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/domain6/PhoneNumber.java new file mode 100644 index 000000000..f0cb7fae9 --- /dev/null +++ b/querydsl-jpa-codegen/src/test/java/com/mysema/query/jpa/domain6/PhoneNumber.java @@ -0,0 +1,23 @@ +package com.mysema.query.jpa.domain6; + +public class PhoneNumber { + + private String type; + private String number; + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } +} \ No newline at end of file diff --git a/querydsl-jpa-codegen/src/test/resources/com/mysema/query/jpa/domain6/Contact.hbm.xml b/querydsl-jpa-codegen/src/test/resources/com/mysema/query/jpa/domain6/Contact.hbm.xml new file mode 100644 index 000000000..c54f660d4 --- /dev/null +++ b/querydsl-jpa-codegen/src/test/resources/com/mysema/query/jpa/domain6/Contact.hbm.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file