hashCode « Data Type « JPA Q&A





1. equals and hashcode in Hibernate    stackoverflow.com

How should model class's equals and hashcode be implemented in Hibernate? What are the common pitfalls? Is the default implementation good enough for most cases? Is there any sense to use ...

2. To equals and hashcode or not on entity classes, that is the question    stackoverflow.com

I have been trying to reason about the best way to handle whether it is generally good practice to implement hashcode and equals on entities (I mean entity in the general ...

3. Entities equals(), hashCode() and toString(). How to correctly implement them?    stackoverflow.com

I'm implementing equals(), hashCode() and toString() of my entities using all the available fields in the bean. I'm getting some Lazy init Exception on the frontend when I try to compare the ...

4. Should @Transient property be used in equals/hashCode/toString?    stackoverflow.com

this is a quick question: I have JPA entities where some properties are annotated with @Transient. Shall I use these properties in equals/hashCode/toString methods? My first thought is NO but.... I don't know ...

5. How to implement equals() and hashcode() methods in BaseEntity of JPA?    stackoverflow.com

I have BaseEntity class which is a superclass of all JPA entities in my application.

@MappedSuperclass
public abstract class BaseEntity implements Serializable {

    private static final long serialVersionUID = -3307436748176180347L;
 ...

6. Using natural keys as part of equals and hashCode    stackoverflow.com

I know that this topic has been discussed umpteen number of times, but still need some clarifications on the issue we are facing. Hibernate best practices talk about using ...

7. apache commons equals/hashcode builder    stackoverflow.com

I'm curious to know, what people here think about using org.apache.commons.lang.builder EqualsBuilder/HashCodeBuilder for implementing the equals/hashcode? Would it be a better practice than writing your own? Does it play well with Hibernate? ...

8. Changing hashCode of object stored in hash-based collection    stackoverflow.com

I have a hash-based collection of objects, such as HashSet or HashMap. What issues can I run into when the implementation of hashCode() is such that it can change with time ...

9. Implement hashCode and equals on base entity class (possibly using reflection)?    stackoverflow.com

I am wondering whether it is a good practice to have hashCode an equals method in the base entity class so that other entity classes that inherit the base class do ...





10. equals() and hashCode() when storing in a Set, in Hibernate    stackoverflow.com

In Hibernate documentation in chapter 4.3. "Implementing equals() and hashCode()" they wrote this sentence "Furthermore, if an instance is unsaved and currently in a Set, saving it will assign ...

12. How hibernate uses equals() and hashCode()?    stackoverflow.com

If you load an entity from db and modify it somehow, will hibernate use equals/hashCode to compare current state of entity with it's snapshot to determine if sql update needs to ...

13. JPA, equals and hashCode    java.net

Option 1: Leave as Object.equals and Object.hashCode + Less code to write + Can add multiple new child entities into a collection - Could have issues adding persisted entities into a collection if you manage to get them from different sessions - Have to search the entire collection with an iterator to check for contents

14. equals and hashCode not invoked    forum.hibernate.org

15. Problem with hashCode() implementation    forum.hibernate.org

Author Message Stef_Kuypers Post subject: Problem with hashCode() implementation Posted: Tue Oct 20, 2009 6:14 am Newbie Joined: Fri Aug 14, 2009 8:06 am Posts: 19 The docs say that to make sure objects from different sessions behave correctly in respect to hashCode() and equals() methods one should override these methods and use hashCode() and equals() on the business ...

16. _not_ a yet another broken hashCode problem!    forum.hibernate.org

Newbie Joined: Wed Nov 04, 2009 9:30 am Posts: 1 Hello everyone! I'm quite new to Hibernate (has been fiddling with it for a week now). About 3 days ago I came across a very annoying bug that I was unable to correct ever since. The problem lies in a many-to-many relationship between objects called RawFilters, DataOrigins and RawData. Specifically a ...





17. Why doesn't SimpleExpression implement hashcode and equals?    forum.hibernate.org

Newbie Joined: Mon Mar 22, 2010 8:45 pm Posts: 1 I have a class called WhereClause that has the following method... Code: public void appendWhereClause( Criteria query ) { if ( !CollectionUtils.isEmpty( this.getProperties() ) ) { ...

18. Hibernate equals() and hashCode() not called    forum.hibernate.org

dear all, i am using Hibernate 3.5.4 final with Annotations Configuration although I implemented the methods equals() and hashCode() in the persistence classes they are not called by hibernate. I inserted some print-outs, but nothing is written on console. Also when putting breakpoints, they are never reached. The classes are using the annotations: ------------------------------------------------------ @Entity @Inheritance(strategy = InheritanceType.JOINED) ------------------------------------------------------ and several ...

19. Best practice? Lazy collection in equals()/hashCode()    forum.hibernate.org

Hi, We have a few classes from our legacy app that uses lazy-loaded collections in their equals/hashCode methods. I have read that this is bad practice as the state of this classes could mutate. However, it was working fine in Hibernate 3.2.6 without issues and now with the newer 3.5/3.6 we get an org.hibernate.AssertionFailure: collection [...] was not processed by flush() ...

20. hashcode & equals automation?    forum.hibernate.org

Hi all, I just wondered whether there's a helper class or an other mechanism in hibernate which calculates hashCode() values and performs the equals()-method. If not, take my question as a feature request. It shouldn't be too difficult to build since hibernate has all needed information about the objects properties in its mapping files and already makes strong use of reflection. ...

21. proxy/hashCode/equals    forum.hibernate.org

Gavin (I hope you will read this), I found another source of ConcurrentModificationException in my code. It is nothing new - just another collection of proxied classes with equals() overriden. Unfortunately I have spent alot of time before realizing this exception is caused by my simplest possible equals() implementation ( which only compares Ids ). Since Hibernate does not know how ...

22. HELP! problems with hashCode()    forum.hibernate.org

I'm using Hibernate 2.1.1. I want to insert a Mp with two MpRequisitosQual in it's "Set". But when I do this, it only inserts the first MpRequisitosQual! By the generated code (hbm2java), I see that the equality between two objects is made via there "id". And when I add the second MpRequisitosQual to the "Set", he's discarded because there's already an ...

23. HELP! problems with hashCode()    forum.hibernate.org

I'm using Hibernate 2.1.1. I want to insert a Mp with two MpRequisitosQual in it's "Set". But when I do this, it only inserts the first MpRequisitosQual! By the generated code (hbm2java), I see that the equality between two objects is made via there "id". And when I add the second MpRequisitosQual to the "Set", he's discarded because there's already an ...

24. equals() / hashCode(): Is there *any* non-broken approach?    forum.hibernate.org

We're grappling with how to implement equals()/hashCode() on our persistent objects. The Hibernate page on the subject recommends using a "semi-unique key" for equals, instead of ID. That doesn't work -- very few of our objects actually have such a key, and IMO it's terrible advice anyway: "Use some other ID that is not the ID as your ID for the ...

25. Proxies with hashCode() and equals()    forum.hibernate.org

Not sure if I can make it any clearer than all the resources you say you have already seen, but here it goes (maybe I can at least make it more condensed): The issue has to do not so much with the entity itself defining equals/hashCode. Its more a problem of those entities which do define an equals/hashCode being included in ...

26. hashCode, equals, Proxies and 2nd Level Cache - solution?    forum.hibernate.org

Newbie Joined: Wed May 19, 2004 1:25 pm Posts: 11 Okay, I'm very new to hibernate. I've got the problem where I'm composing a Set before I save an object graph. After a bit of thinking, and a bit of reading, I dont think anyone else has the solution. fyi Its obvious that the hashcode will change if its based on ...

27. Can't hibernate enhance equals/hashcode() too?    forum.hibernate.org

I have read the article from wiki and , the this way of implementing equals and hashcode() is really weird. Can't hibernate enhance equals/hashcode() too? say , when add 2 new instance in session, hibernate compare them with instance equality, when 2 detached compared,or 1 detached instance with 1 instance loaded in session, hibernate can compare them with orig ID( hibernate ...

28. equals() and hashCode() philosophy.    forum.hibernate.org

In my last go-around with our domain model (using entity beans) our equals() implementation only took into account the surrogate primary key value. This was problematic for clients while creating new objects (which would only compare references), and in moving the code to hibernate I took note of the HiA book's warning that an implementation of hashCode() which involved the surrogate ...

29. HashCode - Equals    forum.hibernate.org

Hi! Even it is an old posting, I think it is still up to date. I have read the very good description about hashCode() and equals() in 'Java Persistence with Hibernate' and understand why to use the business key. However I see two problems with it: As described above the usage of the business key can make from an lazy association ...

30. About equals and hashcode in hibermate java files    forum.hibernate.org

In "Hibernate in Action" I read about the best practice to implement hashcode and equals for the persistent objects. It is not a good practice to use the "surrogate key" (database identifier property) because Hibernate doesnt assign identifier values until an entity is saved. Also it is not a good practice to include all persistent properties of the persistent class because ...

31. hashCode called before properties are initialized    forum.hibernate.org

Hibernate version: 3.0.2 Hi, When I fetch an object from the database using Code: ...

32. hashCode/equals questions - again    forum.hibernate.org

Hibernate version: 3.0.3 Can anyone shed some light on how to come up with the correct way to implement hashCode/equals methods for persistent objects stored as children in collection of parent objects? I have a model with some classes containing collections (sets and lists) of other, polymorphic, objects. Some of these objects have a 'business key' which depends on relationship (eg. ...

33. equals() and hashCode()    forum.hibernate.org

34. Collections in hashCode and equals or not?    forum.hibernate.org

36. Set and HashCode    forum.hibernate.org

Hi, I have the classes: UnidadMedida and ConversionUnidadMedida public class UnidadMedida { private Long id = null; private int version; private String nombre; private Set convertibleA; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public int getVersion() { return version; } public void setVersion(int version) { this.version = version; } public String getNombre() ...

37. Feedback on an equals/hashCode approach    forum.hibernate.org

Newbie Joined: Thu Oct 06, 2005 7:23 pm Posts: 7 I'd like to present and gain feedback on an alternate approach that we've come up with to the whole equals/hashCode problem. We haven't done extensive testing, and I'd like to hear about gotchas from those in the know prior to making a significant investment. After reading what's available (forums, Hib In ...

38. Hashcode Computations    forum.hibernate.org

I have some questions concerning the instructions on pp. 124-125 of Hibernate in Action where it talks about business key equality. 1. What is the recommended way to compute hashcodes when there is more than one property in the business key? Is it recommended to use an implementation like that on page 124? If so, how were the numbers "14" and ...

39. hashCode question    forum.hibernate.org

Hello to everybody, I have the problem concerning hashCode/equals. I have 2 entities DbRssSpider and DbRssProperty an the following relation: Spider: Code: @OneToMany(mappedBy = "spider", cascade = { CascadeType.ALL}, targetEntity = DbRssProperty.class) @OrderBy("id") public Set getProperties() { return propertySet; } ...

40. Deep or Shallow: equals() and hashcode()?    forum.hibernate.org

Hibernate wants me to implement equals() and hashcode() for my persistent classes. Fine. However, this seems to be at odds with lazy loading and collections. For example: Category: String mNameString = "unnamed"; Set mItemSet = new HashSet(); Set mChildCategorySet = new HashSet(); Item: String mNameString = "blah"; double mPrice = 0.0; If category implements equals() to include comparison of the Set ...

41. Do I have to implement equals() and hashcode()?    forum.hibernate.org

42. is there a better way to impl equals() and hashcode() ?    forum.hibernate.org

as in the pojo, I can't find the unique properties in some case, that is, they might all be the same except the database identifier. I know it's a bad way of using database identifier to impl equals() and hashcode(), is there a better way when I can't determine the "bussiness key"?

43. How to write hashCode and compareTo methods    forum.hibernate.org

I need to mix saved and unsaved objects in Set. I have the following equals method: Code: public boolean equals(Object obj) { if (!(obj instanceof MyClass)) return false; else { ...

44. Sets and contains - equals and hashcode    forum.hibernate.org

Hi I have a persistent object "Model", which contains a set of persistent objects "Feature". "Feature" has a single parameter, "name". In "Feature" I have overridden equals and hashcode to check the "name" value instead. e.g. Code: public int hashCode() { return this.getName().toUpperCase().hashCode(); } public boolean equals( Object _obj ) { if ( _obj instanceof Feature ) ...

45. hashcode() and equals()    forum.hibernate.org

Hi, I have one many-to-many association table DepartmentToUser with columns: DepartmentID UserID PrimaryCode Now I want to create the domain object for it and override the hashcode() and equals() method. What properties should I use for that? I think I can not use DepartmentID and UserID because they are set by Hibernate when the objects are saved. PrimaryCode alone is not ...

46. Equals and HashCode    forum.hibernate.org

Hey, I know that this is the most popular discussion, so sorry for raise it again. In my application the objects are equals if they are equals in all the fields (except ID). So few questions: 1. Do i need to check equals on all the fields include references, collections and etc. I looks on equals example and didnt saw that ...

47. Hashcode and Equals on PK    forum.hibernate.org

Beginner Joined: Sat Oct 20, 2007 8:28 am Posts: 28 hi here is my entity : Code: @Entity @AccessType("property") @Table(name = "stats") @IdClass(StatsPK.class) public class Stats implements Serializable,Comparable{ private String statType; private String paperName; private Date beginDate; private Date endDate; private double value; ...

48. Do not initialize proxy when calling my overriden hashCode    forum.hibernate.org

I've implemented a custom hashCode (and equals) method based on the id of my persistent object. This hashCode method works, even if it's a proxy. Code: @Override public int hashCode() { // getClass() is not hashbaar ...

49. Many-To-Many update with mutable hashCode    forum.hibernate.org

@javax.persistence.Column(name = "SYSTEM_ID", insertable = true, updatable = true) public java.lang.String getSystemId() ... public int hashCode() { int hashCode = 0; hashCode = ...

50. Problem with equals / hashcode    forum.hibernate.org

Newbie Joined: Mon Jun 05, 2006 12:41 pm Posts: 19 Hibernate version: 3.2final Mapping documents: Code:

51. hashcode and equals not used    forum.hibernate.org

Hibernate version: 3.3 Mapping documents: @Entity(name="T_APPLICATION") public class Application { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="APPLICATION_ID") private long id; @Column(name="APPLICATION_NAME", nullable=false) private String name; @Column(name="APPLICATION_CODE", nullable=false, unique=true) private String code; @OneToMany(mappedBy="relatedApplication", cascade=CascadeType.ALL) private List fileFormatsToMonitor; ... } @Entity(name="T_FILEFORMAT") public class FileToParse { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="FILEFORMAT_ID") private long id; /** * the path to the file, from the server */ @Column(name="FILEFORMAT_PATH") ...

52. Removing Object from PersistentSet: NOT a hashCode problem!    forum.hibernate.org

Hello, I have a problem with removing an object from a 1:n relation from a PersistentSet. Removing the object works fine when done through the running application. However, the object is not removed when exactly the same code is executed initiated through a JUnit test case with exactly the same data. I'm sure it is not the usual problem of the ...