@IdClass使用JPA和Hibernate生成'实例的标识符'

时间:2015-01-30 08:19:16

标签: java hibernate jpa orm entity

对于使用不区分大小写的数据库模式的JPA实体模型,当我使用@IdClass注释时,我始终得到实例的标识符被更改'例外。对于带有'字符串的对象'主键,当数据库中存在一个案例的字符串并且仅使用相同的字符串执行查询时,会发生错误。

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

以下是具体错误:

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

2 个答案:

答案 0 :(得分:9)

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

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

在您的示例中,即使您启动了一个新事务,您也必须记住PersistenContext尚未关闭,因此您仍然拥有一个附加到Hibernate会话的托管c1实体。

当您尝试找到公司时:

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

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

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

由于SQL是CASE INSENSITIVE,因此您实际上将选择与当前托管公司实体(持久性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')以及DefaultFlushEntityEventListener.checkId()方法会失败。

根据JavaDoc的说法,此检查适用于:

  

make(ing)确定(用户)没有破坏id

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

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

您可以检查c1是否已附加:

assertTrue(em.contains(c1));

答案 1 :(得分:0)

您似乎是手动将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"到"讴歌"这似乎是根本原因。并且您使用相同的实例c1来表示具有不同ID的对象,即1与" ACURA" "讴歌"。