These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Simple One-to-One mapping question
PostPosted: Tue Apr 15, 2008 8:26 am 
Newbie

Joined: Tue Apr 15, 2008 7:56 am
Posts: 6
Hi Board

I want a one-to-one between classA and ClassB.

When i instantiate classA i want to automaticly, generate a classB entry in the database. I cannot instantiate classB in the contructor of classA, since when im loading the objects back, the default constructor will be called, and i'll make duplicates in the database.




I have looked in the documentation, for one-to-one relations, trying to resolve this.

But i have not come to an answer, how to automaticly make an instance of classB when i create classA.

Can it be done in the mappings file only?

Any suggestions would be welcome.


Hibernate version:1.21

/CSky


Top
 Profile  
 
 Post subject: Simple One-to-One mapping question
PostPosted: Tue Apr 15, 2008 8:56 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi CSky,

Quote:
I cannot instantiate classB in the contructor of classA


I understand why you don't want to instantiate classB in the default constructor of classA. Could you you a different overloaded constructor though? Or a factory method perhaps?

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 15, 2008 9:08 am 
Newbie

Joined: Tue Apr 15, 2008 7:56 am
Posts: 6
I could do it programaticly in the code.

But i would prefer it done through the mappings file only. Since classA is instatiated all over 1.000.000 lines of code.
Like a trigger perhaps, just making a entry in the db, when classA is instatiated.


Top
 Profile  
 
 Post subject: Simple One-to-One mapping question
PostPosted: Tue Apr 15, 2008 9:21 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
I don't believe there is a way of incorporating application/business logic like that in the mapping files.

I would recommend taking the pain of the refactoring, and protecting the default constructor.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 15, 2008 9:30 am 
Newbie

Joined: Tue Apr 15, 2008 7:56 am
Posts: 6
Ok thx for the answers.

I think ill do the factory pattern as you suggested.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.