Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp
2.1.6
<class name="Topic" table="pagertest">
<id
column="ID"
name="id"
type="integer"
>
<generator class="native" />
</id>
<property
column="TIME"
length="19"
name="time"
not-null="false"
type="timestamp"
/>
<property
column="TITLE"
length="100"
name="title"
not-null="false"
type="string"
/>
<property
column="NAME"
length="20"
name="name"
not-null="false"
type="string"
/>
</class>
////////////////////////////////////////////////////////////////////////
<class name="Author" table="author">
<id
column="AUTHORID"
name="authorid"
type="string"
>
<generator class="native" />
</id>
<property
column="AGE"
length="11"
name="age"
not-null="false"
type="integer"
/>
<property
column="NAME"
length="100"
name="name"
not-null="false"
type="string"
/>
<property
column="EMAIL"
length="150"
name="email"
not-null="false"
type="string"
/>
</class>
:
Session s=HibernateUtil.currentSession();
Transaction tx=s.beginTransaction();
Topic t=new Topic();
t.setName("Yashnoo");
t.setTitle("Hi all !1");
Object id=s.save(t);
log.info("Get id: "+id);
tx.commit();
HibernateUtil.closeSession();
NOne:
MySQL 4.0.1:
13:32:17,004 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
Hibernate: insert into pagertest (TIME, TITLE, NAME) values (?, ?, ?)
13:32:17,284 INFO Test:35 - Get id: 104
INFO
Hi everyone:
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 :(
My topic javabean is:
Code:
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("lyo@hibernate.com.cn");
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!