2012-07-10 109 views
1

即時通訊嘗試與JPA,JAX-RS和JAX-B構建谷歌應用程序引擎項目。我的POST和GET方法工作,但我的DELETE方法不會刪除數據。應用程序引擎JPA數據存儲刪除實體

資源

@DELETE 
@Path("card/{id}") 
public void deleteCardById (@PathParam ("id") Long id) { 
    Service.removeCard(id); 
} 

服務

public static void removeCard(Long id) { 
    EntityManager em = EMFService.get().createEntityManager(); 
    Card emp = findCard(id); 
    if (emp != null) { 
     em.remove(emp); 
    } 
    em.close(); 
} 

public static Card findCard(Long id) { 
    EntityManager em = EMFService.get().createEntityManager(); 
    Card card = em.find(Card.class, id); 
    em.close(); 
    return card; 
} 

實體

@XmlRootElement 
@Entity 
public class Card { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    Long id; 
    String begriff; 
    String tabu1; 
    String tabu2; 
    String tabu3; 
public Card(String begriff, String tabu1, String tabu2, String tabu3) { 
     super(); 
     Begriff = begriff; 
     Tabu1 = tabu1; 
     Tabu2 = tabu2; 
     Tabu3 = tabu3; 
    } 

    public Card() { 

    } 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getBegriff() { 
     return Begriff; 
    } 

    public void setBegriff(String begriff) { 
     Begriff = begriff; 
    } 

    public String getTabu1() { 
     return Tabu1; 
    } 

    public void setTabu1(String tabu1) { 
     Tabu1 = tabu1; 
    } 

    public String getTabu2() { 
     return Tabu2; 
    } 

    public void setTabu2(String tabu2) { 
     Tabu2 = tabu2; 
    } 

    public String getTabu3() { 
     return Tabu3; 
    } 

    public void setTabu3(String tabu3) { 
     Tabu3 = tabu3; 
    } 

    @Override 
    public String toString() { 
     return "Card [Begriff=" + Begriff + ", Tabu1=" + Tabu1 + ", Tabu2=" 
       + Tabu2 + ", Tabu3=" + Tabu3 + "]"; 
    } 

當我調試它給正確的對象爲刪除功能的應用程序。但它只是不會刪除數據...

回答

2

你的意思是你使用GAE JPA插件的v1,而且你不必爲你的刪除而放置一個事務(所以刪除被延遲到下一個交易......從來沒有發生過)?

顯然,你既可以把一個交易周圍的刪除,或者更好的是你用GAE JPA插件

0

好吧,我想我應該寫這樣的 *編輯V2的問題是findCard功能,我認爲是因爲EntityManager的secone實例。我chnaged它沒有使用這種方法,現在它的工作。

public static void removeCard(Long id) { 
     EntityManager em = EMFService.get().createEntityManager(); 
     EntityTransaction tx = em.getTransaction(); 
     try { 
      tx.begin(); 
      Card card = em.find(Card.class, id); 
      if (card != null) { 
       em.remove(card); 
      } 
      tx.commit(); 
     } finally { 
      if (tx.isActive()) { 
       tx.rollback(); 
      } 
      em.close(); 
     } 
    } 
+0

如果有什麼「不工作」(明確使用該插件的V1),然後做順理成章的事情是看在日誌中在發生什麼事。又名調試 – DataNucleus 2012-07-10 13:41:01

+0

他正在經歷這個方法,沒有任何問題,並且給remove方法提供了正確的對象,但是這個對象並沒有被移除 – AdrianoCelentano 2012-07-10 13:52:47

1

我也面臨類似的問題。 JPA刪除實際上刪除了數據存儲中的實體,但它並未從JPA Cache中刪除該實體。您的頁面實際上是使用JPA緩存結果列表來顯示的。

我用來解析問題是每次刪除後都要清除JPA Cache。

示例代碼將是這樣的:

EM.getTransaction().begin(); 

EM.remove(current_record); 

EM.getTransaction().commit(); 
EM.getEntityManagerFactory().getCache().evictAll(); 
相關問題