HibernateException:实例的标识符已从Alert @ 62794582更改为Alert @ c82ad22

时间:2018-09-18 03:10:14

标签: postgresql hibernate jpa jsonb usertype

我正在使用Hibernate JPA实现持久性API。
我正在使用PostgreSQL 10.4并将数据存储为JSONB。
我正在共享Hibernate和Entity的实现,请指出为什么我会遇到异常。

我跟随这篇精彩的文章来实现自定义UserType,因为Hibernate不支持PostgreSQL JSONB数据类型:
https://www.thoughts-on-java.org/persist-postgresqls-jsonb-data-type-hibernate/

我能够创建实体映射并进行简单的Select查询并打印分配给该映射的值。

问题是当我对事务执行提交操作时,出现以下异常:

  

22:42:35.218 [main]调试   org.hibernate.engine.transaction.internal.TransactionImpl-rollback()   在线程“主”中调用了非活动事务异常   javax.persistence.RollbackException:提交错误   交易于   org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:86)     在com.postgres.main.TestMain.main(TestMain.java:41)上由以下原因引起:   javax.persistence.PersistenceException:    org.hibernate.HibernateException:实例的标识符   com.postgres.model.Alert已更改为   com.postgres.model.Alert@62794582到com.postgres.model.Alert@c82ad22     在   org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)     在   org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)     在    org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:67)     ... 1更多原因:org.hibernate.HibernateException:的标识符   com.postgres.model.Alert的实例已更改为   com.postgres.model.Alert@62794582到com.postgres.model.Alert@c82ad22     在   org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:64)     在   org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:175)     在   org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:135)     在   org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:216)     在   org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:85)     在   org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:38)     在org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1295)     在   org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:468)     在   org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3135)     在   org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2352)     在   org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)     在   org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)     在   org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access $ 100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)     在   org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl $ TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)     在   org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)     在   org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:61)     ...还有1个

我还关注了后面的帖子,那里的人们分享了他们如何解决此问题,但并没有帮助我:
Hibernate: How to fix "identifier of an instance altered from X to Y"?

这是警报实体:

@Entity
@IdClass(Alert.class)
@Table(name = "alert")
public class Alert implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "historyid")
    private Character historyId;

    @Id
    @Column(name = "tenantname")
    private Character tenantName;

    @Column(name = "data")
    @Type(type = "AlertJsonUserType")
    private AlertJson alertJson;

    public Character getHistoryId() {
        return historyId;
    }

    public void setHistoryId(Character historyId) {
        this.historyId = historyId;
    }

    public Character getTenantName() {
        return tenantName;
    }

    public void setTenantName(Character tenantName) {
        this.tenantName = tenantName;
    }

    public AlertJson getAlertJson() {
        return alertJson;
    }

    public void setAlertJson(AlertJson alertJson) {
        this.alertJson = alertJson;
    }
}


这是警报实体中JSONB元素的实体

public class AlertJson implements Serializable 
{
    private static final long serialVersionUID = 1L;

    @JsonProperty("DeviceId")
    private String deviceId;

    @JsonProperty("TenantName")
    private String tenantName;

    @JsonProperty("FocusPointId")
    private List<FocusPointId> focusPointId;

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getTenantName() {
        return tenantName;
    }

    public void setTenantName(String tenantName) {
        this.tenantName = tenantName;
    }

    public List<FocusPointId> getFocusPointId() {
        return focusPointId;
    }

    public void setFocusPointId(List<FocusPointId> focusPointId) {
        this.focusPointId = focusPointId;
    }
}

class FocusPointId
{
    @JsonProperty("PartNumber")
    private String partNumber;

    @JsonProperty("SerialNumber")
    private String serialNumber;

    public String getPartNumber() {
        return partNumber;
    }

    public void setPartNumber(String partNumber) {
        this.partNumber = partNumber;
    }

    public String getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }
  }


下面是我要创建EntityManager实例并发出查询的main()的代码段

public static void main(String[] args)
{
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-persistence-unit");

    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();

    Query alertQuery = em.createNativeQuery("SELECT * FROM \"A.01\".\"Alert\" where tenantname = 'TestUser-152'", Alert.class);
    List<Alert> alertList = alertQuery.getResultList();

    for(Alert jsonElement : alertList)
    {
        System.out.println("======> "+"Device: " + jsonElement.getAlertJson().getDeviceId() + " TenantName: " +jsonElement.getAlertJson().getTenantName());
    }

    em.getTransaction().commit();

    emf.close();
}

以下是描述表和约束。 PK既是historyid又是租户名。

Table Describe

Table Constraints

1 个答案:

答案 0 :(得分:1)

我认为您有这个问题,因为您错误地使用了IdClass注释,例如,您应该创建一个单独的类AlertId,该类将必须包含您组成的PK中的字段,并且该类应用作IdClass注释的参数