persist方法在尝试持久化新Object时给出PersistentObjectException

时间:2016-02-04 03:29:13

标签: java hibernate persist

在我的下面的代码中,我试图坚持新创建的Employee实例,它正在抛出persistantobject异常:

public class abc
{
   public static void main(String as[])
   {
       Configuration cfg= new Configuration();System.out.println("--------------got cfg object-----------");
       cfg = cfg.configure("hibernate.cfg.xml");System.out.println("--------------hbm loaded into cfg-----------");
       SessionFactory sf= cfg.buildSessionFactory();
       Employee item3 = new Employee();
       item3.setEid(420);
       Session session = sf.openSession(); 
       Employee item = (Employee) session.get(Employee.class, new Integer(12));
      // item.setFname("myaim1"); 
      // session.save(item);

     // Integer a= (Integer)session.save(item3);
       //session.persist(item3);
       Transaction tx = session.beginTransaction();
       session.persist(item3);
        tx.commit(); 
       session.close(); // end of first session, item is detached  

   }}

例外是:

-------------hbm loaded into cfg-----------
Hibernate: select employee0_.eid as eid0_0_, employee0_.first_name as first2_0_0_, employee0_.last_name as last3_0_0_, employee0_.email as email0_0_ from EMPLOYEE3 employee0_ where employee0_.eid=?
Exception in thread "main" org.hibernate.PersistentObjectException: detached entity passed to persist: Employee
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:79)
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
    at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596)
    at abc.main(abc.java:23)

我正在使用hibernate核心版本4.3。 什么出错了?

2 个答案:

答案 0 :(得分:0)

在这里使用srcset有什么特别的原因吗? 你为什么不使用<mvc:annotation-driven /> <mvc:default-servlet-handler /> <mvc:resources mapping="/resources/**" location="/resources/" /> <context:component-scan base-package="com.tridenthyundai.ains" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="/WEB-INF/messages" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean>

这样的事情:

persist()

答案 1 :(得分:0)

错误很可能是在 using (var writeStream = await newZipFile.OpenAsync(FileAccessMode.ReadWrite)) { using (var outputStream = writeStream.GetOutputStreamAt(0)) { using (var dataWriter = new DataWriter(outputStream)) { using (Stream input = webResponse.GetResponseStream()) { long totalSize = 0; int read; uint zeroUint = Convert.ToUInt32(0); uint readUint; while ((read = input.Read(buffer, 0, buffer.Length)) >0) { // totalSize += read; totalSize = totalSize + read; //pb2.Value = totalSize * 100 / sizeFit; await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { //Declaration of variables // load.progresschanged(totalSize * 100 / sizeFit); pb2.Value = totalSize * 100 / sizeFit; }); readUint = Convert.ToUInt32(read); IBuffer ibuffer = buffer.AsBuffer(); dataWriter.WriteBuffer(ibuffer, zeroUint, readUint); } await dataWriter.StoreAsync(); await outputStream.FlushAsync(); dataWriter.DetachStream(); } } } } 行。我想你必须使用item3.setEid(420);@Id一起使用`SEQUENCE / IDENTITY / TABLE / AUTO&#39;。如果是这样,您可以将此行注释为try。

如果您继续遇到同样的问题,发布@GeneratedValue类将有助于进一步调试它。