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'.)
Community
  • 1
  • 1
Dennis
  • 747
  • 7
  • 15
  • 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, http://www.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," – Dennis Sep 29 '13 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. – Martin Kersten Sep 30 '13 at 04:09
  • @Martin Kersten Oh, I thought it was JUST for serializaton to databases. I now will definitely put that annotation on. thanks. – Dennis Oct 01 '13 at 05:24

1 Answers1

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

Martin Kersten
  • 5,127
  • 8
  • 46
  • 77