gpt4 book ai didi

java - "org.hibernate.DuplicateMappingException"错误是什么意思?

转载 作者:搜寻专家 更新时间:2023-10-30 20:54:08 25 4
gpt4 key购买 nike

我试图强制 JPA/Hibernate 只生成和使用小写的表名。我已经实现了这样的 NamingStrategy:

public class MyNamingStrategy extends DefaultNamingStrategy {

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

我通过在 persistence.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)

什么是

Same physical table name [planning] references several logical table names: [Planning], [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;
// ...
}

最佳答案

Bogdan,感谢您发帖。对于在 Linux 上使用 Hibernate/JPA/MySQL 的 Unix/Windows 可移植性,我遇到了区分大小写的表名的类似问题。

像你一样,我开始通过在我的 META-INF/persistence.xml 文件中配置自定义 NamingStrategy 来将我的表名绑定(bind)为所有小写:

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

我遇到了相同的异常:org.hibernate.DuplicateMappingException:相同的物理表名称...通过使用调试器,我顿悟了,也许 我一开始并没有使用 DefaultNamingStrategy!所以我将基类更改为 org.hibernate.cfg.EJB3NamingStrategy。我相信,这在使用 JPA 注释时更合适!这是我最终的 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 之前的解决方案对我不起作用。

关于java - "org.hibernate.DuplicateMappingException"错误是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4852610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com