@IdClass使用JPA和Hibernate生成“实例的标识符已更改" [英] @IdClass Produces 'Identifier of an Instance was Altered' with JPA and Hibernate

查看:172
本文介绍了@IdClass使用JPA和Hibernate生成“实例的标识符已更改"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于使用不区分大小写的数据库架构的JPA实体模型,当我使用@IdClass批注时,我始终会收到实例标识符已更改"异常.对于具有字符串"主键的对象,如果数据库中存在一种情况的字符串,并且仅使用大小写不同的同一字符串执行查询,就会发生错误.

For a JPA entity model using a case-insensitive database schema, when I use a @IdClass annotation I consistently get 'identifier of an instance was altered' exception. For an object with a 'string' primary key, the error occurs when an string of one case exists in the database and a query is performed with the same string differing only in case.

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

I've looked at other SO answers and they are of the form: a) don't modify the primary key (I'm not) and b) your equals()/hashCode() implementations are flawed. For 'b' I've tried using toLowerCase() and equalsIgnoringCase() but to no avail. [Additionally, it seems the Hibernate code is directly setting properties, rather than calling property setters when the 'altering' occurs.]

这是具体错误:

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随后查找其他大写字母?

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

Here is the offending code (starting with an empty database):

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"实现:

and here is the 'db.Company' implementation:

@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了;上面是问题的最简单的例子.

Note: I know using @IdClass isn't needed when there is a single primary key; the above is the simplest example of the problem.

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

As I said, I believe this problem persists even when the hashCode()/equals() methods are made case insensitive; however, suggestions taken.

...
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

推荐答案

发生此错误的原因是由于更改了受管实体的实体标识符.

The reason for this error is due to changing the entity identifier of a managed entity.

在PersistenceContext的生存期内,任何给定实体只能有一个且只有一个托管实例.为此,您不能更改现有的受管实体标识符.

During the life-time of a PersistenceContext, there can be one and only one managed instance of any given entity. For this, you can't change an existing managed entity identifier.

在您的示例中,即使您开始新事务,也必须记住PersistenContext尚未关闭,因此您仍将一个托管的c1实体附加到Hibernate会话.

In you example, even if you start a new transaction, you must remember that the PersistenContext has not been closed, so you still have a managed c1 entity attached to the Hibernate Session.

当您尝试查找公司时:

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

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

The identifier doesn't match the one for the Company that's being attached to the current Session, so a query is issued:

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

由于SQL是CASE INSENSITIVE,因此您实际上将选择与当前托管的Company实体(持久化的c1)相同的数据库行.

Because SQL is CASE INSENSITIVE, you will practically select the same database row as the current managed Company entity (the persisted c1).

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

But you can have only one managed entity for the same database row, so Hibernate will reuse the managed entity instance, but it will update the identifier to:

new Company.Identity("ACURA");

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

You can check this assumptions with the following test:

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

在提交第二笔交易时,刷新将尝试更新实体标识符(已从"Acura"更改为"ACURA"),因此

When the second transaction is committed, the flush will try to update the entity identifier (which changed from 'Acura' to 'ACURA') and so the DefaultFlushEntityEventListener.checkId() method will fail.

根据JavaDoc,此检查适用于:

According to teh JavaDoc, this check is for:

确保用户没有篡改ID

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

要修复此问题,您需要删除以下查找方法调用:

To fix it, you need to remove this find method call:

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

您可以检查是否已连接c1:

You can check that c1 is already attached:

assertTrue(em.contains(c1));

这篇关于@IdClass使用JPA和Hibernate生成“实例的标识符已更改"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