New data can't be stored by Hibernate.
Running result as following:
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into mocha_be_process_instance (pmgid, processid, id) values (?, ?, ?)
Hibernate: update mocha_be_process_log set activityid=?, processinstanceid=? where id=?
==1/0
1.Both SQL statment should be "insert",but why the 2nd is "update"?
2.there are nothing be stored in database(Oracle9i) when program has done.
======================
Code:
package com.mdcl.mocha.be.api.entity;
import java.util.HashSet;
import java.util.Set;
public class ProcessInstance {
private long id;
private String pmgId;
private String processId;
private Set logs;
public ProcessInstance(){
this.logs=new HashSet();
}
public long getId() {
return id;
}
public String getPmgId() {
return pmgId;
}
public String getProcessId() {
return processId;
}
public void setId(long l) {
id = l;
}
public void setPmgId(String string) {
pmgId = string;
}
public void setProcessId(String string) {
processId = string;
}
public Set getLogs() {
return logs;
}
public void setLogs(Set logs) {
this.logs = logs;
}
}
Code:
package com.mdcl.mocha.be.api.entity;
public class ProcessLog {
private long id;
private String activityId;
private ProcessInstance parent;
public String getActivityId() {
return activityId;
}
public long getId() {
return id;
}
public ProcessInstance getParent() {
return parent;
}
public void setActivityId(String string) {
activityId = string;
}
public void setId(long l) {
id = l;
}
public void setParent(ProcessInstance parent) {
this.parent = parent;
}
}
Code:
<hibernate-mapping>
<class name="com.mdcl.mocha.be.api.entity.ProcessInstance" table="mocha_be_process_instance">
<id name="id" column="id" type="long">
<generator class="native"/>
</id>
<property name="pmgId" column="pmgid" type="string" length="100"/>
<property name="processId" column="processid" type="string" length="20" not-null="true"/>
<set name="logs" lazy="true" inverse="true" cascade="all">
<key column="processinstanceid"/>
<one-to-many class="com.mdcl.mocha.be.api.entity.ProcessLog"/>
</set>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class name="com.mdcl.mocha.be.api.entity.ProcessLog" table="mocha_be_process_log">
<id name="id" column="id" type="long">
<generator class="native"/>
</id>
<property name="activityId" column="activityid" type="string" length="20" not-null="true"/>
<many-to-one name="parent"
class="com.mdcl.mocha.be.api.entity.ProcessInstance"
column="processinstanceid"/>
</class>
</hibernate-mapping>
Code:
package com.mdcl.mocha.util;
import com.mdcl.mocha.be.api.entity.ProcessInstance;
import com.mdcl.mocha.be.api.entity.ProcessLog;
import com.mdcl.mocha.exception.MochaException;
import net.sf.hibernate.Session;
//import net.sf.hibernate.cfg.Configuration;
//import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
public class MyTest {
public static void main(String[] args) {
Session s=null;
try {
s = DBManager.getSession();
s.getSessionFactory();
ProcessInstance pi = new ProcessInstance();
pi.setPmgId("fff");
pi.setProcessId("123");
ProcessLog pl = new ProcessLog();
pl.setActivityId("111");
pl.setParent(pi);
pi.getLogs().add(pl);
s.save(pi);
s.connection().commit();
s.flush();
System.out.println("=="+pl.getParent().getId()+"/"+pl.getId());
} catch (Exception e) {
e.printStackTrace();
// try {
// Configuration cfg=new Configuration().configure("/hibernate.be.xml");
// new SchemaExport(cfg).create(true, true);
// } catch (Exception e1) {
// e1.printStackTrace();
// }
}finally{
try {
DBManager.releaseSession(s);
} catch (MochaException e1) {
e1.printStackTrace();
}
}
}
}
Code: