簡體   English   中英

如何解決hibernate復合鍵異常(期望IdClass映射)?

[英]How to solve hibernate composite key exception (expecting IdClass mapping)?

以前曾在這里問過這個問題,但沒有一個解決方案對我有用。 由於 SQL Server 2008 表中的復合鍵,我收到以下錯誤。

java.lang.ExceptionInInitializerError
    at HB.Main.<clinit>(Main.java:30)
Caused by: java.lang.IllegalArgumentException: expecting IdClass mapping
    at org.hibernate.metamodel.internal.AttributeFactory$3.resolveMember(AttributeFactory.java:977)
    at org.hibernate.metamodel.internal.AttributeFactory$5.resolveMember(AttributeFactory.java:1035)
    at org.hibernate.metamodel.internal.AttributeFactory.determineAttributeMetadata(AttributeFactory.java:450)
    at org.hibernate.metamodel.internal.AttributeFactory.buildIdAttribute(AttributeFactory.java:139)
    at org.hibernate.metamodel.internal.MetadataContext.buildIdClassAttributes(MetadataContext.java:388)
    at org.hibernate.metamodel.internal.MetadataContext.applyIdMetadata(MetadataContext.java:318)
    at org.hibernate.metamodel.internal.MetadataContext.wrapUp(MetadataContext.java:221)
    at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:274)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:305)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at HB.Main.<clinit>(Main.java:26)
Exception in thread "main" 

我的桌子看起來像這樣

在此處輸入圖片說明

類和映射如下: 實體類

package HB;

import java.util.Objects;

public class EmMonthlyPollTablesEntity
{
    private int monthlyPollId;
    private int tableId;

    public int getMonthlyPollId()
    {
        return monthlyPollId;
    }

    public void setMonthlyPollId(int monthlyPollId)
    {
        this.monthlyPollId = monthlyPollId;
    }

    public int getTableId()
    {
        return tableId;
    }

    public void setTableId(int tableId)
    {
        this.tableId = tableId;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        EmMonthlyPollTablesEntity that = (EmMonthlyPollTablesEntity) o;
        return monthlyPollId == that.monthlyPollId && tableId == that.tableId;
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(monthlyPollId, tableId);
    }
}

身份證類

package HB;

import java.io.Serializable;
import java.util.Objects;

public class EmMonthlyPollTablesEntityPK implements Serializable
{
    private int monthlyPollId;
    private int tableId;

    public EmMonthlyPollTablesEntityPK()
    {
    }

    public EmMonthlyPollTablesEntityPK(int monthlyPollId, int tableId)
    {
        this.monthlyPollId = monthlyPollId;
        this.tableId = tableId;
    }

    public int getMonthlyPollId()
    {
        return monthlyPollId;
    }

    public void setMonthlyPollId(int monthlyPollId)
    {
        this.monthlyPollId = monthlyPollId;
    }

    public int getTableId()
    {
        return tableId;
    }

    public void setTableId(int tableId)
    {
        this.tableId = tableId;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        EmMonthlyPollTablesEntityPK that = (EmMonthlyPollTablesEntityPK) o;
        return monthlyPollId == that.monthlyPollId && tableId == that.tableId;
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(monthlyPollId, tableId);
    }
}

主類

package HB;

import org.hibernate.HibernateException;
import org.hibernate.Metamodel;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import javax.persistence.metamodel.EntityType;

public class Main
{
    private static final SessionFactory ourSessionFactory;

