#219 improved composite handling

This commit is contained in:
Timo Westkämper 2012-08-22 19:44:50 +03:00
parent 7eb5a19341
commit 51549b85b4
6 changed files with 131 additions and 3 deletions

View File

@ -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());
}
}
}

View File

@ -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();

View File

@ -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<PhoneNumber> 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<PhoneNumber> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
}

View File

@ -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<PhoneNumber, QPhoneNumber> phoneNumbers");
targetFile = new File(gen, "com/mysema/query/jpa/domain6/QPhoneNumber.java");
assertContains(targetFile, "QPhoneNumber extends BeanPath<PhoneNumber>",
"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));
}
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,20 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.mysema.query.jpa.domain6" default-lazy="false">
<class name="Contact" table="CONTACT" mutable="false">
<id name="id" column="ID">
<generator class="increment" />
</id>
<property name="name" column="name" not-null="true" />
<list name="phoneNumbers" table="CONTACT_PHONE_NUMBER" lazy="false">
<key column="CONTACT_ID"/>
<list-index column="INDEX" base="0"/>
<composite-element class="PhoneNumber">
<property name="type"/>
<property name="number" />
</composite-element>
</list>
</class>
</hibernate-mapping>