What does “org.hibernate.DuplicateMappingException” error mean?

后端 未结 1 1401
太阳男子
太阳男子 2021-02-19 13:26

I\'m trying to force JPA/Hibernate to generate and use only lowercase tablenames. I\'ve implemented a NamingStrategy like this:

public class MyNamingStrategy ext         


        
相关标签:
1条回答
  • 2021-02-19 13:50

    Bogdan, thanks for posting this. I had a similar problem with case-sensitive table names for Unix/Windows portability using Hibernate/JPA/MySQL on Linux.

    Like you, I set out to bind my table names as all lower-case by configuring a custom NamingStrategy in my META-INF/persistence.xml file:

    <property name="hibernate.ejb.naming_strategy" value="my.package.LowerCaseNamingStrategy" />
    

    I got the same exception: org.hibernate.DuplicateMappingException: Same physical table name... Through using the debugger, I had an epiphany that maybe I wasn't using DefaultNamingStrategy to begin with! So I changed my base class to org.hibernate.cfg.EJB3NamingStrategy. This is more appropriate when using JPA Annotations, I believe! Here was my final NamingStrategy:

    package my.package;
    
    import org.apache.commons.lang.StringUtils;
    import org.hibernate.cfg.EJB3NamingStrategy;
    
    public class LowerCaseNamingStrategy extends EJB3NamingStrategy {
        @Override
        public String classToTableName(String className) {
            return StringUtils.lowerCase(super.classToTableName(className));
        }
    
        @Override
        public String collectionTableName(String ownerEntity, String ownerEntityTable, String associatedEntity,
                String associatedEntityTable, String propertyName) {
            return StringUtils.lowerCase(super.collectionTableName(ownerEntity, ownerEntityTable, associatedEntity, associatedEntityTable, propertyName));
        }
    
        @Override
        public String logicalCollectionTableName(String tableName, String ownerEntityTable, String associatedEntityTable,
                String propertyName) {
            return StringUtils.lowerCase(super.logicalCollectionTableName(tableName, ownerEntityTable, associatedEntityTable, propertyName));
        }
    
        @Override
        public String tableName(String tableName) {
            return StringUtils.lowerCase(super.tableName(tableName));
        }
    }
    

    PS the previous solution by dursun did not work for me.

    0 讨论(0)
提交回复
热议问题