论坛首页 Java企业应用论坛

一个one to many 的数据插入问题

浏览 7338 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-01-09  
TemplateItem.hbm.xml如下
---------------------
<hibernate-mapping>
  <class name="com.jeyo.uprs.tng.TemplateItem" table="template_item">
    <id name="itemId" type="long" column="item_id" unsaved-value="0">
    	<generator class="sequence">
           <param name="sequence">template_item_seq</param>
        </generator>
    </id>
    <property name="templateId" type="long" column="template_id"/>
    <property name="typeId" type="long" column="type_id"/>
    <property name="itemName" column="item_name"  not-null="true" length="32"/>
    <property name="itemDesc" column="item_desc"  length="64"/>
    
    <many-to-one name="template" column="template_id" /> 

  </class>
</hibernate-mapping>

Template.hbm.xml
<hibernate-mapping>
  <class name="com.jeyo.uprs.tng.TemplateData" table="template">
    <id name="templateId" type="long" column="template_id" unsaved-value="0">
    	<generator class="sequence">
           <param name="sequence">template_seq</param>
        </generator>
    </id>
    <property name="templateName" column="template_name"  not-null="true" length="32"/>
    <property name="templateType" column="template_type"  length="32"/>
    <property name="templateDesc" column="template_desc"  length="64"/>
    
    <set name="itemSet" lazy="false" cascade="all" order-by="item_id ASC">
      <key column="template_id"/>
      <one-to-many class="com.jeyo.uprs.tng.TemplateItem"/>
    </set>
  </class>
</hibernate-mapping>


一个template 对应多个templateItem ,template_item 中的template_id是外键,使用如下的Java代码进行插入时
public void testSaveTemplate(); {
		TemplateData template = new TemplateData();;
		template.setTemplateName("template1");;
		template.setTemplateType("HOST");;
		template.setTemplateDesc("template desc");;
		TemplateItem item=new TemplateItem();;
		item.setItemName("item1");;
		item.setItemDesc("item11");;
		item.setTypeId(1);;
		template.addItem(item);;//mark 001
		
		try {
..			
session.saveOrUpdate(template);;
//base.saveTemplate(template);;
			System.out.println("...." + template.getTemplateId(););;
		} catch (Exception ex); {
			ex.printStackTrace();;
		}
	}
}


抛出如下错误:
Java.sql.SQLException: ORA-01006: 赋值变量不存在

假如java代码中去掉mark001所在的一行不会又异常抛出,也就是说插入template 没有问题,插入template_item时出错,但template_item中只有template_id ,item_name,item_id 为not null, 并且其他都已经设定,item_id 有sequence  ,问题时template_id 是否可以在template插入完成以后取的,因为在mark001所在行的代码注释掉后,可以输出template_id ,在插入的时候不会自动赋template_id?
   发表时间:2004-01-09  
1 双向的many-to-one,one-to-many关联设置&lt;set inverse="true"...

2 你只有template.addItem(item);,却没有item.setTemplate(template);
0 请登录后投票
   发表时间:2004-01-12  
&lt;set inverse="true"... 还是又同样的问题存在。
0 请登录后投票
   发表时间:2004-01-12  
没道理
TemplateData template = new TemplateData();; 
...
TemplateItem item=new TemplateItem();; 
...
template.getItemSet();.add(item);;
item.setTemplate(template);;
...
session.save(template);;
0 请登录后投票
   发表时间:2004-01-12  
我改了,问题依然存在,我只能自己自己手动插入相关数据
0 请登录后投票
   发表时间:2004-01-12  
