-->
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.  [ 1 post ] 
Author Message
 Post subject: help me defect a one-to-many mapping problem
PostPosted: Mon Apr 12, 2004 2:09 am 
Newbie

Joined: Sun Apr 04, 2004 9:29 pm
Posts: 1
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:


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

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.