facebook

Composite Keys? Spring. [Closed]

  1. MyEclipse IDE
  2.  > 
  3. General Development
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #213193 Reply

    Thomas Trostel
    Participant

    Ok … 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 πŸ™‚

    #213216 Reply

    Thomas Trostel
    Participant

    Nevermind … 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 πŸ™„

    #213235 Reply

    Thomas Trostel
    Participant

    Hmm … 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.

    #213245 Reply

    Greg
    Member

    Have 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.

    #213247 Reply

    Thomas Trostel
    Participant

    Actually its a Hibernate question πŸ˜‰

    #213253 Reply

    Riyad Kalla
    Member

    Ahh ok, that’s totally different.
    http://forum.hibernate.org/

    πŸ˜›

    #213259 Reply

    Thomas Trostel
    Participant

    Hmmmmm … actually LoadAll(Object) doesn’t work either … trying a short pure hibernate example. This may be a bug.

    #213262 Reply

    Thomas Trostel
    Participant

    Pfft …. 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 πŸ™‚

    #232913 Reply

    andreod
    Member

    Hi, 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

Viewing 9 posts - 1 through 9 (of 9 total)
Reply To: Composite Keys? Spring. [Closed]

You must be logged in to post in the forum log in