:( 实在没道理,会不会是你的实体类有什么问题?如能给出就更好了。
0 请登录后投票
   发表时间:2004-01-13  
非常感谢yehs220得热心回答。下面我贴出Java代码.
TemplateData.java
public class TemplateData {
	private long templateId;
	private String templateName;
	private String templateType;
	private String templateDesc;
	private Set itemSet = new HashSet();;
	/**
	 * @return
	 */
	public Set getItemSet(); {
		return itemSet;
	}

	public List getItemList(); {
		return new ArrayList(itemSet);;
	}
	/**
	 * @return
	 */
	public String getTemplateDesc(); {
		return templateDesc;
	}

	/**
	 * @return
	 */
	public long getTemplateId(); {
		return templateId;
	}

	/**
	 * @return
	 */
	public String getTemplateName(); {
		return templateName;
	}

	/**
	 * @return
	 */
	public String getTemplateType(); {
		return templateType;
	}

	/**
	 * @param list
	 */
	public void setItemSet(Set set); {
		itemSet = set;
	}

	/**
	 * @param list
	 */
	public void addItem(TemplateItem item); {
		itemSet.add(item);;
	}

	/**
	 * @param string
	 */
	public void setTemplateDesc(String string); {
		templateDesc = string;
	}

	/**
	 * @param l
	 */
	public void setTemplateId(long l); {
		templateId = l;
	}

	/**
	 * @param string
	 */
	public void setTemplateName(String string); {
		templateName = string;
	}

	/**
	 * @param string
	 */
	public void setTemplateType(String string); {
		templateType = string;
	}

	public String toString(); {
		StringBuffer sb = new StringBuffer("....template:[");;
		sb.append("\r\n....templateId:");.append(templateId);;
		sb.append("\r\n....templateName:");.append(templateName);;
		sb.append("\r\n....item numbers:");.append(itemSet.size(););.append("\r\n");;
		Iterator it = itemSet.iterator();;
		while (it.hasNext();); {
			sb.append(it.next(););;
		}
		sb.append("]\r\n");;
		return sb.toString();;
	}

}

TemplateItem.java
public class TemplateItem {
	private long templateId;
	private long itemId;
	private long typeId;
	private String itemName;
	private String itemDesc;
	private TemplateData template;
	/**
	 * @return
	 */
	public String getItemDesc(); {
		return itemDesc;
	}

	/**
	 * @return
	 */
	public long getItemId(); {
		return itemId;
	}

	/**
	 * @return
	 */
	public String getItemName(); {
		return itemName;
	}

	/**
	 * @return
	 */
	public long getTemplateId(); {
		return templateId;
	}

	/**
	 * @param string
	 */
	public void setItemDesc(String string); {
		itemDesc = string;
	}

	/**
	 * @param l
	 */
	public void setItemId(long l); {
		itemId = l;
	}

	/**
	 * @param string
	 */
	public void setItemName(String string); {
		itemName = string;
	}

	/**
	 * @param l
	 */
	public void setTemplateId(long l); {
		templateId = l;
	}
	
	/**
	 * @return
	 */
	public TemplateData getTemplate(); {
		return template;
	}

	/**
	 * @param data
	 */
	public void setTemplate(TemplateData data); {
		template = data;
	}

	public String toString(); {
		StringBuffer sb=new StringBuffer("\r\n........templateItem:[");;
		sb.append("\r\n........itemid:");.append(itemId);;
		sb.append("\r\n........typeId:");.append(typeId);;
		sb.append("\r\n........itemName:");.append(itemName);;
		sb.append("\r\n]\r\n");;
		return sb.toString();;
	}

	/**
	 * @return
	 */
	public long getTypeId(); {
		return typeId;
	}

	/**
	 * @param l
	 */
	public void setTypeId(long l); {
		typeId = l;
	}

}


saveTemplate method
	public void saveTemplate(TemplateData template); throws UPRSException {
		Session session = null;
		Long id;
		Transaction tx = null;
		try {
			session = ss.openSession();;
			tx = session.beginTransaction();;
			session.saveOrUpdate(template);;
			//session.save(template);;
			tx.commit();;
			session.flush();;
		} catch (HibernateException ex); {
			//log.error("....HibernateException :", ex);;
			throw new UPRSException(ex);;
		} finally {
			ss.closeSession(session);;
		}
	}


测试代码
public void testSaveTemplate(); {
		TemplateData template = new TemplateData();;
		template.setTemplateName("template1");;
		template.setTemplateType("HOST");;
		template.setTemplateDesc("template desc");;
		TemplateItem item = new TemplateItem();;
		item.setItemName("item1");;
		item.setItemDesc("item11");;
		item.setTypeId(1);;
		template.addItem(item);;
		item = new TemplateItem();;
		item.setItemName("item2");;
		item.setItemDesc("item22");;
		item.setTypeId(2);;
		//itemList.add(item);;
		template.addItem(item);;
				try {
			BaseManager base = new BaseManager();;//调用上面得方法
			base.saveTemplate(templateitemList);;
			
			System.out.println("...." + template.getTemplateId(););;
		} catch (Exception ex); {
			ex.printStackTrace();;
		}
	}
0 请登录后投票
   发表时间:2004-01-13  
你还是没加上
item.setTemplate(template);
0 请登录后投票
   发表时间:2004-01-13  
重新贴测试代码:
public void testSaveTemplate(); {
		TemplateData template = new TemplateData();;
		template.setTemplateName("template1");;
		template.setTemplateType("HOST");;
		template.setTemplateDesc("template desc");;
		TemplateItem item = new TemplateItem();;
		item.setItemName("item1");;
		item.setItemDesc("item11");;
		item.setTypeId(1);;
		item.setTemplate(template);;
		template.addItem(item);;
		
		//Set itemList = new HashSet();;
		//itemList.add(item);;
		
		item = new TemplateItem();;
		item.setItemName("item2");;
		item.setItemDesc("item22");;
		item.setTypeId(2);;
		item.setTemplate(template);;
		template.addItem(item);;
		//itemList.add(item);;
		
		try {
			BaseManager base = new BaseManager();;
			base.saveTemplate(template);;
			//base.saveTemplateItem(itemList);;
			System.out.println("...." + template.getTemplateId(););;
		} catch (Exception ex); {
			ex.printStackTrace();;
		}
	}


插入实现代码
public void saveTemplate(TemplateData template); throws UPRSException {
		Session session = null;
		Long id;
		//Transaction tx = null;
		try {
			session = ss.openSession();;
			//tx = session.beginTransaction();;
			session.saveOrUpdate(template);;
			//session.save(template);;
			session.flush();;
			session.connection();.commit();;
			//tx.commit();;

		} catch (HibernateException ex); {
			//log.error("....HibernateException :", ex);;
			throw new UPRSException(ex);;
		} catch (java.sql.SQLException ex); {
			//log.error("....HibernateException :", ex);;
			throw new UPRSException(ex);;
		} finally {
			ss.closeSession(session);;
		}
	}


Exception:
16:44:12,687 ERROR JDBCExceptionReporter:37 - Could not synchronize database state with session
java.sql.SQLException: ORA-01006: 赋值变量不存在

	at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java);
	at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java);
	at oracle.jdbc.ttc7.Oall7.receive(Oall7.java);
	at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java);
	at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteFetch(TTC7Protocol.java);
	at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java);
	at oracle.jdbc.driver.OracleStatement.doExecuteOther(OracleStatement.java);
	at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java);
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeBatch(DelegatingPreparedStatement.java:205);
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeBatch(DelegatingPreparedStatement.java:205);
	at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:50);
	at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:96);
	at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2095);
	at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2064);
	at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2008);
	at com.jeyo.uprs.tng.BaseManager.saveTemplate(BaseManager.java:171);
	at com.jeyo.uprs.tng.TNGManagerTest.testSaveTemplate(TNGManagerTest.java:160);
	at com.jeyo.uprs.tng.TNGManagerTest.main(TNGManagerTest.java:32);

0 请登录后投票
   发表时间:2004-01-13  
我试过你的代码,没问题呀。
映射文件如下,就比你的多了schema="hibernate"
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping schema="hibernate"> 
  <class name="com.jeyo.uprs.tng.TemplateItem" table="template_item"> 
    <id name="itemId" type="long" column="item_id" unsaved-value="0"> 
       <generator class="sequence"> 
           <param name="sequence">template_item_seq</param> 
        </generator> 
    </id> 

    <property name="typeId" type="long" column="type_id"/> 
    <property name="itemName" column="item_name"  not-null="true" length="32"/> 
    <property name="itemDesc" column="item_desc"  length="64"/> 
    
    <many-to-one name="template" column="template_id" /> 

  </class> 
  
  <class name="com.jeyo.uprs.tng.TemplateData" table="template"> 
    <id name="templateId" type="long" column="template_id" unsaved-value="0"> 
       <generator class="sequence"> 
           <param name="sequence">template_seq</param> 
        </generator> 
    </id> 
    <property name="templateName" column="template_name"  not-null="true" length="32"/> 
    <property name="templateType" column="template_type"  length="32"/> 
    <property name="templateDesc" column="template_desc"  length="64"/> 
    
    <set name="itemSet" lazy="false" cascade="all" order-by="item_id ASC" inverse="true"> 
      <key column="template_id"/> 
      <one-to-many class="com.jeyo.uprs.tng.TemplateItem"/> 
    </set> 
  </class>
</hibernate-mapping>
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics