- This topic has 8 replies, 4 voices, and was last updated 19 years, 4 months ago by andreod.
-
AuthorPosts
-
Thomas TrostelParticipantOk … after excessive searching on the net we have been unable to unearth an example of code to help us with a particular problem.
There is a table with a composite key containing two strings … code and code_type. MyEclipse will produce a key class for this without a problem now. I’d like to use the Spring framework find method to get a value out of this codes table … so we do something like this:
SomethingCodesKey myKey = new SomethingCodesKey();
myKey.setCode(“Key1”);
myKey.setCodeType(“Key2”);List liResults = getHibernateTemplate.find(“from SomethingCodes as code where code.id = ?”, myKey, Hibernate.OBJECT);
which doesnt work obviously. There is a better way to do this but its not clear what that is and we have not found a good starting place for this question.
Any thoughts out there? If we find the answer before you we will post the answer back π
Thomas TrostelParticipantNevermind … the answer is:
SomethingCode myCode = (SomethingCode) getHibernateTemplate.load(SomethingCode.Class, myKey);
One should really finish their cup of coffee in the morning before posting silly questions π
Thomas TrostelParticipantHmm … still not working
// Construct a new doc CodesKey myKey = new CodesKey(); myKey.setCode("Key1"); myKey.setCodeType("Key2"); Codes myCode = null; try { myCode = (Codes) getHibernateTemplate().load(Codes.class,myKey); } catch (DataAccessException e1) { e1.printStackTrace(); }
throws the error:
org.springframework.orm.hibernate.HibernateSystemException: identifier of an instance of Codes altered from CodesKey@7f565967 to CodesKey@7f565967; nested exception is net.sf.hibernate.HibernateException: identifier of an instance of Codes altered from CodesKey@7f565967 to CodesKey@7f565967
which is terribly non descriptive. Any ideas why this doesn’t work? The record does exist.
GregMemberHave you tried the new Spring forums?
http://forum.springframework.org/
They would be much more equipped to help you with your problem since your issue is spring related and not ME specific.
Thomas TrostelParticipantActually its a Hibernate question π
Riyad KallaMemberAhh ok, that’s totally different.
http://forum.hibernate.org/π
Thomas TrostelParticipantHmmmmm … actually LoadAll(Object) doesn’t work either … trying a short pure hibernate example. This may be a bug.
Thomas TrostelParticipantPfft …. works fine in pure Hibernate … It would appear to be a Spring bug! … Time to go ask them for help. Thank you for the link π
andreodMemberHi, folks. I don’t if you solved this issue, but the solution follows anyway:
Implement toString() method accordingly, using org.apache.commons.lang.builder.ToStringBuilder, from Jakarta Commons Lang (http://jakarta.apache.org/commons/lang/), or another implementation you like.
This problem occurs inside a transaction (im my case). Implementing the toString method correctly and modifying the object, works withou problems, because Hibernate relies on it the make assumptions if the object is the same or not.
I hope this helps.
Regards,
AndrΓ© Dias
-
AuthorPosts