Extend test for #322 - using inheritance discriminator with RawSql

This commit is contained in:
Robin Bygrave
2015-09-03 15:57:38 +12:00
parent 572bb41b73
commit 7e66e0db1f
2 changed files with 100 additions and 0 deletions
@@ -8,6 +8,7 @@ import com.avaje.ebean.RawSqlBuilder;
import org.avaje.test.model.rawsql.inherit.ChildA;
import org.avaje.test.model.rawsql.inherit.ChildB;
import org.avaje.test.model.rawsql.inherit.Data;
import org.avaje.test.model.rawsql.inherit.EUncle;
import org.avaje.test.model.rawsql.inherit.Parent;
import org.avaje.test.model.rawsql.inherit.ParentAggregate;
import org.junit.Test;
@@ -17,6 +18,7 @@ import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class ParentRawSqlTest extends BaseTestCase {
@@ -72,6 +74,14 @@ public class ParentRawSqlTest extends BaseTestCase {
c.setData(exampleData);
Ebean.save(c);
EUncle e1 = new EUncle();
e1.setParent(b);
e1.setName("fester");
Ebean.save(e1);
joinToInheritanceHierarchy_withAliasMapping();
joinToInheritanceHierarchy();
useColumnMappingIgnore();
useColumnMappingWithDiscriminator();
@@ -79,6 +89,41 @@ public class ParentRawSqlTest extends BaseTestCase {
useExtraColumnMappingIgnore();
}
private void joinToInheritanceHierarchy() {
RawSql rawSql = RawSqlBuilder
.unparsed("select u.id, u.name, p.type as ptype, p.id as pid from rawinherit_uncle u join rawinherit_parent p where p.id = u.parent_id")
.columnMapping("id", "id")
.columnMapping("name", "name")
.columnMapping("ptype", "parent.type")
.columnMapping("pid", "parent.id")
.create();
List<EUncle> uncles = Ebean.find(EUncle.class).setRawSql(rawSql)
.fetch("parent", new FetchConfig().query())
.findList();
assertNotNull(uncles.get(0));
Parent parent = uncles.get(0).getParent();
assertTrue(parent instanceof ChildB);
}
private void joinToInheritanceHierarchy_withAliasMapping() {
RawSql rawSql = RawSqlBuilder
.parse("select u.id, u.name, p.type, p.id from rawinherit_uncle u join rawinherit_parent p where p.id = u.parent_id")
.tableAliasMapping("p", "parent")
.create();
List<EUncle> uncles = Ebean.find(EUncle.class).setRawSql(rawSql)
.findList();
assertNotNull(uncles.get(0));
Parent parent = uncles.get(0).getParent();
assertTrue(parent instanceof ChildB);
}
/**
* Map the discriminator column - columnMapping("type", "parent.type").
*/
@@ -0,0 +1,55 @@
package org.avaje.test.model.rawsql.inherit;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Version;
@Entity
@Table(name = "rawinherit_uncle")
public class EUncle {
@Id
private Integer id;
private String name;
@ManyToOne(optional = false)
private Parent parent;
@Version
private Long version;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}