@IdClass 使用 JPA 和 Hibernate 生成 'Identifier of an Instance was Altered'

@IdClass Produces 'Identifier of an Instance was Altered' with JPA and Hibernate

对于使用不区分大小写的数据库模式的 JPA 实体模型,当我使用 @IdClass 批注时,我始终得到 'identifier of an instance was altered' 异常。对于主键为'string'的对象,当数据库中存在一个大小写的字符串,使用相同的字符串进行查询,只是大小写不同时,就会出现该错误。

我看过其他 SO 答案,它们的形式是:a) 不要修改主键(我没有)和 b) 你的 equals()/hashCode() 实现有缺陷。对于 'b' 我已经尝试使用 toLowerCase()equalsIgnoringCase() 但无济于事。 [此外,Hibernate 代码似乎是直接设置属性,而不是在 'altering' 发生时调用 属性 setter。]

具体错误如下:

Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: 
identifier of an instance of db.Company was altered 
 from {Company.Identity [62109154] ACURA}
   to {Company.Identity [63094242] Acura}

问:对于包含公司 'Acura'(作为主键)的不区分大小写的数据库,使用 @IdClass 如何查找其他大写字母?

这是有问题的代码(从一个空数据库开始):

public class Main {    
    public static void main(String[] args) {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("mobile.mysql");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();

        Company c1 = new Company ("Acura");
        em.persist(c1);

        em.getTransaction().commit();
        em.getTransaction().begin();

        c1 = em.find (Company.class, new Company.Identity("ACURA"));

        em.getTransaction().commit();
        em.close();
        System.exit (0);    
    }
}

这里是 'db.Company' 实现:

@Entity
@IdClass(Company.Identity.class)
public class Company implements Serializable {

    @Id
    protected String name;

    public Company(String name) {
        this.name = name;
    }

    public Company() { }

    @Override
    public int hashCode () {
        return name.hashCode();
    }

    @Override
    public boolean equals (Object that) {
        return this == that ||
                (that instanceof Company &&
                        this.name.equals(((Company) that).name));}

    @Override
    public String toString () {
        return "{Company@" + hashCode() + " " + name + "}";
    }

    //

    public static class Identity implements Serializable {
        protected String name;

        public Identity(String name) {
            this.name = name;
        }

        public Identity() { }

        @Override
        public int hashCode () {
            return name.hashCode();
        }

        @Override
        public boolean equals (Object that) {
            return this == that ||
                    (that instanceof Identity &&
                        this.name.equals(((Identity)that).name));
        }

        @Override
        public String toString () {
            return "{Company.Identity [" + hashCode() + "] " + name + "}";
        }
    }
}

注意:我知道当只有一个主键时不需要使用 @IdClass;以上是最简单的问题示例。

正如我所说,即使 hashCode()/equals() 方法不区分大小写,我相信这个问题仍然存在;但是,采纳了建议。

...
INFO: HHH000232: Schema update complete
Hibernate: insert into Company (name) values (?)
Hibernate: select company0_.name as name1_0_0_ from Company company0_ where company0_.name=?
Exception in thread "main" javax.persistence.RollbackException: Error while committing the transaction
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:94)
    at com.lambdaspace.Main.main(Main.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: identifier of an instance of db.Company was altered from {Company.Identity [62109154] ACURA} to {Company.Identity [63094242] Acura}
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:82)
    ... 6 more
Caused by: org.hibernate.HibernateException: identifier of an instance of db.Company was altered from {Company.Identity [62109154] ACURA} to {Company.Identity [63094242] Acura}
    at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:80)
    at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:192)
    at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:152)
    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:231)
    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:102)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:55)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)
    ... 6 more

您似乎正在手动将 ID 分配给由 JPA 本身管理的持久对象,并且您正在尝试更改该实体已经存在的 ID,这是不允许的。

    Company c1 = new Company ("Acura");
    em.persist(c1);

    em.getTransaction().commit();
    em.getTransaction().begin();

    c1 = em.find (Company.class, new Company.Identity("ACURA"));

在上面的代码中,您是否尝试将 "ACURA" 更改为 "Acura",这似乎是根本原因。并且您使用相同的实例 c1 来表示具有不同 ID 的对象,即 1 与 "ACURA" 和 2nd 与 "Acura".

出现此错误的原因是更改了托管实体的实体标识符。

在 PersistenceContext 的生命周期内,任何给定实体只能有一个托管实例。为此,您无法更改现有的托管实体标识符。

在你的例子中,即使你开始一个新的事务,你必须记住 PersistenContext 没有被关闭,所以你仍然有一个托管的 c1 实体附加到 Hibernate Session。

当您尝试查找公司时:

c1 = em.find (Company.class, new Company.Identity("ACURA"));

标识符与附加到当前会话的公司的标识符不匹配,因此发出查询:

Hibernate: select company0_.name as name1_0_0_ from Company company0_ where company0_.name=?

因为 SQL 是不区分大小写的,您实际上 select 与当前管理的公司实体(持久化的 c1)相同的数据库行。

但同一数据库行只能有一个托管实体,因此 Hibernate 将重用托管实体实例,但会将标识符更新为:

new Company.Identity("ACURA");

您可以通过以下测试检查此假设:

String oldId = c1.name;
Company c2 = em.find (Company.class, new Company.Identity("ACURA"));
assertSame(c1, c2);
assertFalse(oldId.equals(c2.name));

当第二个事务提交时,刷新将尝试更新实体标识符(从 'Acura' 更改为 'ACURA'),因此 DefaultFlushEntityEventListener.checkId() 方法将失败。

根据 JavaDoc,此检查用于:

make(ing) sure (the) user didn't mangle the id

要修复它,您需要删除此查找方法调用:

c1 = em.find (Company.class, new Company.Identity("ACURA"));

您可以检查是否已附加 c1

assertTrue(em.contains(c1));