-->
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.  [ 14 posts ] 
Author Message
 Post subject: afterLoad
PostPosted: Tue Mar 15, 2011 8:33 am 
Newbie

Joined: Tue Mar 15, 2011 8:24 am
Posts: 8
After my pojo has been loaded form the database I want to some additional processing. What is the best way to do this?


Top
 Profile  
 
 Post subject: Re: afterLoad
PostPosted: Tue Mar 15, 2011 9:00 am 
Beginner
Beginner

Joined: Mon Nov 15, 2010 10:39 am
Posts: 27
I think Interceptors might be a good place to start.. Or perhaps adding a method call in the method that loads the object.


Top
 Profile  
 
 Post subject: Re: afterLoad
PostPosted: Tue Mar 15, 2011 9:31 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
If you are using annotations you can do this by specifying a callback method @PostLoad
But I don't know the equivalent for hbm files ...


Top
 Profile  
 
 Post subject: Re: afterLoad
PostPosted: Tue Mar 15, 2011 9:37 am 
Newbie

Joined: Tue Mar 15, 2011 8:24 am
Posts: 8
I tried @PostLoad but it does not seem to work.


Top
 Profile  
 
 Post subject: Re: afterLoad
PostPosted: Tue Mar 15, 2011 10:32 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
It works very well for me!
Can you post your example?


Top
 Profile  
 
 Post subject: Re: afterLoad
PostPosted: Tue Mar 15, 2011 4:51 pm 
Newbie

Joined: Tue Mar 15, 2011 8:24 am
Posts: 8
This is what I tried for @PostLoad. I left all the the getters and setters out.

@Entity
@javax.persistence.SequenceGenerator(name = "GeneratorDBC", sequenceName = "GENERATOR_DBC", allocationSize = 1)
public class DBC extends BO {


@PostLoad
public void doAfterLoad() {
this.dbcDBStatus = this.dbcStatus;
System.out.println("doPostload dbcStatus = " + this.dbcStatus);
}
}


Top
 Profile  
 
 Post subject: Re: afterLoad
PostPosted: Tue Mar 15, 2011 5:17 pm 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
There is too little information for me to help ...
Can you post the full mapping of DBC and BO ?? (without getters and setters but with annotations)
And what is really happening when you launch your example?


Top
 Profile  
 
 Post subject: Re: afterLoad
PostPosted: Tue Mar 15, 2011 5:37 pm 
Newbie

Joined: Tue Mar 15, 2011 8:24 am
Posts: 8
Hi,

here is the code. (some getter setters left out). Application is working well. I am using a hardcoded annotation configuration.

AnnotationConfiguration configuration = new AnnotationConfiguration()
.setProperty("hibernate.connection.driver_class", "org.firebirdsql.jdbc.FBDriver")
.setProperty("hibernate.connection.url", "jdbc:firebirdsql:localhost/3050:D:/Atlas/Database/Atlas.fdb")
.setProperty("hibernate.dialect", "org.hibernate.dialect.FirebirdDialect").setProperty("hibernate.connection.username", "RIDDBA")
.setProperty("hibernate.connection.password", "Vegas1499").setProperty("hibernate.connection.pool_size", "10")
.setProperty("show_sql", "true")
.setProperty("hibernate.current_session_context_class", "org.hibernate.context.ThreadLocalSessionContext");
// .setProperty("hibernate.query.substitutions", "true J, false N"); werkt
// niet

configuration.addAnnotatedClass(Activiteit.class);
configuration.addAnnotatedClass(BO.class);
configuration.addAnnotatedClass(AtlasVersion.class);
configuration.addAnnotatedClass(Client.class);
configuration.addAnnotatedClass(DBC.class);
configuration.addAnnotatedClass(DBCActiviteit.class);
sessionFactory = configuration.buildSessionFactory();



package nl.rid.domein.bo;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import javax.persistence.Version;

import nl.rid.common.message.MessageList;

@MappedSuperclass
public abstract class BO {
public static String SYSTEM_ID = "systemId";

// common fields
protected Integer systemId;
private Integer versionLock;

//
private List<BusinessRule> beforeInsertRules = new ArrayList<BusinessRule>();
private List<BusinessRule> beforeUpdateRules = new ArrayList<BusinessRule>();
private List<BusinessRule> beforeDeleteRules = new ArrayList<BusinessRule>();

public BO() {
addRules();
}

public static String[] getFieldNames() {
return new String[] { "systemId" };
}

protected abstract void addRules();

@Version
@Column(name = "VERSION_LOCK")
public Integer getVersionLock() {
return this.versionLock;
}

public void setVersionLock(Integer aValue) {
this.versionLock = aValue;
}

@Transient
public boolean isNew() {
return (this.systemId == null);
}

public void validateBeforeInsert(MessageList aMessageList) {
for (BusinessRule br : beforeInsertRules) {
br.execute(this, aMessageList);
}
}

public void validateBeforeUpdate(MessageList aMessageList) {
for (BusinessRule br : beforeUpdateRules) {
br.execute(this, aMessageList);
}
}

public void validateBeforeDelete(MessageList aMessageList) {
for (BusinessRule br : beforeDeleteRules) {
br.execute(this, aMessageList);
}
}

protected void addRuleBeforeInsert(BusinessRule aRule) {
beforeInsertRules.add(aRule);
}

protected void addRuleBeforeUpdate(BusinessRule aRule) {
beforeUpdateRules.add(aRule);
}

@Transient
public String getDefaultDisplayValue() {
if (this.systemId == null) {
return "";
}
else {
return this.systemId.toString();
}
}

public void executeOnLoad(Object[] state, String[] propertyNames) {}
}


@Entity
@javax.persistence.SequenceGenerator(name = "GeneratorDBC", sequenceName = "GENERATOR_DBC", allocationSize = 1)
public class DBC extends BO {

private DBCStatus dbcStatus;
private DBCStatus dbcDBStatus;
private Client client;
private Date startDatum;
private Date eindDatum;
private Integer aantalDagen;
private Date sluitingsDatum;
private Medewerker medewerker;
private Integer minutenGerealiseerd;
private ZorgverzekeraarLabel zorgverzekeraarLabel;
private BigDecimal totaalBedrag;
private BigDecimal contractBedrag;
private boolean isBetaald;
private Date betaalDatum;
private BigDecimal betaaldBedrag;
private String vecozoId;
private Integer vecozoStatus;

private Set<DBCActiviteit> dbcActiviteiten = new HashSet<DBCActiviteit>();

public DBC() {
this.dbcStatus = DBCStatus.CONCEPT;
}

public void setDbcStatus(DBCStatus aDbcStatus) {
this.dbcStatus = aDbcStatus;
}

@Column(name = "DBC_STATUS")
@Enumerated(EnumType.STRING)
public DBCStatus getDbcStatus() {
return dbcStatus;
}

@PostLoad
public void doAfterLoad() {
this.dbcDBStatus = this.dbcStatus;
System.out.println("doPostload dbcStatus = " + this.dbcStatus);
}

@Transient
public DBCStatus getDbcDBStatus() {
return this.dbcDBStatus;
}

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "GeneratorDBC")
public Integer getSystemId() {
return this.systemId;
}

public void setSystemId(Integer aSystemId) {
this.systemId = aSystemId;
}

@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "FK_CLIENT")
public Client getClient() {
return this.client;
}

public void setClient(Client aClient) {
this.client = aClient;
}

@Column(name = "AANTAL_DAGEN")
public Integer getAantalDagen() {
return this.aantalDagen;
}

public void setAantalDagen(Integer aAantalDagen) {
this.aantalDagen = aAantalDagen;
}


@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "FK_MEDEWERKER")
public Medewerker getMedewerker() {
return this.medewerker;
}

public void setMedewerker(Medewerker aMedewerker) {
this.medewerker = aMedewerker;
}

@Column(name = "MINUTEN_GEREALISEERD")
public Integer getMinutenGerealiseerd() {
return this.minutenGerealiseerd;
}

public void setMinutenGerealiseerd(Integer aAantal) {
this.minutenGerealiseerd = aAantal;
}

@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "FK_ZORGVERZEKERAAR_LABEL")
public ZorgverzekeraarLabel getZorgverzekeraarLabel() {
return this.zorgverzekeraarLabel;
}

public void setZorgverzekeraarLabel(ZorgverzekeraarLabel aZorgverzekeraarLabel) {
this.zorgverzekeraarLabel = aZorgverzekeraarLabel;
}

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "FK_DBC")
public Set<DBCActiviteit> getDBCActiviteiten() {
return this.dbcActiviteiten;
}

public void setDBCActiviteiten(Set<DBCActiviteit> aDBCActiviteiten) {
this.dbcActiviteiten = aDBCActiviteiten;
}

private void calculateAantalDagen() {
this.aantalDagen = 0;
if ((this.startDatum != null) && (this.eindDatum != null)) {
Calendar start = Calendar.getInstance();
Calendar eind = Calendar.getInstance();
start.setTime(this.startDatum);
eind.setTime(this.eindDatum);
long div = (eind.getTime().getTime() - start.getTime().getTime());
long minuten = div / (1000 * 60 * 60 * 24);
this.aantalDagen = new Integer(new Long(minuten).intValue());
}
}

@Override
protected void addRules() {
BusinessRule validateDBCFieldsNotEmpty = new ValidateDBCFieldsNotEmpty();
// before insert
addRuleBeforeInsert(new ValidateDBCUniqueForClient());
addRuleBeforeInsert(validateDBCFieldsNotEmpty);
addRuleBeforeInsert(new ValidateDBCInitialState());
// before update
addRuleBeforeUpdate(validateDBCFieldsNotEmpty);
addRuleBeforeUpdate(new ValidateDBCStateChange());
}
}





Top
 Profile  
 
 Post subject: Re: afterLoad
PostPosted: Wed Mar 16, 2011 8:50 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
And what is happening exactly?
Do you have an error?
Does the method annotated with @PostLoad is called?
When do you expect this method to be called?


Top
 Profile  
 
 Post subject: Re: afterLoad
PostPosted: Wed Mar 16, 2011 9:22 am 
Newbie

Joined: Tue Mar 15, 2011 8:24 am
Posts: 8
Problem is: method is not called


Top
 Profile  
 
 Post subject: Re: afterLoad
PostPosted: Fri Mar 18, 2011 7:52 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Are you using the EntityManager or a Session ?


Top
 Profile  
 
 Post subject: Re: afterLoad
PostPosted: Fri Mar 18, 2011 11:40 am 
Newbie

Joined: Tue Mar 15, 2011 8:24 am
Posts: 8
Session


Top
 Profile  
 
 Post subject: Re: afterLoad
PostPosted: Fri Mar 18, 2011 11:49 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Ok so I think that's it, I read somewhere that the callback methods could only be used with the EntityManager!
I don't remember where though ...


Top
 Profile  
 
 Post subject: Re: afterLoad
PostPosted: Mon Mar 21, 2011 5:28 am 
Newbie

Joined: Tue Mar 15, 2011 8:24 am
Posts: 8
Thanks for all the effor. I also think that I read that somewhere but cannot find it again.


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