-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: It is my test code...
PostPosted: Tue Sep 16, 2003 10:21 am 
Newbie

Joined: Sat Sep 13, 2003 12:47 pm
Posts: 15
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()


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 16, 2003 10:25 am 
Newbie

Joined: Sat Sep 13, 2003 12:47 pm
Posts: 15
my hibernate.cfg.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 2.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

    <session-factory
        name="java:comp/env/hibernate/SessionFactory">
      <property name="cglib.use_reflection_optimizer">false</property>

        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="use_outer_join">true</property>

        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/exm?useUnicode=true&amp;characterEncoding=GBK</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <property name="connection.pool_size">1</property>
        <property name="statement_cache.size">25</property>

        <mapping resource="eg.hbm.xml"/>

    </session-factory>

</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 16, 2003 10:28 am 
Newbie

Joined: Sat Sep 13, 2003 12:47 pm
Posts: 15
Sorry,,, i changed Test's method in my testing...

Code:
  private PopedomVO selectByPK(Long id) throws
      Exception {
    return (PopedomVO) s.load(PopedomVO.class, id);
  }


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 16, 2003 10:36 am 
Newbie

Joined: Sat Sep 13, 2003 12:47 pm
Posts: 15
link table EXM_ROLE_POPEDON not be insert

why?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 16, 2003 10:43 am 
Newbie

Joined: Sat Sep 13, 2003 12:47 pm
Posts: 15
sorry, drop the post please

a foolish mistake ... i use a early version 's eg.hbm.xml


thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.