-->
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.  [ 3 posts ] 
Author Message
 Post subject: Lazy init in a (possible ) pojo class. best practice questio
PostPosted: Wed Feb 01, 2006 5:09 am 
Newbie

Joined: Wed Feb 01, 2006 5:01 am
Posts: 2
Sorry for the vague topic but that was all I could fit into the subject line.

I have a class. The class was nice and safe as it didn't define default
constructors, however I want the class to be hibernate compatable.

Will initializing the variables inside the method body cause problems
for hibernate or does it work a layer above this ?

What would be best practice to ensure the variables are allways
initialized ?

Thanks

Brian

import java.math.BigDecimal;
import java.util.Currency;

/**
* @author brian
*/
public class PaymentAmount {

private Currency currency;

private BigDecimal amount;

public PaymentAmount(BigDecimal amount, Currency currency) {
this.amount = amount;
this.currency = currency;
}

public PaymentAmount() {
super();
}

public PaymentAmount(BigDecimal amount) {
this(amount, Currency.getInstance("EUR"));
}

public BigDecimal getAmount() {
if(this.amount == null) {
this.amount = new BigDecimal("0");
}
return this.amount;
}

public Currency getCurrency() {
if(this.currency == null) {
//TODO Is initialising the variables this way compatable with the way that hibernate works ?
this.currency = Currency.getInstance("EUR");
}
return this.currency;
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 5:39 am 
Newbie

Joined: Fri Dec 23, 2005 7:54 am
Posts: 12
You can make the default constructor package protected; Hibernate will use this when proxy-ing your class.
But you still can ensure that fields are initialized using your custom constructor, being the only one with public visibility...

so
Code:
public PaymentAmount(BigDecimal amount, Currency currency) {
this.amount = amount;
this.currency = currency;
}

protected PaymentAmount() {}


Hope this helps,
Francesco


Top
 Profile  
 
 Post subject: that answers my question, thanks
PostPosted: Wed Feb 01, 2006 5:52 am 
Newbie

Joined: Wed Feb 01, 2006 5:01 am
Posts: 2
that answers my question, thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.