java “org.hibernate.DuplicateMappingException”错误是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4852610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "org.hibernate.DuplicateMappingException" error mean?
提问by Bogdan
I'm trying to force JPA/Hibernate to generate and use only lowercase tablenames. I've implemented a NamingStrategy like this:
我试图强制 JPA/Hibernate 生成并仅使用小写表名。我已经实现了这样的 NamingStrategy:
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:
我通过在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]
相同的物理表名 [planning] 引用了几个逻辑表名:[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;
// ...
}
回答by Steve Jones
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.
Bogdan,感谢您发布此信息。我在 Linux 上使用 Hibernate/JPA/MySQL 的 Unix/Windows 可移植性的区分大小写的表名有类似的问题。
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:
像您一样,我开始通过在 META-INF/persistence.xml 文件中配置自定义 NamingStrategy 来将我的表名绑定为全部小写:
<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:
我得到了同样的异常: org.hibernate.DuplicateMappingException: Same physical table name... 通过使用调试器,我顿悟了,也许我一开始没有使用 DefaultNamingStrategy !所以我将我的基类更改为 org.hibernate.cfg.EJB3NamingStrategy。这在使用 JPA Annotations 时更合适,我相信!这是我最后的命名策略:
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.
PS,dursun 以前的解决方案对我不起作用。