我正在尝试使用复合主键进行Hibernate查找,但始终出现以下错误:

Caused by: org.hibernate.exception.SQLGrammarException: Invalid column name 'merchantNumberAccountTypeId'.
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at com.sun.proxy.$Proxy19.executeQuery(Unknown Source)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1953)
    at org.hibernate.loader.Loader.doQuery(Loader.java:829)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    at org.hibernate.loader.Loader.loadEntity(Loader.java:2033)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3720)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:458)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:427)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:204)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:260)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1079)
    at org.hibernate.internal.SessionImpl.access$2200(SessionImpl.java:172)
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2425)
    at org.hibernate.internal.SessionImpl.get(SessionImpl.java:975)
    at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:807)
    ... 30 more


我可以持久地保留Entity,但不会出现任何问题(我也尝试过直接创建查询,但遇到同样的问题)。

我的Entity类如下所示:

@Entity
public class MerchantNumberAccountType implements Serializable {

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private MerchantNumberAccountTypeId merchantNumberAccountTypeId;

    public MerchantNumberAccountTypeId getMerchantNumberAccountTypeId() {
        return merchantNumberAccountTypeId;
    }

    public void setMerchantNumberAccountTypeId(MerchantNumberAccountTypeId merchantNumberAccountTypeId) {
        this.merchantNumberAccountTypeId = merchantNumberAccountTypeId;
    }

    @Embeddable
    public static class MerchantNumberAccountTypeId implements Serializable {

        private static final long serialVersionUID = 1L;

        @Column
        public String merchantNumber;
        @Column
        public String accountType;

        public MerchantNumberAccountTypeId(){

        }

        @Override
        public boolean equals(Object other){
            if(this == other){
                return true;
            }
            if (!(other instanceof MerchantNumberAccountTypeId)) {
                return false;
            }
            MerchantNumberAccountTypeId otherAccTypeId = (MerchantNumberAccountTypeId) other;

            return this.merchantNumber.equals(otherAccTypeId.merchantNumber) && this.accountType.equals(otherAccTypeId.accountType);

        }

        @Override
        public int hashCode(){
            return merchantNumber.hashCode() + accountType.hashCode();
        }

        public String getMerchantNumber() {
            return merchantNumber;
        }

        public void setMerchantNumber(String merchantNumber) {
            this.merchantNumber = merchantNumber;
        }

        public String getAccountType() {
            return accountType;
        }

        public void setAccountType(String accountType) {
            this.accountType = accountType;
        }
    }

}


更新:

我已经找到了解决方法。我从使用@EmbeddedId策略更改为@IdClass。但是,当我执行此操作时,实例化EnitityManagerFactory时出现以下错误-由以下原因引起:org.hibernate.DuplicateMappingException:表[tblmerchantnumberaccounttype]包含由多个物理列名称引用的逻辑列名称[accountType]:[id.accountType] ,[identifierMapper.accountType]。为了防止将物理列的ID和identifierMapper附加到它们的前面。 (任何人都可以告诉我为什么会这样吗?)如果您的@Column不带@,则我用@Column(value =“ AccountType)&@Column(value =” MerchantNumber“)在Id类中注释了accountType和商家编号。值属性仍然失败,此后仍然失败,因为现在物理列名称在大写字母之前得到了加下划线,即AccountType成为account_type。为了解决这个问题,我可以使用自己的版本覆盖EnhancedNamingStrategy中的columnName方法那不加下划线,或者我可以简单地使用@Column(value =“ accounttype”)有人可以向我解释这里发生了什么吗?非常感谢。

更新2:
@EmbeddedId方式也可以。注释键类中的成员变量并显式为其指定列名(无大写字母!),这一点很重要。

最佳答案

我遇到了同样的问题。
我使用@PrimaryKeyJoinColumns解决了这个问题,尝试一下(从另一个示例中):

@ManyToOne
@PrimaryKeyJoinColumns(value = {
    @PrimaryKeyJoinColumn(name = "country_code", referencedColumnName = "country_code"),
    @PrimaryKeyJoinColumn(name = "zip_code", referencedColumnName = "code")
})
private Zip zip;

@ManyToOne
@PrimaryKeyJoinColumns(value = {
    @PrimaryKeyJoinColumn(name = "country_code", referencedColumnName = "country_code"),
    @PrimaryKeyJoinColumn(name = "state_code", referencedColumnName = "state_code"),
    @PrimaryKeyJoinColumn(name = "city_name", referencedColumnName = "name")
})
private City city;


来自Hibernate throws AnnotationException on column used by multiple overlapping foreign keys

关于java - 使用复合键进行 hibernate 查找。无效的列名异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16335513/

10-09 01:42