#2177 - findNative not mapping bean when schema specified in entity mapping

This commit is contained in:
Robin Bygrave
2021-03-01 22:08:17 +13:00
parent 464f48d717
commit 6315edfc21
6 changed files with 96 additions and 6 deletions
@@ -727,7 +727,7 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
String[] cols = indexDef.getColumns();
BeanProperty[] props = new BeanProperty[cols.length];
for (int i = 0; i < cols.length; i++) {
String propName = findBeanPath("", cols[i]);
String propName = findBeanPath("", "", cols[i]);
if (propName == null) {
return;
}
@@ -2472,13 +2472,16 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
/**
* Return the property path given the db table and column.
*/
public String findBeanPath(String tableName, String columnName) {
if (tableName.isEmpty() || tableName.equalsIgnoreCase(baseTable)) {
public String findBeanPath(String schemaName, String tableName, String columnName) {
if (matchBaseTable(schemaName, tableName)) {
return columnPath.get(columnName);
}
BeanPropertyAssoc<?> assocProperty = tablePath.get(tableName);
if (assocProperty == null) {
assocProperty = tablePath.get(schemaName + "." + tableName);
}
if (assocProperty != null) {
String relativePath = assocProperty.getTargetDescriptor().findBeanPath(tableName, columnName);
String relativePath = assocProperty.getTargetDescriptor().findBeanPath(schemaName, tableName, columnName);
if (relativePath != null) {
return SplitName.add(assocProperty.getName(), relativePath);
}
@@ -2486,6 +2489,12 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
return null;
}
private boolean matchBaseTable(String schemaName, String tableName) {
return tableName.isEmpty()
|| baseTable.equalsIgnoreCase(tableName)
|| baseTable.equalsIgnoreCase(schemaName + "." + tableName);
}
/**
* Return a 'dynamic property' used to read a formula.
*/
@@ -442,9 +442,10 @@ class CQueryBuilder {
int cols = 1 + metaData.getColumnCount();
List<String> propertyNames = new ArrayList<>(cols - 1);
for (int i = 1; i < cols; i++) {
String schemaName = metaData.getSchemaName(i).toLowerCase();
String tableName = metaData.getTableName(i).toLowerCase();
String columnName = metaData.getColumnName(i).toLowerCase();
String path = desc.findBeanPath(tableName, columnName);
String path = desc.findBeanPath(schemaName, tableName, columnName);
if (path != null) {
propertyNames.add(path);
} else {
@@ -8,7 +8,7 @@ import javax.validation.constraints.Size;
* Product entity bean.
*/
@Entity
@Table(name = "o_product")
@Table(name = "o_product", schema = "foo")
public class Product extends BaseModel {
@Size(max = 20)
@@ -0,0 +1,78 @@
package org.querytest;
import io.ebean.DB;
import org.example.domain.Customer;
import org.example.domain.Order;
import org.example.domain.OrderDetail;
import org.example.domain.Product;
import org.junit.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class FindNative_withSchema {
@Test
public void test_rootLevelUsesSchema() {
Product p = new Product();
p.setName("prod1");
p.setSku("p1");
p.save();
String sql = "select * from foo.o_product p where p.sku = ?";
Product product = DB.findNative(Product.class, sql)
.setParameter("p1")
.findOne();
assertThat(product).isNotNull();
assertThat(product.getName()).isEqualTo("prod1");
}
@Test
public void test_joinToSchema() {
Order order = setupData();
String sql = "select l.*, p.* " +
" from o_order_detail l " +
" join foo.o_product p on p.id = l.product_id " +
" where l.order_id = ?";
List<OrderDetail> lines = DB.findNative(OrderDetail.class, sql)
.setParameter(order.getId())
.findList();
assertThat(lines).hasSize(1);
OrderDetail line = lines.get(0);
Product product = line.getProduct();
// check normal bean population
assertThat(line.getOrderQty()).isEqualTo(10);
assertThat(line.getOrder().getId()).isEqualTo(order.getId());
// check associated bean with schema is populated
assertThat(product).isNotNull();
assertThat(product.getName()).isEqualTo("prod2");
}
private Order setupData() {
Product p = new Product();
p.setName("prod2");
p.setSku("p2");
p.save();
Customer customer = new Customer();
customer.setName("junk");
customer.save();
Order order = new Order();
order.setCustomer(customer);
OrderDetail line = new OrderDetail(p, 10, 10.0);
order.getDetails().add(line);
order.save();
return order;
}
}
@@ -1,5 +1,6 @@
ebean.ddl.generate=true
ebean.ddl.run=true
ebean.ddl.initSql=init-db.sql
datasource.default=h2
@@ -0,0 +1 @@
create schema foo;