我正在尝试强制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)
是什么?
相同的物理表名[planning]引用了几个逻辑表名称:[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;
// ...
}
答案 0 :(得分:2)
波格丹,谢谢发布此内容。我在Linux上使用Hibernate / JPA / MySQL的Unix / Windows可移植性的区分大小写的表名也有类似的问题。
和你一样,我打算通过在META-INF / persistence.xml文件中配置自定义NamingStrategy来将我的表名绑定为全小写:
<property name="hibernate.ejb.naming_strategy" value="my.package.LowerCaseNamingStrategy" />
我得到了同样的异常:org.hibernate.DuplicateMappingException:相同的物理表名...通过使用调试器,我有一个顿悟可能我没有使用DefaultNamingStrategy开始!所以我将我的基类改为org.hibernate.cfg.EJB3NamingStrategy 。我认为,这在使用JPA Annotations时更合适!这是我最后的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解决方案对我不起作用。