2012-07-17 84 views
1

我想從行數據庫中刪除特定的記錄。但是,當我嘗試執行此查詢:卡桑德拉昆德拉 - 刪除記錄按行鍵

Query query = em.createQuery(
      "DELETE FROM User u WHERE u.userId = :u"); 

query.setParameter("u", userID).executeUpdate(); 

我得到這個異常:「條件=不支載對行鍵查詢!」。

有沒有什麼解決方法,或者我錯過了什麼?

回答

2

你可以做一個變通方法是什麼:

查找使用: 用戶u = em.find(User.class,用戶id)

然後, em.delete(U);

+0

止跌」導致數據庫讀取:(...在PlayOrm中爲cassandra和JPA,而是用戶user = em.getReference(User.clas,userId)和em.remove(user);所以你不需要額外的數據庫讀取 – 2012-10-28 15:56:10

+0

em.getReference(User.clas,userId) 如何獲得對實際記錄的引用而不需要訪問數據庫甚至填充到cac中他你需要它。 無論如何,其他方式是通過執行CQL查詢。同樣在2.1以後,這樣的查詢應該可以工作。 -Vivek – 2012-10-29 02:35:02

+0

你可以看看hibernate的代碼或PlayOrm的,但基本上getReference只是在沒有觸摸db的情況下包裝Id,然後刪除解包併發送刪除id等於該id的行,所以不需要從數據庫讀取。有時候沒有必要把它放在緩存中,所以我不確定你爲什麼引用緩存。 – 2012-10-29 11:06:18

1

以執行一種可能的方法,例如刪除(在使用昆德拉,版本2.2此刻一個命令)時使用「原生查詢」,例如:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("cassandra_pu"); 

EntityManager em = emf.createEntityManager(); 

// "table" is the table name (case sensitive) you name your table in Cassandra 
String query = "delete from table where key = 'keyValue'"; 

// "TablePersistencyEntity" is the Kundera Persistency Entity (Class) for the "table" 
Query q = em.createNativeQuery(query, TablePersistencyEntity.class); 
q.getResultList(); 

em.close();