Add static getTableName(Class) and instance.tableName() methods.

This commit is contained in:
2025-03-18 10:48:15 -05:00
parent 38cdb94876
commit ae4e269b88
2 changed files with 46 additions and 0 deletions

View File

@ -583,4 +583,31 @@ public abstract class QRecordEntity
return (null); return (null);
} }
/***************************************************************************
**
***************************************************************************/
public static String getTableName(Class<? extends QRecordEntity> entityClass) throws QException
{
try
{
Field tableNameField = entityClass.getDeclaredField("TABLE_NAME");
String tableNameValue = (String) tableNameField.get(null);
return (tableNameValue);
}
catch(Exception e)
{
throw (new QException("Could not get TABLE_NAME from entity class: " + entityClass.getSimpleName(), e));
}
}
/***************************************************************************
** named without the 'get' to avoid conflict w/ entity fields named that...
***************************************************************************/
public String tableName() throws QException
{
return (getTableName(this.getClass()));
}
} }

View File

@ -41,6 +41,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -566,4 +567,22 @@ class QRecordEntityTest extends BaseTest
assertEquals(0, order.getLineItems().size()); assertEquals(0, order.getLineItems().size());
} }
/*******************************************************************************
**
*******************************************************************************/
@Test
void testTableName() throws QException
{
assertEquals(Item.TABLE_NAME, QRecordEntity.getTableName(Item.class));
assertEquals(Item.TABLE_NAME, Item.getTableName(Item.class));
assertEquals(Item.TABLE_NAME, new Item().tableName());
//////////////////////////////////
// no TABLE_NAME in Order class //
//////////////////////////////////
assertThatThrownBy(() -> Order.getTableName(Order.class));
}
} }