mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-24 08:25:50 +00:00
#322 - RawSql fetch object with inheritance
This commit is contained in:
@@ -533,7 +533,7 @@ public class BeanProperty implements ElPropertyValue {
|
||||
sqlBeanLoad.load(this);
|
||||
}
|
||||
|
||||
public void buildSelectExpressionChain(String prefix, List<String> selectChain) {
|
||||
public void buildRawSqlSelectChain(String prefix, List<String> selectChain) {
|
||||
if (prefix == null) {
|
||||
selectChain.add(name);
|
||||
} else {
|
||||
|
||||
@@ -225,7 +225,8 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
|
||||
return createElPropertyValue(propName, remainder, chain, propertyDeploy);
|
||||
}
|
||||
|
||||
public void buildSelectExpressionChain(String prefix, List<String> selectChain) {
|
||||
@Override
|
||||
public void buildRawSqlSelectChain(String prefix, List<String> selectChain) {
|
||||
// do not add to the selectChain at the top level of the Many bean
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,5 @@
|
||||
package com.avaje.ebeaninternal.server.deploy;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebean.SqlUpdate;
|
||||
@@ -28,6 +20,13 @@ import com.avaje.ebeaninternal.server.query.SqlJoinType;
|
||||
import com.avaje.ebeaninternal.server.text.json.ReadJson;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Property mapped to a joined bean.
|
||||
*/
|
||||
@@ -273,17 +272,27 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> {
|
||||
return embeddedProps;
|
||||
}
|
||||
|
||||
public void buildSelectExpressionChain(String prefix, List<String> selectChain) {
|
||||
@Override
|
||||
public void buildRawSqlSelectChain(String prefix, List<String> selectChain) {
|
||||
|
||||
prefix = SplitName.add(prefix, name);
|
||||
|
||||
if (!embedded){
|
||||
targetIdBinder.buildSelectExpressionChain(prefix, selectChain);
|
||||
|
||||
InheritInfo inheritInfo = targetDescriptor.getInheritInfo();
|
||||
if (inheritInfo != null) {
|
||||
// expect the discriminator column to be included in order
|
||||
// to determine the inheritance type so we add it to the
|
||||
// selectChain (so that it takes a position in the resultSet)
|
||||
String discriminatorColumn = inheritInfo.getDiscriminatorColumn();
|
||||
String discProperty = prefix + "." + discriminatorColumn;
|
||||
selectChain.add(discProperty);
|
||||
}
|
||||
targetIdBinder.buildRawSqlSelectChain(prefix, selectChain);
|
||||
|
||||
} else {
|
||||
for (int i = 0; i < embeddedProps.length; i++) {
|
||||
embeddedProps[i].buildSelectExpressionChain(prefix, selectChain);
|
||||
}
|
||||
embeddedProps[i].buildRawSqlSelectChain(prefix, selectChain);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
package com.avaje.ebeaninternal.server.deploy.id;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
|
||||
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
|
||||
import com.avaje.ebeaninternal.server.core.DefaultSqlUpdate;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
|
||||
@@ -15,6 +8,12 @@ import com.avaje.ebeaninternal.server.deploy.DbReadContext;
|
||||
import com.avaje.ebeaninternal.server.deploy.DbSqlContext;
|
||||
import com.avaje.ebeaninternal.server.type.DataBind;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Binds id values to prepared statements.
|
||||
*/
|
||||
@@ -135,7 +134,7 @@ public interface IdBinder {
|
||||
*/
|
||||
public String getIdInValueExprDelete(int size);
|
||||
|
||||
public void buildSelectExpressionChain(String prefix, List<String> selectChain);
|
||||
public void buildRawSqlSelectChain(String prefix, List<String> selectChain);
|
||||
|
||||
/**
|
||||
* Read the id value from the result set and set it to the bean also returning
|
||||
|
||||
@@ -1,11 +1,5 @@
|
||||
package com.avaje.ebeaninternal.server.deploy.id;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
|
||||
import com.avaje.ebeaninternal.server.core.DefaultSqlUpdate;
|
||||
@@ -17,6 +11,12 @@ import com.avaje.ebeaninternal.server.deploy.DbSqlContext;
|
||||
import com.avaje.ebeaninternal.server.query.SplitName;
|
||||
import com.avaje.ebeaninternal.server.type.DataBind;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Bind an Id that is an Embedded bean.
|
||||
*/
|
||||
@@ -115,12 +115,12 @@ public final class IdBinderEmbedded implements IdBinder {
|
||||
return embIdProperty.getName();
|
||||
}
|
||||
|
||||
public void buildSelectExpressionChain(String prefix, List<String> selectChain) {
|
||||
public void buildRawSqlSelectChain(String prefix, List<String> selectChain) {
|
||||
|
||||
prefix = SplitName.add(prefix, embIdProperty.getName());
|
||||
|
||||
for (int i = 0; i < props.length; i++) {
|
||||
props[i].buildSelectExpressionChain(prefix, selectChain);
|
||||
props[i].buildRawSqlSelectChain(prefix, selectChain);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
package com.avaje.ebeaninternal.server.deploy.id;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
|
||||
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
|
||||
import com.avaje.ebeaninternal.server.core.DefaultSqlUpdate;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
|
||||
@@ -15,6 +8,12 @@ import com.avaje.ebeaninternal.server.deploy.DbReadContext;
|
||||
import com.avaje.ebeaninternal.server.deploy.DbSqlContext;
|
||||
import com.avaje.ebeaninternal.server.type.DataBind;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* For beans with no id properties AKA report type beans.
|
||||
*/
|
||||
@@ -38,7 +37,7 @@ public final class IdBinderEmpty implements IdBinder {
|
||||
return pathPrefix;
|
||||
}
|
||||
|
||||
public void buildSelectExpressionChain(String prefix, List<String> selectChain) {
|
||||
public void buildRawSqlSelectChain(String prefix, List<String> selectChain) {
|
||||
}
|
||||
|
||||
public int getPropertyCount() {
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
package com.avaje.ebeaninternal.server.deploy.id;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
|
||||
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
|
||||
import com.avaje.ebeaninternal.server.core.DefaultSqlUpdate;
|
||||
import com.avaje.ebeaninternal.server.core.InternString;
|
||||
@@ -17,6 +10,12 @@ import com.avaje.ebeaninternal.server.deploy.DbSqlContext;
|
||||
import com.avaje.ebeaninternal.server.type.DataBind;
|
||||
import com.avaje.ebeaninternal.server.type.ScalarType;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Bind an Id where the Id is made of a single property (not embedded).
|
||||
*/
|
||||
@@ -59,9 +58,9 @@ public final class IdBinderSimple implements IdBinder {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void buildSelectExpressionChain(String prefix, List<String> selectChain) {
|
||||
public void buildRawSqlSelectChain(String prefix, List<String> selectChain) {
|
||||
|
||||
idProperty.buildSelectExpressionChain(prefix, selectChain);
|
||||
idProperty.buildRawSqlSelectChain(prefix, selectChain);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -31,9 +31,11 @@ public class CQueryPlanRawSql extends CQueryPlan {
|
||||
|
||||
private int[] createIndexPositions(OrmQueryRequest<?> request, SqlTree sqlTree) {
|
||||
|
||||
List<String> chain = sqlTree.buildSelectExpressionChain();
|
||||
List<String> chain = sqlTree.buildRawSqlSelectChain();
|
||||
ColumnMapping columnMapping = request.getQuery().getRawSql().getColumnMapping();
|
||||
|
||||
// if the top level bean has inheritance expect first column
|
||||
// to be the discriminator type column (and use offset 1)
|
||||
InheritInfo inheritInfo = request.getBeanDescriptor().getInheritInfo();
|
||||
boolean addDiscriminator = inheritInfo != null;
|
||||
int offset = addDiscriminator ? 1 : 0;
|
||||
@@ -43,12 +45,25 @@ public class CQueryPlanRawSql extends CQueryPlan {
|
||||
// discriminator column must always be first in the query
|
||||
indexPositions[0] = 1;
|
||||
}
|
||||
|
||||
// set the resultSet index positions for the property expressions
|
||||
for (int i = 0; i < chain.size(); i++) {
|
||||
String expr = chain.get(i);
|
||||
int indexPos = 1 + columnMapping.getIndexPosition(expr);
|
||||
indexPositions[i + offset] = indexPos;
|
||||
}
|
||||
|
||||
// check and handle the case where a discriminator column for
|
||||
// an associated bean is in the raw SQL but is mapped columnIgnore
|
||||
for (int i = 0; i <indexPositions.length ; i++) {
|
||||
if (indexPositions[i] == 0) {
|
||||
if (i < indexPositions.length) {
|
||||
// expect discriminator column to immediately proceed id column
|
||||
indexPositions[i] = indexPositions[i+1] - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return indexPositions;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package com.avaje.ebeaninternal.server.query;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Represents the SELECT clause part of the SQL query.
|
||||
*/
|
||||
@@ -83,9 +83,9 @@ public class SqlTree {
|
||||
/**
|
||||
* Build a select expression chain for RawSql.
|
||||
*/
|
||||
public List<String> buildSelectExpressionChain() {
|
||||
public List<String> buildRawSqlSelectChain() {
|
||||
ArrayList<String> list = new ArrayList<String>();
|
||||
rootNode.buildSelectExpressionChain(list);
|
||||
rootNode.buildRawSqlSelectChain(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,20 @@
|
||||
package com.avaje.ebeaninternal.server.query;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebeaninternal.server.deploy.DbReadContext;
|
||||
import com.avaje.ebeaninternal.server.deploy.DbSqlContext;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
public interface SqlTreeNode {
|
||||
|
||||
String COMMA = ", ";
|
||||
|
||||
void buildSelectExpressionChain(List<String> selectChain);
|
||||
/**
|
||||
* Build the select chain for a RawSql query.
|
||||
*/
|
||||
void buildRawSqlSelectChain(List<String> selectChain);
|
||||
|
||||
/**
|
||||
* Append the required column information to the SELECT part of the sql
|
||||
|
||||
@@ -137,18 +137,18 @@ public class SqlTreeNodeBean implements SqlTreeNode {
|
||||
}
|
||||
}
|
||||
|
||||
public void buildSelectExpressionChain(List<String> selectChain) {
|
||||
public void buildRawSqlSelectChain(List<String> selectChain) {
|
||||
if (readId) {
|
||||
idBinder.buildSelectExpressionChain(prefix, selectChain);
|
||||
idBinder.buildRawSqlSelectChain(prefix, selectChain);
|
||||
}
|
||||
for (int i = 0, x = properties.length; i < x; i++) {
|
||||
properties[i].buildSelectExpressionChain(prefix, selectChain);
|
||||
properties[i].buildRawSqlSelectChain(prefix, selectChain);
|
||||
}
|
||||
// recursively continue reading...
|
||||
for (int i = 0; i < children.length; i++) {
|
||||
// read each child... and let them set their
|
||||
// values back to this localBean
|
||||
children[i].buildSelectExpressionChain(selectChain);
|
||||
children[i].buildRawSqlSelectChain(selectChain);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package com.avaje.ebeaninternal.server.query;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssoc;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
|
||||
@@ -11,6 +7,10 @@ import com.avaje.ebeaninternal.server.deploy.DbReadContext;
|
||||
import com.avaje.ebeaninternal.server.deploy.DbSqlContext;
|
||||
import com.avaje.ebeaninternal.server.deploy.TableJoin;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The purpose is to add an extra join to the query.
|
||||
* <p>
|
||||
@@ -35,7 +35,8 @@ public class SqlTreeNodeExtraJoin implements SqlTreeNode {
|
||||
this.manyJoin = assocBeanProperty instanceof BeanPropertyAssocMany<?>;
|
||||
}
|
||||
|
||||
public void buildSelectExpressionChain(List<String> selectChain) {
|
||||
@Override
|
||||
public void buildRawSqlSelectChain(List<String> selectChain) {
|
||||
// nothing to add
|
||||
}
|
||||
|
||||
@@ -49,7 +50,6 @@ public class SqlTreeNodeExtraJoin implements SqlTreeNode {
|
||||
return manyJoin;
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return prefix;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package com.avaje.ebeaninternal.server.query;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssoc;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany;
|
||||
@@ -12,6 +8,9 @@ import com.avaje.ebeaninternal.server.deploy.DbReadContext;
|
||||
import com.avaje.ebeaninternal.server.deploy.DbSqlContext;
|
||||
import com.avaje.ebeaninternal.server.deploy.TableJoin;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Join to Many (or child of a many) to support where clause predicates on many properties.
|
||||
*/
|
||||
@@ -76,7 +75,7 @@ public class SqlTreeNodeManyWhereJoin implements SqlTreeNode {
|
||||
}
|
||||
}
|
||||
|
||||
public void buildSelectExpressionChain(List<String> selectChain) {
|
||||
public void buildRawSqlSelectChain(List<String> selectChain) {
|
||||
// nothing to add
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ public class BaseTestCase {
|
||||
|
||||
static {
|
||||
logger.debug("... preStart");
|
||||
if (!AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent","debug=1;packages=com.avaje.tests.**")) {
|
||||
if (!AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent","debug=1;packages=com.avaje.tests.**,org.avaje.test.**")) {
|
||||
logger.info("avaje-ebeanorm-agent not found in classpath - not dynamically loaded");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.avaje.tests.rawsql.inherit;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
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.Parent;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class ParentQueryTest extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void QueryParentCollectionFetch() {
|
||||
|
||||
List<Data> exampleData = new ArrayList<Data>();
|
||||
exampleData.add(new Data(0));
|
||||
exampleData.add(new Data(1));
|
||||
exampleData.add(new Data(2));
|
||||
|
||||
ChildA a = new ChildA(0);
|
||||
a.setData(exampleData);
|
||||
Ebean.save(a);
|
||||
|
||||
ChildB b = new ChildB(1);
|
||||
b.setData(exampleData);
|
||||
Ebean.save(b);
|
||||
|
||||
ChildA c = new ChildA(2);
|
||||
c.setData(exampleData);
|
||||
Ebean.save(c);
|
||||
|
||||
List<Parent> partial = Ebean.find(Parent.class).where().ge("number", 1).findList();
|
||||
assertNotNull(partial.get(0).getData());
|
||||
assertEquals(partial.get(0).getData().get(0).getNumber().intValue(), 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package com.avaje.tests.rawsql.inherit;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.FetchConfig;
|
||||
import com.avaje.ebean.RawSql;
|
||||
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.Parent;
|
||||
import org.avaje.test.model.rawsql.inherit.ParentAggregate;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class ParentRawSqlTest extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void RawSqlParentLoad() {
|
||||
|
||||
List<Data> exampleData = new ArrayList<Data>();
|
||||
exampleData.add(new Data(0));
|
||||
exampleData.add(new Data(1));
|
||||
exampleData.add(new Data(2));
|
||||
|
||||
ChildA a = new ChildA(0);
|
||||
a.setData(exampleData);
|
||||
Ebean.save(a);
|
||||
|
||||
ChildB b = new ChildB(1);
|
||||
b.setData(exampleData);
|
||||
Ebean.save(b);
|
||||
|
||||
ChildA c = new ChildA(2);
|
||||
c.setData(exampleData);
|
||||
Ebean.save(c);
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.parse("select type, id, number from rawinherit_parent where number > 1")
|
||||
.create();
|
||||
|
||||
List<Parent> partial = Ebean.find(Parent.class)
|
||||
.setRawSql(rawSql)
|
||||
.findList();
|
||||
|
||||
assertNotNull(partial.get(0).getData());
|
||||
assertEquals(partial.get(0).getData().get(0).getNumber().intValue(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void RawSqlParentFetch() {
|
||||
|
||||
List<Data> exampleData = new ArrayList<Data>();
|
||||
exampleData.add(new Data(0));
|
||||
exampleData.add(new Data(1));
|
||||
exampleData.add(new Data(2));
|
||||
|
||||
ChildA a = new ChildA(0);
|
||||
a.setData(exampleData);
|
||||
Ebean.save(a);
|
||||
|
||||
ChildB b = new ChildB(1);
|
||||
b.setData(exampleData);
|
||||
Ebean.save(b);
|
||||
|
||||
ChildA c = new ChildA(2);
|
||||
c.setData(exampleData);
|
||||
Ebean.save(c);
|
||||
|
||||
useColumnMappingIgnore();
|
||||
|
||||
useColumnMappingWithDiscriminator();
|
||||
|
||||
useExtraColumnMappingIgnore();
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the discriminator column - columnMapping("type", "parent.type").
|
||||
*/
|
||||
private void useColumnMappingWithDiscriminator() {
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.unparsed("select type, id from rawinherit_parent where number > 1")
|
||||
.columnMapping("type", "parent.type") // can map the discriminator column 'type'
|
||||
.columnMapping("id", "parent.id")
|
||||
.create();
|
||||
|
||||
List<ParentAggregate> aggregates = Ebean.find(ParentAggregate.class).setRawSql(rawSql)
|
||||
.fetch("parent", new FetchConfig().query())
|
||||
.findList();
|
||||
|
||||
List<Parent> partial = new ArrayList<Parent>();
|
||||
for (ParentAggregate aggregate : aggregates) {
|
||||
partial.add(aggregate.parent);
|
||||
}
|
||||
|
||||
assertNotNull(partial.get(0).getData());
|
||||
assertEquals(partial.get(0).getData().get(0).getNumber().intValue(), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use columnMappingIgnore() to ignore the discriminator column.
|
||||
*/
|
||||
private void useColumnMappingIgnore() {
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.unparsed("select type, id from rawinherit_parent where number > 1")
|
||||
.columnMappingIgnore("type") // can ignore the discriminator 'type'
|
||||
.columnMapping("id", "parent.id")
|
||||
.create();
|
||||
|
||||
List<ParentAggregate> aggregates = Ebean.find(ParentAggregate.class).setRawSql(rawSql)
|
||||
.fetch("parent", new FetchConfig().query())
|
||||
.findList();
|
||||
|
||||
List<Parent> partial = new ArrayList<Parent>();
|
||||
for (ParentAggregate aggregate : aggregates) {
|
||||
partial.add(aggregate.parent);
|
||||
}
|
||||
|
||||
assertNotNull(partial.get(0).getData());
|
||||
assertEquals(partial.get(0).getData().get(0).getNumber().intValue(), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extra columnMappingIgnore() before and after.
|
||||
*/
|
||||
private void useExtraColumnMappingIgnore() {
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.unparsed("select 'a', type, id, 'b' from rawinherit_parent where number > 1")
|
||||
.columnMappingIgnore("a") // extra ignore before
|
||||
.columnMappingIgnore("type")
|
||||
.columnMapping("id", "parent.id")
|
||||
.columnMappingIgnore("b") // extra ignore after
|
||||
.create();
|
||||
|
||||
List<ParentAggregate> aggregates = Ebean.find(ParentAggregate.class).setRawSql(rawSql)
|
||||
.fetch("parent", new FetchConfig().query())
|
||||
.findList();
|
||||
|
||||
List<Parent> partial = new ArrayList<Parent>();
|
||||
for (ParentAggregate aggregate : aggregates) {
|
||||
partial.add(aggregate.parent);
|
||||
}
|
||||
assertNotNull(partial.get(0).getData());
|
||||
assertEquals(partial.get(0).getData().get(0).getNumber().intValue(), 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.avaje.test.model.rawsql.inherit;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@DiscriminatorValue("A")
|
||||
public class ChildA extends Parent {
|
||||
|
||||
public String getName() {
|
||||
return "A Name";
|
||||
}
|
||||
|
||||
public ChildA(Integer number) {
|
||||
super(number);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.avaje.test.model.rawsql.inherit;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@DiscriminatorValue("B")
|
||||
public class ChildB extends Parent {
|
||||
|
||||
public String getName() {
|
||||
return "B Name";
|
||||
}
|
||||
|
||||
public ChildB(Integer number) {
|
||||
super(number);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.avaje.test.model.rawsql.inherit;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.Table;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "rawinherit_data")
|
||||
public class Data {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private Integer number;
|
||||
|
||||
@ManyToMany(mappedBy = "data")
|
||||
public List<Parent> parents = new ArrayList<Parent>();
|
||||
|
||||
public Data(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Integer getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.avaje.test.model.rawsql.inherit;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.DiscriminatorColumn;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.Table;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "rawinherit_parent")
|
||||
@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
|
||||
@DiscriminatorColumn(name="type")
|
||||
public abstract class Parent {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private Integer number;
|
||||
|
||||
@ManyToMany(cascade = CascadeType.PERSIST)
|
||||
private List<Data> data = new ArrayList<Data>();
|
||||
|
||||
protected Parent(Integer number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public abstract String getName();
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Integer getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public List<Data> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(List<Data> datas) {
|
||||
this.data = datas;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.avaje.test.model.rawsql.inherit;
|
||||
|
||||
import com.avaje.ebean.annotation.Sql;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
@Entity
|
||||
@Sql
|
||||
public class ParentAggregate {
|
||||
|
||||
@OneToOne
|
||||
public Parent parent;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user