diff --git a/src/main/java/com/avaje/ebean/config/MatchingNamingConvention.java b/src/main/java/com/avaje/ebean/config/MatchingNamingConvention.java index bf871e4b4..7b622c83f 100644 --- a/src/main/java/com/avaje/ebean/config/MatchingNamingConvention.java +++ b/src/main/java/com/avaje/ebean/config/MatchingNamingConvention.java @@ -41,4 +41,11 @@ public class MatchingNamingConvention extends AbstractNamingConvention { public String getPropertyFromColumn(Class beanClass, String dbColumnName) { return dbColumnName; } + + @Override + public String getForeignKey(String prefix, String fkProperty) { + // add fkProperty as init caps + return prefix + fkProperty.substring(0, 1).toUpperCase() + fkProperty.substring(1); + } + } diff --git a/src/main/java/com/avaje/ebean/config/NamingConvention.java b/src/main/java/com/avaje/ebean/config/NamingConvention.java index d86733112..36363cd08 100644 --- a/src/main/java/com/avaje/ebean/config/NamingConvention.java +++ b/src/main/java/com/avaje/ebean/config/NamingConvention.java @@ -100,6 +100,15 @@ public interface NamingConvention { */ boolean isUseForeignKeyPrefix(); + /** + * Return the foreign key column given the local and foreign properties. + * + * @param prefix the local column used to prefix the fk column + * @param fkProperty the property name of the foreign key + * @return the foreign key column + */ + String getForeignKey(String prefix, String fkProperty); + /** * Load setting from properties. */ diff --git a/src/main/java/com/avaje/ebean/config/UnderscoreNamingConvention.java b/src/main/java/com/avaje/ebean/config/UnderscoreNamingConvention.java index b44c6f140..ee6e52829 100644 --- a/src/main/java/com/avaje/ebean/config/UnderscoreNamingConvention.java +++ b/src/main/java/com/avaje/ebean/config/UnderscoreNamingConvention.java @@ -100,6 +100,11 @@ public class UnderscoreNamingConvention extends AbstractNamingConvention { this.digitsCompressed = digitsCompressed; } + @Override + public String getForeignKey(String prefix, String fkProperty) { + return prefix + "_" + toUnderscoreFromCamel(fkProperty); + } + /** * Convert and return the string to underscore from camel case. */ diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptorMap.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptorMap.java index 6fee345f9..3a4d5ddcd 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptorMap.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptorMap.java @@ -1,6 +1,7 @@ package com.avaje.ebeaninternal.server.deploy; import com.avaje.ebean.config.EncryptKey; +import com.avaje.ebean.config.NamingConvention; import com.avaje.ebean.config.ServerConfig; import com.avaje.ebeaninternal.server.cache.SpiCacheManager; import com.avaje.ebeaninternal.server.deploy.id.IdBinder; @@ -30,6 +31,11 @@ public interface BeanDescriptorMap { */ SpiCacheManager getCacheManager(); + /** + * Return the naming convention. + */ + NamingConvention getNamingConvention(); + /** * Return the BeanDescriptor for a given class. */ diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanTable.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanTable.java index b19123965..654bac64e 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanTable.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanTable.java @@ -21,6 +21,8 @@ public class BeanTable { private static final Logger logger = LoggerFactory.getLogger(BeanTable.class); + private final BeanDescriptorMap owner; + private final Class beanType; /** @@ -34,6 +36,7 @@ public class BeanTable { * Create the BeanTable. */ public BeanTable(DeployBeanTable mutable, BeanDescriptorMap owner) { + this.owner = owner; this.beanType = mutable.getBeanType(); this.baseTable = InternString.intern(mutable.getBaseTable()); this.idProperties = mutable.createIdProperties(owner); @@ -94,14 +97,13 @@ public class BeanTable { String lc = prop.getDbColumn(); String fk = lc; if (foreignKeyPrefix != null) { - fk = foreignKeyPrefix + "_" + fk; + fk = owner.getNamingConvention().getForeignKey(foreignKeyPrefix, fk); } if (complexKey) { // just to copy the column name rather than prefix with the foreignKeyPrefix. // I think that with complex keys this is the more common approach. - String msg = "On table[" + baseTable + "] foreign key column [" + lc + "]"; - logger.debug(msg); + logger.debug("On table[{}] foreign key column [{}]", baseTable, lc); fk = lc; } if (sqlFormulaSelect != null) { diff --git a/src/test/java/com/avaje/ebean/config/MatchingNamingConventionTest.java b/src/test/java/com/avaje/ebean/config/MatchingNamingConventionTest.java index f9aa5649f..47ee2c814 100644 --- a/src/test/java/com/avaje/ebean/config/MatchingNamingConventionTest.java +++ b/src/test/java/com/avaje/ebean/config/MatchingNamingConventionTest.java @@ -15,4 +15,14 @@ public class MatchingNamingConventionTest { String col = namingConvention.getColumnFromProperty(null, fkCol); assertThat(col).isEqualTo(fkCol); } -} \ No newline at end of file + + @Test + public void getForeignKey() { + + String fk = namingConvention.getForeignKey("billingAddress", "id"); + assertThat(fk).isEqualTo("billingAddressId"); + + fk = namingConvention.getForeignKey("billingAddress", "remoteIdProperty"); + assertThat(fk).isEqualTo("billingAddressRemoteIdProperty"); + } +} diff --git a/src/test/java/com/avaje/ebean/config/UnderscoreNamingConventionTest.java b/src/test/java/com/avaje/ebean/config/UnderscoreNamingConventionTest.java index 209b702bc..add4ed97a 100644 --- a/src/test/java/com/avaje/ebean/config/UnderscoreNamingConventionTest.java +++ b/src/test/java/com/avaje/ebean/config/UnderscoreNamingConventionTest.java @@ -17,4 +17,13 @@ public class UnderscoreNamingConventionTest { assertThat(col).isEqualTo(fkCol); } -} \ No newline at end of file + @Test + public void getForeignKey() { + + String fk = namingConvention.getForeignKey("billing_address", "id"); + assertThat(fk).isEqualTo("billing_address_id"); + + fk = namingConvention.getForeignKey("billing_address", "remoteIdProperty"); + assertThat(fk).isEqualTo("billing_address_remote_id_property"); + } +}