eg.hbm.xml.steve
Code:
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="eg.RoleVO" table="EXM_ROLE">
<id name="id" type="java.lang.Long" unsaved-value="0">
<column name="ID" sql-type="INT(10)"/>
<generator class="identity"/>
</id>
<property name="name">
<column name="NAME" not-null="true" sql-type="VARCHAR(40)"/>
</property>
<property name="creationDate" type="timestamp" column="CREATIONDATE"/>
<property name="modificationDate" type="timestamp" column="MODIFICATIONDATE"/>
<property type="java.lang.Long" name="creationUid">
<column name="CREATION_USER_ID" sql-type="INT(10)"/>
</property>
<property type="java.lang.Long" name="modificationUid">
<column name="MODIFICATION_USER_ID" sql-type="INT(10)"/>
</property>
<property name="description">
<column name="DESCRIPTION" sql-type="VARCHAR(255)"/>
</property>
<bag name="popedoms" lazy="true" table="EXM_ROLE_POPEDOM" cascade="all">
<key column="ROLE_ID"/>
<many-to-many class="cn.mBig.experience.vo.PopedomVO" column="POPEDOM_ID"/>
</bag>
<property type="java.lang.Byte" name="state">
<column name="STATE" sql-type="TINYINT(1)"/>
</property>
</class>
<class name="eg.PopedomVO" table="EXM_POPEDOM">
<id name="id" type="java.lang.Long" unsaved-value="0">
<column name="ID" sql-type="INT(10)"/>
<generator class="identity"/>
</id>
<property name="name">
<column name="NAME" not-null="true" sql-type="VARCHAR(40)"/>
</property>
<property name="description">
<column name="DESCRIPTION" sql-type="VARCHAR(255)"/>
</property>
</class>
</hibernate-mapping>
my old eg.hbm.xml
Code:
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="eg.RoleVO" table="EXM_ROLE">
<id name="id" type="java.lang.Long" unsaved-value="0">
<column name="ID" sql-type="INT(10)"/>
<generator class="identity"/>
</id>
<property name="name">
<column name="NAME" not-null="true" sql-type="VARCHAR(40)"/>
</property>
<property name="creationDate" type="timestamp" column="CREATIONDATE"/>
<property name="modificationDate" type="timestamp" column="MODIFICATIONDATE"/>
<property type="java.lang.Long" name="creationUid">
<column name="CREATION_USER_ID" sql-type="INT(10)"/>
</property>
<property type="java.lang.Long" name="modificationUid">
<column name="MODIFICATION_USER_ID" sql-type="INT(10)"/>
</property>
<property name="description">
<column name="DESCRIPTION" sql-type="VARCHAR(255)"/>
</property>
<bag name="popedoms" lazy="true" table="EXM_ROLE_POPEDOM" cascade="all">
<key>
<column name="ROLE_ID" sql-type="INT(10)"/>
</key>
<many-to-many class="eg.PopedomVO" insert="true" update="true">
<column name="POPEDOM_ID" sql-type="INT(10)"/>
</many-to-many>
</bag>
<property type="java.lang.Byte" name="state">
<column name="STATE" sql-type="TINYINT(1)"/>
</property>
</class>
<class name="eg.PopedomVO" table="EXM_POPEDOM">
<id name="id" type="java.lang.Long" unsaved-value="0">
<column name="ID" sql-type="INT(10)"/>
<generator class="identity"/>
</id>
<property name="name">
<column name="NAME" not-null="true" sql-type="VARCHAR(40)"/>
</property>
<property name="description">
<column name="DESCRIPTION" sql-type="VARCHAR(255)"/>
</property>
</class>
</hibernate-mapping>
Code:
package eg;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
import net.sf.hibernate.HibernateException;
import java.util.*;
public class Test {
public Test() {
}
public static void main(String[] args) {
Test t = new Test();
try {
t.createTables();
t.init();
t.createSomePopedom();
t.popedomPKs = new Long[3];
t.popedomPKs[0] = new Long(1);
t.popedomPKs[1] = new Long(3);
t.popedomPKs[2] = new Long(5);
t.insert();
t.dis();
} catch (Exception e) {
e.printStackTrace();
}
}
private void createTables() {
try {
cfg = new Configuration().configure();
SchemaExport dbExport = new SchemaExport(cfg);
dbExport.setOutputFile("eg.sql");
dbExport.create(true, true);
System.err.println("It is ok !");
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}
private void init() throws Exception {
s = cfg.buildSessionFactory().openSession();
}
private void dis() throws Exception {
s.close();
}
private void createSomePopedom() throws Exception {
PopedomVO p = new PopedomVO();
p.setName("11111");
s.save(p);
p = new PopedomVO();
p.setName("22222");
s.save(p);
p = new PopedomVO();
p.setName("33333");
s.save(p);
p = new PopedomVO();
p.setName("44444");
s.save(p);
p = new PopedomVO();
p.setName("55555");
s.save(p);
p = new PopedomVO();
p.setName("66666");
s.save(p);
s.flush();
}
private Session s;
private Long[] popedomPKs;
private Configuration cfg;
private void insert() throws Exception {
RoleVO role = new RoleVO();
role.setName("test");
Date now = new Date();
role.setCreationDate(now);
role.setModificationDate(now);
System.err.println("====================");
List tmp = new ArrayList();
if (null != popedomPKs) {
for (int i = 0; i < popedomPKs.length; i++) {
System.err.println(popedomPKs[i]);
PopedomVO popdom = selectByPK(popedomPKs[i]);
tmp.add(popdom);
}
role.setPopedoms(tmp);
}
for (java.util.Iterator i = tmp.iterator(); i.hasNext(); ) {
System.err.println( ( (PopedomVO) i.next()).getName());
}
s.saveOrUpdate(role);
s.flush();
}
private PopedomVO selectByPK(Long id) throws
Exception {
PopedomVO vo = null;
vo = new PopedomVO();
s.load(vo, id);
return vo;
}
}
Code:
package eg;
/**
* <p>Title: mBig Framework</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003 mBig.CN</p>
* <p>Company: mBig.CN</p>
* @author macula
* @version 1.0
*/
import java.util.*;
public class RoleVO {
private String name;
private String description;
private Date creationDate;
private Date modificationDate;
private List popedoms;
private Long id;
private Long creationUid;
private Long modificationUid;
private Byte state;
public RoleVO() {
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public java.util.Date getCreationDate() {
return creationDate;
}
public void setCreationDate(java.util.Date creationDate) {
this.creationDate = creationDate;
}
public java.util.Date getModificationDate() {
return modificationDate;
}
public void setModificationDate(java.util.Date modificationDate) {
this.modificationDate = modificationDate;
}
public java.util.List getPopedoms() {
return popedoms;
}
public void setPopedoms(java.util.List popedoms) {
this.popedoms = popedoms;
}
public void setId(Long id) {
this.id = id;
}
public Long getCreationUid() {
return creationUid;
}
public Long getModificationUid() {
return modificationUid;
}
public void setCreationUid(Long creationUid) {
this.creationUid = creationUid;
}
public void setModificationUid(Long modificationUid) {
this.modificationUid = modificationUid;
}
public Byte getState() {
return state;
}
public void setState(Byte state) {
this.state = state;
}
}
Code:
package eg;
/**
* <p>Title: mBig Framework</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003 mBig.CN</p>
* <p>Company: mBig.CN</p>
* @author macula
* @version 1.0
*/
import java.util.*;
public class PopedomVO {
private String name;
private String description;
private Long id;
private java.util.List roles;
public PopedomVO() {
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setId(Long id) {
this.id = id;
}
public java.util.List getRoles() {
return roles;
}
public void setRoles(java.util.List roles) {
this.roles = roles;
}
}
report
Code:
Hibernate: insert into EXM_POPEDOM (NAME, DESCRIPTION) values (?, ?)
Hibernate: SELECT LAST_INSERT_ID()
Hibernate: insert into EXM_POPEDOM (NAME, DESCRIPTION) values (?, ?)
Hibernate: SELECT LAST_INSERT_ID()
Hibernate: insert into EXM_POPEDOM (NAME, DESCRIPTION) values (?, ?)
Hibernate: SELECT LAST_INSERT_ID()
Hibernate: insert into EXM_POPEDOM (NAME, DESCRIPTION) values (?, ?)
Hibernate: SELECT LAST_INSERT_ID()
Hibernate: insert into EXM_POPEDOM (NAME, DESCRIPTION) values (?, ?)
Hibernate: SELECT LAST_INSERT_ID()
Hibernate: insert into EXM_POPEDOM (NAME, DESCRIPTION) values (?, ?)
Hibernate: SELECT LAST_INSERT_ID()
====================
1
3
5
11111
33333
55555
Hibernate: insert into EXM_ROLE (NAME, CREATIONDATE, MODIFICATIONDATE, CREATION_USER_ID, MODIFICATION_USER_ID, DESCRIPTION, STATE) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: SELECT LAST_INSERT_ID()