“org.hibernate.DuplicateMappingException”错误是什么意思?

2022-09-04 23:30:46

我试图强制JPA / Hibernate生成并使用小写的表名。我已经实现了一个命名策略,如下所示:

public class MyNamingStrategy extends DefaultNamingStrategy {

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

我通过在持久性中设置此属性来应用它.xml:

<property name="hibernate.ejb.naming_strategy" value="entities.strategy.MyNamingStrategy"/>

当我这样做时,我得到这个堆栈跟踪:

SEVERE: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method
org.hibernate.DuplicateMappingException: Same physical table name [planning] references several logical table names: [Planning], [OrderProductMan_Planning]
        at org.hibernate.cfg.Configuration$MappingsImpl.addTableBinding(Configuration.java:2629)
        at org.hibernate.cfg.annotations.TableBinder.buildAndFillTable(TableBinder.java:254)
        at org.hibernate.cfg.annotations.TableBinder.bind(TableBinder.java:177)

什么是

相同的物理表名称 [计划] 引用多个逻辑表名称:[计划]、[OrderProductMan_Planning]

意味 着?

实体从错误,尽可能简化。如果您需要其余的,请告诉我。

@Entity
public class Planning implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;

  private Integer qty;

  @ManyToOne
  private OrderProductMan orderProduct;

  ....
}


@Entity
@Table
public class OrderProductMan implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  private Integer opId;

  @Basic(optional = false)
  private int qty;

  @ManyToOne(optional = false)
  private ProductMan produse;

  @ManyToOne(optional = false)
  private OrderMan orders;

  @Transient
  private int totalScheduled;

  @Transient
  private int totalProduced;
   // ...
 }

答案 1

波格丹,谢谢你发布这个。我在Linux上使用Hibernate / JPA / MySQL的Unix / Windows可移植性的大小写表名也遇到了类似的问题。

像您一样,我开始通过在我的META-INF/persistence文件中配置自定义命名策略来将我的表名全部小写.xml:

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

我得到了同样的例外:org.hibernate.DuplicateMappingException:相同的物理表名称...通过使用调试器,我顿悟了,也许我一开始就没有使用DefaultNamingStrategy!所以我把我的基类改成了 org.hibernate.cfg.EJB3NamingStrategy。我相信,这在使用JPA注释时更合适!这是我最后的命名策略:

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 dursun以前的解决方案对我不起作用。


答案 2

推荐