2011-08-21 70 views
0

新聞Spring JPATemplate DAO。從表中刪除項目

@Entity 
@Table(name = "NEWS") 

@NamedQueries({ 
    @NamedQuery(name = "News.findAll", query = "SELECT n FROM News n"), 
    @NamedQuery(name = "News.delete", query = "DELETE FROM News n WHERE n.newsId in(:ids)") 
}) 

@GenericGenerator(name = "test-increment-strategy", strategy = "increment") 
public class News implements Serializable { 

    private static final long serialVersionUID = 3330980835510468207L; 
    private Integer newsId; 
    private String title; 
    private String brief; 
    private String content; 
    private Date created; 
    private String dateCreatedString; 

    public News() { 
    } 

    @Id 
    @Column(name = "NEWS_ID") 
    // @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "news_seq") 
    // @SequenceGenerator(name = "news_seq", sequenceName = "NEWS_SEQ") 
    @GeneratedValue(generator = "test-increment-strategy") 
    public Integer getNewsId() { 
     return newsId; 
    } 

DAO

@Override 
@Transactional(readOnly = false) 
public void removeNews(List<Integer> listOfIdNewsForDeleting) throws DAOException { 
    EntityManager entityManager = getJpaTemplate().getEntityManagerFactory().createEntityManager(); 

    Query query = entityManager.createNamedQuery("News.delete"); 
    query.setParameter("ids", listOfIdNewsForDeleting); 
    int deleted = query.executeUpdate(); 
} 

它是不成功的嘗試刪除的消息。

TransactionManager不給我交易新的EntityManager我打電話。

但我不能使用查詢投擲JpaTemplate。有一些想法?

回答

0

事務管理器不會爲手動創建的實體管理器啓動事務。它只處理容器/彈簧管理的實體管理器。沒有交易,你不能刪除。

JpaTemplate的javadocs建議您使用JPA樣式的數據訪問。因此,使用

@PersistenceContext 
private EntityManager entityManager; 

(請檢查您需要啓用它的spring文檔)。

如果你真的想要huops JpaTemplate然後只使用它,並沒有得到底層的工廠。