Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Am I mapping this correctly?

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am have a tough time trying to figure out when to use joined-subclass, component, many-to-one, etc. I have a POJO called Issue that contains some properties like:



These are foreign keys to other tables. So I have a table called WorkstationModels that has 2 columns. Id, and ModelNumber. What I need to do is when an Issue is written to the database, the id is written in the issue table for these fields. But when I retrieve an Issue from the database, I need the actualy name like ModelNumber instead of the Id.

So, do I need to create classes for each of these columns? Hardware, PrinterModel, etc? What is the best way to write my mapping in Issue.hbm.xml so that I can get what I want? I looked into using a map, set, or list, but as I will only have 1 id per issue, it doesn't make sense to use a collection unless that is the preferred way.

Thanks.
[ October 20, 2004: Message edited by: Gregg Bolinger ]
 
Surya Suravarapu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
So, do I need to create classes for each of these columns? Hardware, PrinterModel, etc? What is the best way to write my mapping in Issue.hbm.xml so that I can get what I want? I looked into using a map, set, or list, but as I will only have 1 id per issue, it doesn't make sense to use a collection unless that is the preferred way.


Ideally, you should have classes for WorkStationModel, Hardware, etc. Then you should have a one-to-many relation from Issue to these classes.

The easiest way is to use Middlegen to generate mapping files. Then run hibernate ant task on those mapping files to generate domain model classes.

- SPS
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Surya Suravarapu:


Ideally, you should have classes for WorkStationModel, Hardware, etc. Then you should have a one-to-many relation from Issue to these classes.

The easiest way is to use Middlegen to generate mapping files. Then run hibernate ant task on those mapping files to generate domain model classes.

- SPS


That doesn't make sense. And I am not going to use the tools. I need to do this by hand so I know what the heck I am doing.

It seems overkill to create a class for 2 properties. Are you sure this is the best way? I won't be updating these tables from the issue at all. They are just there to provide data for this issue. Like a list of States, countries, etc. They are very static.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For my application (using entity beans, converting to Hibernate now) I map each of these "code" attributes to an Integer field in the beans. In the database I have a single lookup table for mapping code to display_text and long_desc and grouping them into lookup tables (states, countries, device types, user statuses, etc).

So far I think we've got similar designs. So do you want to replace the use of Long with classes? So that instead of "public Long getHardwareId()" you'd have "public Hardware getHardware()" with one instance of Hardware for each possible hardware_id in your database?

I don't know yet how to do that, or if it's even worth it versus using a Long directly, but I just wanted to make sure that was what you were asking about. I haven't decided myself if I'll do that as part of the migration to Hibernate.

I have a generic LookupTable class that gets populated (one instance per grouping) at application startup. Subclasses like UserStatus also have constants like NON_MEMBER and ACTIVE for ease of use. The superclass provides isValid(code), codeToText(code), textToCode(text), etc.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic