当前位置: 代码迷 >> J2EE >> hibernate的org.hibernate.TransientObjectException错误如何办处理
  详细解决方案

hibernate的org.hibernate.TransientObjectException错误如何办处理

热度:115   发布时间:2016-04-22 02:59:51.0
hibernate的org.hibernate.TransientObjectException异常怎么办处理
下面是异常信息
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.fyx.test.Order
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:242)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:430)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:101)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:777)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1165)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at com.fyx.test.Test.main(Test.java:62)
下面是代码

package com.fyx.test;

import java.util.HashSet;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test
{
private static SessionFactory sessionFactory;
static
{
try
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args)
{
Session session = sessionFactory.openSession();
Transaction tx = null;
Customer customer = new Customer();
customer.setName("zhangsan");
customer.setOrders(new HashSet());



Order order1 = new Order();
order1.setOrderNumber("order1");



Order order2 = new Order();
order2.setOrderNumber("order2");


Order order3 = new Order();
order3.setOrderNumber("order3");


order1.setCustomer(customer);
order2.setCustomer(customer);
order3.setCustomer(customer);

customer.getOrders().add(order1);
customer.getOrders().add(order2);
customer.getOrders().add(order3);
try
{
tx = session.beginTransaction();
session.save(customer);
tx.commit();
}
catch(Exception ex)
{
if(null != tx)
{
tx.rollback();
}
ex.printStackTrace();
}
finally
{
session.close();
}
System.out.println("asdf");
}
}

------解决方案--------------------
Customer customer = new Customer();
customer.setName("zhangsan");
//customer.setOrders(new HashSet());//此处先不设置多端
session.save(customer);//先保存customer

Customer customer_find = session.findMethod();//session查找到刚插入的customer

Order order1 = new Order();
order1.setOrderNumber("order1");

Order order2 = new Order();
order2.setOrderNumber("order2");

Order order3 = new Order();
order3.setOrderNumber("order3");

//设置order对应的单端
order1.setCustomer(customer_find);
order2.setCustomer(customer_find);
order3.setCustomer(customer_find);
  相关解决方案