Hi,
I am encountering a quite strange problem when updating persistent objects with Hibernate. I am using Struts2 for my UI. When i update an entry more than once, its value changes with each refresh (and thus each query to Hibernate), alternating between the newly updated value, and the one of the last values that the entry had (I have checked with test printouts and ensured that its not a caching issue on the frontend, Hibernate actually returns different values for each call).
Some code:
HibernateUtil.java
Code:
public void persistObject(DataElement o) {
getSessionFactory().getCurrentSession().beginTransaction();
getSessionFactory().getCurrentSession().saveOrUpdate(o);
getSessionFactory().getCurrentSession().getTransaction().commit();
}
Code:
public List runQuery(String query) {
List result;
getSessionFactory().getCurrentSession().beginTransaction();
result = getSessionFactory().getCurrentSession().createQuery(query).list();
return result;
}
The latter is in turn called by this method, within PersistenceFactory.java:
Code:
public Artifact getArtifact(long uuid) {
List result = hibernateUtil.runQuery("from Artifact where uuid = " + uuid);
System.out.println("Result size: " + result.size());
return (Artifact) result.get(0);
}
Which again is called by a method in the action class:
Code:
public String editArtifact() {
artifactBean = persistenceFactory.getArtifact(uuid);
System.out.println("uuid: " + uuid + " version: " + artifactBean.getVersion());
return "success";
}
Upon refreshing the action responsible for calling the editArtifact() method, the printouts that i get does not make much sense:
Code:
4 4
Hibernate: select artifact0_.uuid as uuid14_, artifact0_.Description as Descript2_14_, artifact0_.Name as Name14_, artifact0_.version as version14_, artifact0_.ArtifactStateType as Artifact1_21_, artifact0_.clazz_ as clazz_ from ( select uuid, Description, Name, version, ArtifactStateType, 7 as clazz_ from Artifact union all select uuid, Description, Name, version, ArtifactStateType, 8 as clazz_ from Specification union all select uuid, Description, Name, version, ArtifactStateType, 9 as clazz_ from Request union all select uuid, Description, Name, version, ArtifactStateType, 10 as clazz_ from Report union all select uuid, Description, Name, version, ArtifactStateType, 11 as clazz_ from Plan union all select uuid, Description, Name, version, ArtifactStateType, 12 as clazz_ from Log union all select uuid, Description, Name, version, ArtifactStateType, 13 as clazz_ from List union all select uuid, Description, Name, version, ArtifactStateType, 14 as clazz_ from Instruction union all select uuid, Description, Name, version, ArtifactStateType, 15 as clazz_ from Diagram union all select uuid, Description, Name, version, ArtifactStateType, 16 as clazz_ from Description ) artifact0_
Hibernate: select artifact0_.uuid as uuid14_, artifact0_.Description as Descript2_14_, artifact0_.Name as Name14_, artifact0_.version as version14_, artifact0_.ArtifactStateType as Artifact1_21_, artifact0_.clazz_ as clazz_ from ( select uuid, Description, Name, version, ArtifactStateType, 7 as clazz_ from Artifact union all select uuid, Description, Name, version, ArtifactStateType, 8 as clazz_ from Specification union all select uuid, Description, Name, version, ArtifactStateType, 9 as clazz_ from Request union all select uuid, Description, Name, version, ArtifactStateType, 10 as clazz_ from Report union all select uuid, Description, Name, version, ArtifactStateType, 11 as clazz_ from Plan union all select uuid, Description, Name, version, ArtifactStateType, 12 as clazz_ from Log union all select uuid, Description, Name, version, ArtifactStateType, 13 as clazz_ from List union all select uuid, Description, Name, version, ArtifactStateType, 14 as clazz_ from Instruction union all select uuid, Description, Name, version, ArtifactStateType, 15 as clazz_ from Diagram union all select uuid, Description, Name, version, ArtifactStateType, 16 as clazz_ from Description ) artifact0_ where artifact0_.uuid=1
Result size: 1
uuid: 1 version: 2
Hibernate: select artifactst0_.uuid as uuid2_, artifactst0_.Description as Descript2_2_, artifactst0_.Name as Name2_ from ArtifactStateType artifactst0_
Hibernate: select artifact0_.uuid as uuid14_, artifact0_.Description as Descript2_14_, artifact0_.Name as Name14_, artifact0_.version as version14_, artifact0_.ArtifactStateType as Artifact1_21_, artifact0_.clazz_ as clazz_ from ( select uuid, Description, Name, version, ArtifactStateType, 7 as clazz_ from Artifact union all select uuid, Description, Name, version, ArtifactStateType, 8 as clazz_ from Specification union all select uuid, Description, Name, version, ArtifactStateType, 9 as clazz_ from Request union all select uuid, Description, Name, version, ArtifactStateType, 10 as clazz_ from Report union all select uuid, Description, Name, version, ArtifactStateType, 11 as clazz_ from Plan union all select uuid, Description, Name, version, ArtifactStateType, 12 as clazz_ from Log union all select uuid, Description, Name, version, ArtifactStateType, 13 as clazz_ from List union all select uuid, Description, Name, version, ArtifactStateType, 14 as clazz_ from Instruction union all select uuid, Description, Name, version, ArtifactStateType, 15 as clazz_ from Diagram union all select uuid, Description, Name, version, ArtifactStateType, 16 as clazz_ from Description ) artifact0_ where artifact0_.uuid=1
Result size: 1
uuid: 1 version: 4
Hibernate: select artifactst0_.uuid as uuid2_, artifactst0_.Description as Descript2_2_, artifactst0_.Name as Name2_ from ArtifactStateType artifactst0_
Hibernate: select artifact0_.uuid as uuid14_, artifact0_.Description as Descript2_14_, artifact0_.Name as Name14_, artifact0_.version as version14_, artifact0_.ArtifactStateType as Artifact1_21_, artifact0_.clazz_ as clazz_ from ( select uuid, Description, Name, version, ArtifactStateType, 7 as clazz_ from Artifact union all select uuid, Description, Name, version, ArtifactStateType, 8 as clazz_ from Specification union all select uuid, Description, Name, version, ArtifactStateType, 9 as clazz_ from Request union all select uuid, Description, Name, version, ArtifactStateType, 10 as clazz_ from Report union all select uuid, Description, Name, version, ArtifactStateType, 11 as clazz_ from Plan union all select uuid, Description, Name, version, ArtifactStateType, 12 as clazz_ from Log union all select uuid, Description, Name, version, ArtifactStateType, 13 as clazz_ from List union all select uuid, Description, Name, version, ArtifactStateType, 14 as clazz_ from Instruction union all select uuid, Description, Name, version, ArtifactStateType, 15 as clazz_ from Diagram union all select uuid, Description, Name, version, ArtifactStateType, 16 as clazz_ from Description ) artifact0_ where artifact0_.uuid=1
Result size: 1
uuid: 1 version: 2
Hibernate: select artifactst0_.uuid as uuid2_, artifactst0_.Description as Descript2_2_, artifactst0_.Name as Name2_ from ArtifactStateType artifactst0_
The "4 4" printout at the top shows the version variable of the element that is being persisted, as well as the version variable of the element through a Hibernate query. This looks as it should. But then, upon editing this same element, the value of version is suddenly 2 (which it was a couple of updates ago). Another refresh (and another Hibernate query), and the value is 4. When refreshing again, the value is back to 2. And all of this without a single call to Hibernate asking it to update or change anything.
The way an element is added/updated:
Code:
if(artifactBean != null) { //if an artifact has been added through struts:
if(artifactBean.getUuid() != null) { //check if it has been stored in hibernate
Artifact tmp = persistenceFactory.getArtifact(artifactBean.getUuid()); //get existing version
tmp.setUuid(artifactBean.getUuid());
//tmp is populated with the values of artifactBean, defined by the user through struts
hibernateUtil.persistObject(tmp); //save tmp
}else { //if the artifact has not been stored, just save it
hibernateUtil.persistObject(artifactBean);
}
Does anyone know what might be wrong here? (I can add more code if needed.)
Regards,
Tobb