什么是“org.hibernate.DuplicateMappingException”错误的意思? [英] What does "org.hibernate.DuplicateMappingException" error mean?
问题描述
我试图强制JPA / Hibernate生成和使用只有小写的表名。我已经实现了一个像这样的NamingStrategy:
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();
}
}
我通过将此属性设置为持久性来应用它。 xml:
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)
什么是
$ b
What does the
相同的物理表名称[planning]会引用多个逻辑表名称:[Planning],[ OrderProductMan_Planning]
Same physical table name [planning] references several logical table names: [Planning], [OrderProductMan_Planning]
是什么意思?
来自错误的实体,尽可能简化尽我所能。
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;
// ...
}
推荐答案
Bogdan,谢谢你发布这个。我在Linux上使用Hibernate / JPA / MySQL的Unix / Windows可移植性的大小写敏感的表名类似的问题。
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.
像你一样,我开始绑定我的表通过在我的META-INF / persistence.xml文件中配置自定义的NamingStrategy,名称全部小写:
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" />
我得到了相同的异常:org.hibernate.DuplicateMappingException:相同的物理表名...通过使用调试器,我有一个顿悟,也许我没有使用DefaultNamingStrategy开始!所以我将我的基类更改为org.hibernate.cfg.EJB3NamingStrategy 。在使用JPA批注时,这是比较合适的,我相信!这是我最后的NamingStrategy:
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 dursun以前的解决方案并不适合我。
PS the previous solution by dursun did not work for me.
这篇关于什么是“org.hibernate.DuplicateMappingException”错误的意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!