1

在我的 Java 应用程序中,我正在使用枚举集合,如下所示:

@ElementCollection
@Enumerated(EnumType.ORDINAL)
protected Set<Tag> tags = new TreeSet<>();

然而,这个定义是给出的@MappedSuperClass,所以我不能定义name@JoinTable因为名称会在子类中发生冲突。我的问题是默认的休眠命名策略被忽略了。例如,对于继承的类Event而不是带有 name 的表event_tags,hibernate 试图使用它Event_tags而不是 fieldevent_id它试图使用Event_id。在我看来,Hibernate 完全忽略了命名策略,只使用实体名称而不做任何更改。

如何强制它使用默认命名策略?

4

1 回答 1

0

似乎默认命名策略无法处理这些,您需要自己实现。例如:

public class NamingPolicy implements NamingStrategy, Serializable {

    @Override
    public String classToTableName(String className) {
        return StringHelper.unqualify(className).toLowerCase();
    }



    @Override
    public String propertyToColumnName(String propertyName) {
        return StringHelper.unqualify(propertyName);
    }



    public String singularize(String propertyName) {
        if (propertyName != null && propertyName.endsWith("s")) {
            propertyName = propertyName.substring(0, propertyName.length() - 1);
        }

        return propertyName;
    }



    @Override
    public String tableName(String tableName) {
        return tableName;
    }



    @Override
    public String columnName(String columnName) {
        return columnName;
    }



    @Override
    public String collectionTableName(
            String ownerEntity, String ownerEntityTable, String associatedEntity,
            String associatedEntityTable, String propertyName) {

        return classToTableName(ownerEntityTable) + "_" + 
                or(associatedEntityTable, singularize(propertyName));
    }



    @Override
    public String joinKeyColumnName(String joinedColumn, String joinedTable) {
        return columnName(joinedColumn);
    }



    @Override
    public String foreignKeyColumnName(
            String propertyName, String propertyEntityName,
            String propertyTableName, String referencedColumnName) {

        String header = propertyName != null ? propertyName : propertyTableName;
        if (header == null) {
            throw new AssertionFailure("NamingStrategy not properly filled");
        }

        return classToTableName(header) + "_" + referencedColumnName;
    }



    @Override
    public String logicalColumnName(String columnName, String propertyName) {
        return StringHelper.isNotEmpty(columnName)
                ? columnName : StringHelper.unqualify(propertyName);
    }



    @Override
    public String logicalCollectionTableName(
            String tableName, String ownerEntityTable, String associatedEntityTable, String propertyName) {


        if (tableName != null) {
            return tableName;
        } else {
            return tableName(ownerEntityTable) + "_" + (associatedEntityTable != null
                    ? associatedEntityTable
                    : singularize(propertyName));
        }
    }



    @Override
    public String logicalCollectionColumnName(
            String columnName, String propertyName, String referencedColumn) {
        return StringHelper.isNotEmpty(columnName)
                ? columnName
                : classToTableName(propertyName) + "_" + singularize(referencedColumn);
    }
}
于 2013-11-10T08:24:11.413 回答