    static
    {
        try
        {
            Configuration configuration = new Configuration();
            configuration.setProperty("hibernate.connection.username", "sa");
            configuration.setProperty("hibernate.connection.password", "");

            configuration.configure();

            ourSessionFactory = configuration.buildSessionFactory();
        }
        catch (Throwable ex)
        {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession() throws HibernateException
    {
        return ourSessionFactory.openSession();
    }

    public static void main(final String[] args) throws Exception
    {
        final Session session = getSession();
        try
        {

            System.out.println("querying all the managed entities...");
            final Metamodel metamodel = session.getSessionFactory().getMetamodel();
            for (EntityType<?> entityType : metamodel.getEntities())
            {
                try
                {
                    final String entityName = entityType.getName();
                    final Query query = session.createQuery("from " + entityName);
                    System.out.println("executing: " + query.getQueryString());
                    for (Object o : query.list())
                    {
                        try
                        {
                            System.out.println("  " + o);
                        }

                        catch (Exception ex)
                        {
                            ex.printStackTrace();
                            ;
                        }
                    }
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                    ;
                }
            }

        }
        finally
        {
            session.close();
    }
}

}

映射

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="HB.EmMonthlyPollTablesEntity" table="EM_MONTHLY_POLL_TABLES" schema="dbo" catalog="HB2">
        <composite-id mapped="true" class="HB.EmMonthlyPollTablesEntityPK">
            <key-property name="monthlyPollId">
                <column name="MONTHLY_POLL_ID" sql-type="int"/>
            </key-property>
            <key-property name="tableId">
                <column name="TABLE_ID" sql-type="int"/>
            </key-property>
        </composite-id>
    </class>
</hibernate-mapping>

可以從這里下載整個 Intellij Idea 項目和表格腳本

通過 hbm.xml 定義的Hibernate復合 id 中存在一個錯誤,導致 JPA 部署中的 java.lang.IllegalArgumentException

根本原因

在 JPA 中,復合 ID 必須是可嵌入的。 因為EmMonthlyPollTablesEntityPK類不可嵌入並且它不是一個組件,所以 JPA 環境永遠不會支持它。

解決方案 1:解決方法

hibernate.cfg.xml禁用元模型填充

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.ejb.metamodel.population">disabled</property>
        <mapping class="..."/>
<!-- ... -->
    </session-factory>
</hibernate-configuration>

解決方案 2:刪除 HBM 映射並使用注釋

據我所知,HBM 映射已被棄用,並將在 Hibernate 的未來版本中刪除。

在這種情況下,您可以選擇使用Composite IdIdClassEmbeddedId方法。 以下示例使用EmbeddedId

實體類

@Entity
@Table(name = "EM_MONTHLY_POLL_TABLES")
public class EmMonthlyPollTablesEntity implements Serializable {

    @EmbeddedId
    private EmMonthlyPollTablesEntityPK id;

    @Column(name = "NAME")
    private String name;

    public EmMonthlyPollTablesEntityPK getId() {
        return id;
    }

    public void setId(EmMonthlyPollTablesEntityPK id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof EmMonthlyPollTablesEntity)) return false;
        EmMonthlyPollTablesEntity that = (EmMonthlyPollTablesEntity) o;
        return id.equals(that.id) &&
                Objects.equals(name, that.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }
}

PK課

@Embeddable
public class EmMonthlyPollTablesEntityPK implements Serializable {

    @Column(name = "MONTHLY_POLL_ID", nullable = false)
    private int monthlyPollId;

    @Column(name = "TABLE_ID", nullable = false)
    private int tableId;

    public EmMonthlyPollTablesEntityPK() {
    }

    public EmMonthlyPollTablesEntityPK(int monthlyPollId, int tableId) {
        this.monthlyPollId = monthlyPollId;
        this.tableId = tableId;
    }

    public int getMonthlyPollId() {
        return monthlyPollId;
    }

    public void setMonthlyPollId(int monthlyPollId) {
        this.monthlyPollId = monthlyPollId;
    }

    public int getTableId() {
        return tableId;
    }

    public void setTableId(int tableId) {
        this.tableId = tableId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        EmMonthlyPollTablesEntityPK that = (EmMonthlyPollTablesEntityPK) o;
        return monthlyPollId == that.monthlyPollId && tableId == that.tableId;
    }

    @Override
    public int hashCode() {
        return Objects.hash(monthlyPollId, tableId);
    }
}

委派PK的getter和setter當然是可用的。 在這種情況下,您不必禁用元模型填充,並且可以從hibernate.cfg.xml刪除 HBM 映射資源

雖然我知道將龐大的遺留代碼庫遷移到 JPA 注釋和 XML 映射是非常令人筋疲力盡的,但我相信這是要走的路。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM