喜用冬眠來管理我的數據庫交易的所有IM,查找和方法的createQuery工作良好,但是當我做了合併沒有任何反應,並且不會顯示任何錯誤。合併的實體管理器不工作
我正嘗試從一個Repositorio刪除一些Categorias。 我正確地更改屬性附加傷害
private List<ICategoria> categoriaList;
,但在合併I'm有問題
所有的值這是我的實體Repositorio,一個Repositorio有許多類別。
@Entity
public class Repositorio extends EntityBean implements IRepositorio {
private static final long serialVersionUID = 1L;
private String nome;
@OneToMany(targetEntity=Categoria.class, cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="repositorio")
@Fetch(FetchMode.SELECT)
private List<ICategoria> categoriaList;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public void incluirCategoria(ICategoria categoria) {
}
public void excluirCategoria(ICategoria categoria) {
}
public List<ICategoria> getCategoriaList() {
if (categoriaList == null) {
categoriaList = new ArrayList<ICategoria>();
}
return categoriaList;
}
@Override
public String toString() {
return this.nome;
}
}
這是我的實體Categoria
@Entity
public class Categoria extends EntityBean implements ICategoria {
private static final long serialVersionUID = 1L;
@OneToMany(targetEntity=Documento.class, cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="categoria")
private List<IDocumento> documentoList;
@ManyToOne(targetEntity=Repositorio.class)
private IRepositorio repositorio;
private String nome;
@ManyToOne(targetEntity=Categoria.class)
private ICategoria categoria;//pai
@OneToMany(targetEntity=Categoria.class, cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="categoria")
private List<ICategoria> categoriaList;
public IRepositorio getRepositorio() {
return repositorio;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public ICategoria getCategoria() {
return categoria;
}
public List<ICategoria> getCategoriaList() {
if (categoriaList == null) {
categoriaList = new ArrayList<ICategoria>();
}
return categoriaList;
}
@Override
public String toString(){
return this.nome;
}
public List<IDocumento> getDocumentoList() {
return documentoList;
}
public void setCategoria(ICategoria categoria) {
this.categoria = categoria;
}
public void setCategoriaList(List<ICategoria> categoriaList) {
this.categoriaList = categoriaList;
}
@Override
public void setRepositorio(IRepositorio repositorio) {
this.repositorio = repositorio;
}
}
這是我更新方法
public T update(T entity) {
beginTransaction();
T mergedEntity = entityManager.merge(entity);
entityManager.flush();
commit();
return mergedEntity;
}
更新
這裏就是我所說的DAO讓我的元素方法excluirCategoria是我在哪裏刪除categor IES處理
public class RepositorioFacade implements IRepositorioFacade {
private IRepositorioDAO repositorioDAO;
public List<IRepositorio> listar() {
return getRepositorioDAO().list();
}
public IRepositorio criar() {
return EntityBeanFactory.getRepositorio();
}
public void excluir(IRepositorio repositorio) {
getRepositorioDAO().delete(repositorio);
}
public void gravar(IRepositorio repositorio) {
getRepositorioDAO().save(repositorio);
}
public void incluirCategoria(IRepositorio repositorio, ICategoria categoria) {
categoria.setRepositorio(repositorio);
repositorio.getCategoriaList().add(categoria);
}
public void excluirCategoria(IRepositorio repositorio, ICategoria categoria) {
while (categoria.getCategoriaList().size() > 0) {
excluirCategoria(repositorio, categoria.getCategoriaList().get(0));
}
if (categoria.getCategoria() != null) {
ICategoria catPai = categoria.getCategoria();
if (catPai.getCategoriaList().indexOf(categoria) >= 0)
catPai.getCategoriaList().remove(categoria);
}
if (repositorio.getCategoriaList().indexOf(categoria) >= 0) {
repositorio.getCategoriaList().remove(categoria);
}
}
private IRepositorioDAO getRepositorioDAO(){
if (repositorioDAO == null){
repositorioDAO = DAOFactory.getRepositorioDAO();
}
return repositorioDAO;
}
@Override
public void incluirSubCategoria(ICategoria categoriaPai, ICategoria categoriaFilho) {//(categoria selecionada e categoria nova (do pooup))
categoriaFilho.setCategoria(categoriaPai);//
categoriaPai.getCategoriaList().add(categoriaFilho); //esto es para que el papa quede acctualizado pero para hibernate no se precisa
}
public IRepositorio find(int id){
return DAOFactory.getRepositorioDAO().find(id);
}
}
這是我的DAO哪裏都是事務性的方法列表方法fecth元素庫。
public class GenericDAO<T extends IEntityBean> {
private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("WSysGED");
protected EntityManager entityManager = emf.createEntityManager();
protected Class<?> entityClass;
private void beginTransaction() {
entityManager.getTransaction().begin();
}
private void commit() {
entityManager.getTransaction().commit();
}
public GenericDAO(Class<?> entityClass) {
this.entityClass = entityClass;
}
public void save(T entity) {
if (entity.getId() == 0) {
beginTransaction();
entityManager.persist(entity);
commit();
} else {
update(entity);
}
}
public void delete(T entity) {
beginTransaction();
entityManager.remove(entity);
commit();
}
public T update(T entity) {
System.out.println("update");
beginTransaction();
T mergedEntity = entityManager.merge(entity);
entityManager.flush();
entityManager.clear();
commit();
return mergedEntity;
}
@SuppressWarnings({"unchecked"})
public T find(int entityID) {
return (T) entityManager.find(entityClass, entityID);
}
@SuppressWarnings({"unchecked"})
public T findReferenceOnly(int entityID) {
return (T) entityManager.getReference(entityClass, entityID);
}
// Using the unchecked because JPA does not have a
// em.getCriteriaBuilder().createQuery()<T> method
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<T> list() {
CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return entityManager.createQuery(cq).getResultList();
}
// Using the unchecked because JPA does not have a
// query.getSingleResult()<T> method
@SuppressWarnings("unchecked")
protected T findOneResult(String namedQuery, Map<String, Object> parameters) {
T result = null;
try {
Query query = entityManager.createNamedQuery(namedQuery);
// Method that will populate parameters if they are passed not null and empty
if (parameters != null && !parameters.isEmpty()) {
populateQueryParameters(query, parameters);
}
result = (T) query.getSingleResult();
} catch (NoResultException e) {
System.out.println("No result found for named query: " + namedQuery);
} catch (Exception e) {
System.out.println("Error while running query: " + e.getMessage());
e.printStackTrace();
}
return result;
}
private void populateQueryParameters(Query query, Map<String, Object> parameters) {
for (Entry<String, Object> entry : parameters.entrySet()) {
query.setParameter(entry.getKey(), entry.getValue());
}
}
}
更新2
這是我得到的所有的類別從程序存儲庫中adavance
public TreeNode getRoot() {
if (root == null) {
List<ICategoria> listaCategorias = repositorio.getCategoriaList();
root = new DefaultTreeNode(null);
TreeNode noInicial = new DefaultTreeNode("Categorias", root);
for (ICategoria categoria : listaCategorias) {
if (categoria.getCategoria() == null) {
TreeNode node = new DefaultTreeNode(categoria, noInicial);
incluirNoFilho(categoria, node);
}
}
}
return root;
}
感謝您的時間和JPA 2.0文檔回答
打開的問題'在休眠狀態下顯示sql'以查看在提交事務時執行哪種類型的sql(如果有的話)。而且,在提交之前,您不必刷新實體管理器(如果它是JPA實體管理器),因爲無論如何,提交都會這樣做。你能告訴我們交易管理方法嗎? – Antoniossss 2014-09-22 17:51:08
我激活了sql日誌,但在那個操作中沒有任何反應。交易管理方法意味着什麼? @Antoniossss – 2014-09-22 17:58:49
我的意思是你的'beginTransaction();'和'commit();'沒有任何事情發生,因爲你的改變沒有意義,可能是因爲我在我的答案中指出的事實。顯示獲取類別的代碼以及從中刪除文檔 – Antoniossss 2014-09-22 18:00:54