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

强颜欢笑 提交于 2019-12-21 07:15:34

问题


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

public class MyNamingStrategy extends DefaultNamingStrategy {

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

I have applied it by setting this property in persistence.xml:

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

When I do this I get this stacktrace:

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)

What does the

Same physical table name [planning] references several logical table names: [Planning], [OrderProductMan_Planning]

mean?

Entities from the error, simplified as much as I could. Let me know if you need the rest.

@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:


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.



来源:https://stackoverflow.com/questions/4852610/what-does-org-hibernate-duplicatemappingexception-error-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!