Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes How to implement equals for DB model? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to implement equals for DB model?" Watch "How to implement equals for DB model?" New topic
Author

How to implement equals for DB model?

Paul Brabban
Ranch Hand

Joined: Jan 13, 2008
Posts: 44
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,


"The important thing is not to stop questioning."
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


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

Knowing this, why do the attributes matter?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Paul Brabban
Ranch Hand

Joined: Jan 13, 2008
Posts: 44
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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Jan 13, 2008
Posts: 44
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to implement equals for DB model?
 
Similar Threads
Hibernate Annotations mapping with inheritance
Criteria across multiple tables
How to map one class to multiple similar tables with Hibernate?
Multiple Tables
Arraylist with Objects