I want to save an Author after save an Topic object.So I implements Lifecycle interface. But it seem don't work property. After save Topic,the author don't be saved :(
public class Topic implements Serializable,Lifecycle {
private static final Log log=LogFactory.getLog(Topic.class);
/** identifier field */
private Integer id;
/** nullable persistent field */
private Date time;
/** nullable persistent field */
private String title;
/** nullable persistent field */
private String name;
/** full constructor */
public Topic(Date time, String title, String name) {
this.time = time;
this.title = title;
this.name = name;
}
/** default constructor */
public Topic() {
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getTime() {
return this.time;
}
public void setTime(Date time) {
this.time = time;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}
/* (non-Javadoc)
* @see net.sf.hibernate.Lifecycle#onSave(net.sf.hibernate.Session)
*/
public boolean onSave(Session s) throws CallbackException {
Author au=new Author();
au.setAge(22);
au.setEmail("
[email protected]");
au.setName("LIren wang");
Transaction tx=null;
try {
tx=s.beginTransaction();
Object id=s.save(au);
log.info("Saving author ::: "+id);
tx.commit();
} catch (HibernateException e) {
}
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Lifecycle#onUpdate(net.sf.hibernate.Session)
*/
public boolean onUpdate(Session arg0) throws CallbackException {
// TODO Auto-generated method stub
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Lifecycle#onDelete(net.sf.hibernate.Session)
*/
public boolean onDelete(Session arg0) throws CallbackException {
// TODO Auto-generated method stub
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.Lifecycle#onLoad(net.sf.hibernate.Session, java.io.Serializable)
*/
public void onLoad(Session arg0, Serializable arg1) {
// TODO Auto-generated method stub
}
}
I implements the "onSave" method.But the author don't be saved.why? How to use the Lifecycle interface to do this? Help!