*
The moose likes Object Relational Mapping and the fly likes Need help with Mapping(Annotation) and Composition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Need help with Mapping(Annotation) and Composition" Watch "Need help with Mapping(Annotation) and Composition" New topic
Author

Need help with Mapping(Annotation) and Composition

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
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 ]

SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: Sep 18, 2005
Posts: 746
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with Mapping(Annotation) and Composition
 
Similar Threads
Value is session managed bean's attribute is nulled
Apache Derby Sql Statement's
Members are not poppulated?
Can a class be mapped to a table and be Embeddable at the same time?
Mysql auto generated PK used for another table and JPA