0

I used Hibernate ddl generation of SQL to generate an entity's table (I think.) Anyway, there's hashcode field. My questions are:

  1. Is that field necessary?
  2. If I'm inserting test data in the table using SQL, what do I set the value to? From what I read here, native java implementation uses memory address to feed hash function? (Java -- Object.hashCode() algorithm)
  3. If I wrote a mapped supperclass for all serialized entity to inherit which performed a regular hash of the entity's Id field, would I be causing myself some grief later? (I think I got that idea from 'effective java'.)
3
  • Found why the hashCode in the SQL: Because I put it in the code and hibernate cooperated by putting in the SQL. I reread Effective Java Ch3, scribd.com/doc/36454091/Effective-Java-Chapter3, and found out why I did it(But I"m taking it out now) "If a class is immutable and the cost of computing the hash code is significant,you might consider caching the hash code in the object rather than recalculating iteach time it is requested. If you believe that most objects of this type will be usedas hash keys, then you should calculate the hash code when the instance is created.Otherwise," Commented Sep 29, 2013 at 23:45
  • That explains it. You can also test @Transient annotation or also using the Java keyword transient alongside it. The keyword transient marks any field as being not part of a serialization representation of the object. Hibernate also values the transient keyword. Give it a test drive, it is a nice asset to know about the transient keyword / annotation. Commented Sep 30, 2013 at 4:09
  • @Martin Kersten Oh, I thought it was JUST for serializaton to databases. I now will definitely put that annotation on. thanks. Commented Oct 1, 2013 at 5:24

1 Answer 1

0

There should be by no means a hashCode field in your table. If Hibernate generated such a field, somewhere in your class hierarchy (super-classes) there is a hashCode field defined. Try to find it and mark it as @Transient. Hibernate ignores all fields of Object.class.

If I wrote a mapped supperclass for all serialized entity to inherit which performed a regular hash of the entity's Id field, would I be causing myself some grief later? (I think I got that idea from 'effective java'.)

Of cause you are in great danager to suffer from this later on :-). Do not go with such a field unless you have to like having special instances you assign random hashCodes on instance creation that remain consistent throughout the life-cycle of the entity (including db-life).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.