HibernateDomainExporter and inheritance with composite-id.

This commit is contained in:
Vasco Veloso 2015-08-31 09:31:21 +01:00
parent 7f6e64402d
commit 3f85f12fa7
7 changed files with 111 additions and 3 deletions

View File

@ -64,18 +64,26 @@ public class EntitySerializer implements Serializer {
this.keywords = keywords;
}
private boolean superTypeHasEntityFields(EntityType model) {
Supertype superType = model.getSuperType();
return null != superType && null != superType.getEntityType()
&& superType.getEntityType().hasEntityFields();
}
protected void constructors(EntityType model, SerializerConfig config,
CodeWriter writer) throws IOException {
String localName = writer.getRawName(model);
String genericName = writer.getGenericName(true, model);
boolean hasEntityFields = model.hasEntityFields();
boolean hasEntityFields = model.hasEntityFields() || superTypeHasEntityFields(model);
boolean stringOrBoolean = model.getOriginalCategory() == TypeCategory.STRING
|| model.getOriginalCategory() == TypeCategory.BOOLEAN;
String thisOrSuper = hasEntityFields ? THIS : SUPER;
String additionalParams = getAdditionalConstructorParameter(model);
String classCast = localName.equals(genericName) ? EMPTY : "(Class) ";
// String
constructorsForVariables(writer, model);
@ -91,6 +99,7 @@ public class EntitySerializer implements Serializer {
Type type = new ClassType(Path.class, new TypeExtends(simpleModel));
writer.beginConstructor(new Parameter("path", type));
}
if (!hasEntityFields) {
if (stringOrBoolean) {
writer.line("super(path.getMetadata());");
@ -161,7 +170,7 @@ public class EntitySerializer implements Serializer {
boolean stringOrBoolean = model.getOriginalCategory() == TypeCategory.STRING
|| model.getOriginalCategory() == TypeCategory.BOOLEAN;
boolean hasEntityFields = model.hasEntityFields();
boolean hasEntityFields = model.hasEntityFields() || superTypeHasEntityFields(model);
String thisOrSuper = hasEntityFields ? THIS : SUPER;
String additionalParams = hasEntityFields ? "" : getAdditionalConstructorParameter(model);
@ -495,7 +504,7 @@ public class EntitySerializer implements Serializer {
inits.add(0, STAR);
String initsAsString = QUOTE + JOINER.join(inits) + QUOTE;
writer.privateStaticFinal(PATH_INITS_TYPE, "INITS", "new PathInits(" + initsAsString + ")");
} else if (model.hasEntityFields()) {
} else if (model.hasEntityFields() || superTypeHasEntityFields(model)) {
writer.privateStaticFinal(PATH_INITS_TYPE, "INITS", "PathInits.DIRECT2");
}
}

View File

@ -189,6 +189,22 @@ public class HibernateDomainExporterTest {
CompileUtils.compile("target/gen5");
}
@Test
public void Execute_CompositeKey() throws IOException {
// See https://github.com/querydsl/querydsl/issues/1459
FileUtils.delete(new File("target/gen18"));
Configuration config = new Configuration();
config.addFile(new File("src/test/resources/route1.hbm.xml"));
config.addFile(new File("src/test/resources/route2.hbm.xml"));
HibernateDomainExporter exporter = new HibernateDomainExporter("Q",
new File("target/gen18"), serializerConfig, config);
exporter.execute();
CompileUtils.compile("target/gen18");
}
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);

View File

@ -0,0 +1,28 @@
package com.querydsl.jpa.domain18;
public class Route1 {
private RouteDbKey routeKey;
private int version;
private int cost;
public RouteDbKey getRouteKey() {
return routeKey;
}
public void setRouteKey(RouteDbKey routeKey) {
this.routeKey = routeKey;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
}

View File

@ -0,0 +1,5 @@
package com.querydsl.jpa.domain18;
public class Route2 extends Route1 {
}

View File

@ -0,0 +1,21 @@
package com.querydsl.jpa.domain18;
public class RouteDbKey {
private String type;
private String key;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}

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 default-access="field" default-lazy="false">
<class abstract="true"
name="com.querydsl.jpa.domain18.Route1" table="ROUTES">
<composite-id access="field" name="routeKey" class="com.querydsl.jpa.domain18.RouteDbKey">
<key-property name="type" type="string" column="ROUTE_TYPE"/>
<key-property name="key" type="string" column="ROUTE_KEY"/>
</composite-id>
<discriminator force="false" insert="false" not-null="true">
<column name="ROUTE_TYPE"/>
</discriminator>
<version access="field" column="VERSION" generated="never"
name="version" type="int"/>
<property access="field" generated="never" lazy="false" name="cost" type="int">
<column name="COST"/>
</property>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,9 @@
<?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>
<subclass discriminator-value="GATEWAY"
extends="com.querydsl.jpa.domain18.Route1" lazy="false"
name="com.querydsl.jpa.domain18.Route2" select-before-update="false">
</subclass>
</hibernate-mapping>