• 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

Need help with Mapping(Annotation) and Composition

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys! I have a problem... I have this mapping in my class named FixedItem. And with it is a member named assignedTo which is mapped to an int in a relational table. This is suppose to be a foreign key but I decided not to make it one because I'm not gonna be relating it in the database but instead get its value from a webservice.



Now, I wanted to change the member from an int to a class named Employee which looks like this...





thus, changing the code to



but I don't know where to put the annotations and which mapping to make. The employeeId needs to be the one mapped to the database table field. assigned_to.

If you need to take a look at my FixedItem class and its mapping, here it is...




Sorry for my ignorance, I'm just depending on the netbeans auto-generated code and so are my mappings. Please help me form my annotation and mapping. Thanks!
[ May 11, 2007: Message edited by: Timothy Sam ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well here is some things, you can have two instance variables for assignedTo,

meaning

private int assignedTo;

and

private Employee assignedEmployee;

Now the assignedTo is still mapped to the int column, because it sounds like you can't change the database.

Now what you will have to do to get the assignedEmployee, is to now look it up.

And to go the other way, you have the assignedEmployee, and call the getId from it to get the value to set assignedTo to.

Personally, If it is a referential integrity need, make the foreign key relationship. You have too many ways to mess up the data if you don't. Then you will only need the @ManyToOne for private Employee assignedTo;

Mark

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see your topic has Composition, if it is truely composition, then look at the @Embeddable and @Embedded annotations and see if that is what you are looking for.

Mark
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Thank you for your suggestion. I'm off the office now so I can't test them yet. Your first suggestion would seem to work. But it sounds like I'll be doing a hack and I'm not a good fan of hacking my own code. Sounds very clever and very tempting though. I will be trying the @Embeddable and @Embedded annotations once I get back to work. I surely hope this could solve my problem.


There is no referential integrity for the assigned_to field by the way... It's just an ordinary int value with no relationship whatsoever. I will be getting its value from a webservice.

Thank you very much for your reply!
reply
    Bookmark Topic Watch Topic
  • New Topic