Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to implement equals for DB model?

 
Paul Brabban
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to represent a relational database and its tables, so I have a class representing a database, and a class representating a table.

In the database class, there is a Set, containing Table objects, as I know that no two tables can have the same name within my database (ignoring more complex database structures for now...)

I can think of two equals() implementations that seem valid. I need to return true if two tables have the same name, so as to ensure no duplicate names in my collection, so that's one. However, if I do that, two table objects will be equal even if they contain different attributes and columns, so a direct comparison of two tables will be misleading.

Any thoughts on how to approach this best?

Many thanks,
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I know that no two tables can have the same name within my database

Knowing this, why do the attributes matter?
 
Paul Brabban
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Attributes & columns would matter if I was to try and implement something that diff'd the tables to identify differences.

eg. the database wouldn't care if two tables had different columns but the same name, but a comparison of the two tables would care that there were different columns.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that equality test is valid if you are trying to copy how an relational database works (object names are always unique within a schema). Perhaps I'm not understanding your requirements?
 
Paul Brabban
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're right - I'm looking at equality in the context of the container (if names are equal) and equality in the context of the object (exactly equal in state).

It's almost like I could do with passing a Comparator-like object for equals to the Set to tell it how to perform the equals() method from its point of view.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic