I read many books about hibernate but no one concern with writing business logic into POJO. Maybe it is absolutly clear(we firstly think it too), but we deeply stuck with it.
It will be nice to provide clear rules what DO and what DO NOT in Interceptor method. How POJO can be notificated than will be deleted. Rules if you can stop it. Rules if you can insert another POJO in notification method. etc .. If is preferable way do it with events or Interceptor interface. If you can use session in notification method, if you cant use find() and load() in this methods, etc..
Now I realize you cant use this methods in for example preFlush method to make some checks .. and this means for us Hibernate is unusable for us!!
We realy deeply stuck with it.. with hibernate many exotic exceptions.
One example follows :
We have master and slave tables. Slave have id_master column.
And all we need is when user in UI tier change id_master in slave table we want to reconect to another master.
@Entity
public class Slave {
private Integer id_master;
public void setId_master(Integer id_master){
this.id_master = id_master;
//non working busines logic
//we simply want switch to real master if id_master change
Code:
if (id_master!=null && !id_master.equals(this.id_master)){
master = (Master) getLocalHibernateTemplate().load(Master.class,id_master));
}
}
@ManyToOne(cascade={},fetch=FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="id_master", referencedColumnName="id_master", unique=false, nullable=false, insertable=false, updatable=false)
})
private Master master;
public Master getMaster() { return this.master; }
public void setMaster(Master master) {
//remove old
if (this.master != null) {
this.master.getSlaves().remove(this);
}
//add new
this.master = master;
if (this.master != null) {
this.master.getSlaves().add(this);
}
}
}
In
http://www.hibernate.org/116.html#A25 I found simple statement from hibernate team this do not work :
Also, make sure that a call to an accessor method couldn't do anything wierd ... like initialize a lazy collection or proxy.
But how-to write this kind of code ?
Need help.