• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

n-n relationship

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have two tables : Brand and Type . They have a n-n relationship , so I have a table BrandType . Do I must create a class named BrandType ? If dont , where I put a function which manipulates on table BrandType ? For example , I have a function which selects max id on table BrandType , where I put that function ( on class Brand or on class Type ) ?
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anh,

It is a design choice. This choice dependes on your architecture.
If you use DAO you may create a BrandDAO that has responsability to access the 3 tables.
In fact, the table BrandType has no meaning in a OO business domain.

Vagner
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What often happens as a design/app matures is that a simple relationship starts accumulating properties, and at that point you wish you had defined a class to represent it. I'm learning Hibernate now, and its documentation points out that its easy to stipulate a bidirection many-to-many association, to support the POJOs:
(or List, etc...) but, again, the relationship isn't being modeled per se.


Again, don't say BrandType doesn't having a business meaning. A relationship is a concept, concepts begat classes!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anh Vu:
For example , I have a function which selects max id on table BrandType , where I put that function ( on class Brand or on class Type ) ?



What do you need that id for?
 
Vu Pham
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a column named BrandTypeID (primary key) . That column doesn't increase automatically , so I must get id to insert new row . Ok , It's just the example , I also get BrandName and something on that table .
BrandType has no meaning in the real world .
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the boundary between OO and Relational some less than ideal things have to happen. Your Java object model might be as Jeff suggested, where Brand and Type each contain a collection of the other, and the data accessors right at the boundary can secretly read and write the BrandType table.

If the relationship has its own attributes, maybe a name or effective date, a BrandTypeRelationship object would not be out of the questions.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anh Vu:
I have a column named BrandTypeID (primary key) . That column doesn't increase automatically , so I must get id to insert new row . Ok , It's just the example , I also get BrandName and something on that table .
BrandType has no meaning in the real world .



OK, so the id has only meaning in the database model.

Ideally, your domain classes - Brand and Type - shouldn't know anything about the database. (You should be able to switch to a different persistence mechanism - XML, OODB, flat files... - without touching your domain classes.)

Incrementing the primary key sounds more like the responsibility of the persistence layer, for example a DAO.
 
Vu Pham
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all . Now I can understand what you said . Thanks again
 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic