detached entity passed to persist: 問題的解決

來源:互聯網
上載者:User
在結構中使用了OneToOne的表單映射。但是在儲存主,子表的時候,發生 detached entity passed to persist: 的錯誤。解決辦法:去掉主表中     import org.hibernate.annotations.GenericGenerator;import javax.persistence.GeneratedValue;@GenericGenerator(name = "generator", strategy = "uuid")@GeneratedValue(generator = "generator")這個注釋。原意是使用 hibernate uuid 來進行自動id的賦值。但是hibernate 3.3版本好像有問題。無法完成自動功能。只能使用如下代碼來進行id複製。
  1.   public boolean addUserInfo(AlcorTUser alcorUser) throws Exception{
  2.         String userID = UUID.randomUUID().toString();
  3.         alcorUser.setUserid(userID);
  4.         alcorUser.setPassword("password");
  5.         alcorUser.setLoginCount(0);
  6.         alcorUser.setLastLogin(new Date());
  7.         alcorUser.getAlcorTUserDetail().setUserid(userID);
  8.         /*Iterator<AlcorTUserDetail> userDetailIterator =  alcorUser.getAlcorTUserDetails().iterator();
  9.         logger.debug("含有"+alcorUser.getAlcorTUserDetails().toArray().length+"個使用者詳細資料");
  10.         while (userDetailIterator.hasNext())
  11.         {
  12.             userDetailIterator.next().setUserid(userID);
  13.             //this.alcorTUserDetailDAO.save(userDetailIterator.next()) ;
  14.         }*/
  15.         this.alcorTUserDAO.save(alcorUser);
  16.        
  17.         return true;
  18.     }

其中 alcorUser 是主表, alcorUserDetail是子表,是OneToOne的映射。

傳入的alcorUser對象是flex通過lcds傳入。已經在flex這端。把一個子表內容的對象放入到這個主表內容的對象中去了。所以在這裡只要對主表進行save,自動的會對子表也進行儲存。中間屏蔽的這端是處理OneToMany的方法。使用了Iterator。在POJO這端通過HashSet來定義。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.