I am implementing persistence API using Hibernate JPA.
I am using PostgreSQL 10.4 and storing data as JSONB.
I am sharing my implementation of Hibernate and Entity, please point why i am getting below exception.
I following this wonderful article to implement custom UserType as Hibernate does not support PostgreSQL JSONB datatype:
https://www.thoughts-on-java.org/persist-postgresqls-jsonb-data-type-hibernate/
I was able to create the Entity mappings and do a simple Select query and print the value assigned to the mapping.
The issue is when i perform a commit operation on the transaction, i get the below exception:
22:42:35.218 [main] DEBUG org.hibernate.engine.transaction.internal.TransactionImpl - rollback() called on an inactive transaction Exception in thread "main" javax.persistence.RollbackException: Error while committing the transaction at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:86) at com.postgres.main.TestMain.main(TestMain.java:41) Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: identifier of an instance of com.postgres.model.Alert was altered from com.postgres.model.Alert@62794582 to com.postgres.model.Alert@c82ad22 at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602) at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:67) ... 1 more Caused by: org.hibernate.HibernateException: identifier of an instance of com.postgres.model.Alert was altered from com.postgres.model.Alert@62794582 to com.postgres.model.Alert@c82ad22 at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:64) at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:175) at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:135) at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:216) at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:85) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:38) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1295) at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:468) at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3135) at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2352) at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231) at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65) at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:61) ... 1 more
I also followed following post where folks shared how they fixed the issue, but it did not help me:
Hibernate: How to fix "identifier of an instance altered from X to Y"?
This is the Alert Entity:
@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;
}
}
This is the Entity for the JSONB element in the Alert Entity
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;
}
}
Below is the snippet of the main() where i am creating a EntityManager instance and issuing a query
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();
}
Below is the describe table and the constraints. PK is both historyid and tenantname.
I think you have this problem because you wrongly use IdClass
annotation, you should create a separate class AlertId
for example that will have to fields from your composed PK, and this class should be used as parameter for IdClass
annotation