新闻
@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 :(得分:0)
事务管理器不会为手动创建的实体管理器启动事务。它只处理容器/弹簧管理的实体管理器。如果没有交易,您无法删除。
JpaTemplate
的javadoc建议您使用JPA样式的数据访问。所以使用
@PersistenceContext
private EntityManager entityManager;
(查看spring文档了解启用它所需的内容)。
如果您真的想要使用JpaTemplate
,那么只使用它,而不要使用基础工厂。