Java 无法刷新非托管对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20328740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:06:58  来源:igfitidea点击:

Can not refresh not managed object

javajpa

提问by Marwen Trabelsi

I've to refresh a list of Elevefrom the database to a Jtable. The Jtableit's self is bound to an observable list< Eleve >, so any add/delete to the list is automatically updated in the Jtable.

我必须刷新Eleve从数据库到 Jtable 的列表。在Jtable它的自我绑定到一个可观察到的list< Eleve >,所以任何添加/删除列表中的自动更新Jtable

My code explain it's self:

我的代码解释了它的自我:

EleveJpaController ejc = new EleveJpaController(emf);
java.util.Collection<Eleve> data = ejc.findEleveByNiveauClasseNomPrenom(niv, classe, nom, prenom);

for (Eleve entity : data) {
    entityManager.refresh(entity);
}

list.clear();
list.addAll(data);

List< Eleve > findEleveByNiveauClasseNomPrenom(a,v,x,n):

List< Eleve > findEleveByNiveauClasseNomPrenom(a,v,x,n):

 public List<Eleve> findEleveByNiveauClasseNomPrenom(String niv,String classe,String nom, String prenom)
    {
        EntityManager em = getEntityManager();
        Query qr= em.createQuery("SELECT e FROM Eleve e WHERE e.niv.niv like '%"+niv+"%' and e.class1.libelle like '%"+classe+"%' and e.nom like '%"+nom+"%' and e.prenom like '%"+prenom+"%'");

       return qr.getResultList(); 
    }

As you can see, It's well manged list of Eleveobjects.. So why this exception is thrown ?

正如您所看到的,它是管理良好的Eleve对象列表.. 那么为什么会抛出这个异常呢?

[EL Info]: 2013-12-02 13:25:33.312--ServerSession(1903253526)--EclipseLink, version: Eclipse Persistence Services - 2.3.0.v20110604-r9504
[EL Info]: 2013-12-02 13:25:35.005--ServerSession(1903253526)--file:/C:/Users/marwen/Documents/NetBeansProjects/JocondeFinale/build/classes/_JocondeFinalePU login successful
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Can not refresh not managed object: entities.Eleve[ idEleve=7 ].
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.refresh(EntityManagerImpl.java:943)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.refresh(EntityManagerImpl.java:849)
    at entities.GestionPaiement.niveauJcbbxItemStateChanged(GestionPaiement.java:736)
    at entities.GestionPaiement.access00(GestionPaiement.java:32)
    at entities.GestionPaiement$FormListener.itemStateChanged(GestionPaiement.java:607)
    at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1225)
    at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1273)
    at javax.swing.JComboBox.contentsChanged(JComboBox.java:1329)
    at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
    at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:94)
    at javax.swing.JComboBox.setSelectedItem(JComboBox.java:578)
    at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:624)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:835)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at javax.swing.plaf.basic.BasicComboPopup.processMouseEvent(BasicComboPopup.java:499)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access0(EventQueue.java:103)
    at java.awt.EventQueue.run(EventQueue.java:694)
    at java.awt.EventQueue.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue.run(EventQueue.java:708)
    at java.awt.EventQueue.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Cheers.

干杯。

采纳答案by Simiil

You're probably using a different EntityManager to refresh your entities than the one which loads the entities from the database. The entities are not managed in the new EntityManager. According to the javadoc, the exception is the normal behaviour for not managed entities. Either use the same EntityManager, or use merge()to make your objects managed

您可能正在使用不同的 EntityManager 来刷新实体,而不是从数据库加载实体的实体。实体不在新的 EntityManager 中管理。根据javadoc,异常是非托管实体的正常行为。要么使用相同的 EntityManager,要么使用merge()来管理你的对象

回答by Andrei I

That means that when calling entityManager.refresh(entity), your entities are not managed by that instanceof EntityManager, not by the entityManager used in findEleveByNiveauClasseNomPrenom(). On the other side, you may ask yourself why you try to refresh those entities (havbe you made any changes to them?).

这意味着,打电话时entityManager.refresh(entity),你的实体不受管理该实例EntityManager,而不是由EntityManager的使用findEleveByNiveauClasseNomPrenom()。另一方面,您可能会问自己为什么要尝试刷新这些实体(您是否对它们进行了任何更改?)。

回答by Dinath

As @Simiil says, mergecan get an entity bean managed.

正如@Simiil 所说,merge可以管理实体 bean。

@Transactional
public void refresh(Entity entity) {
    final Entity e = getFacade().getEntityManager().merge(entity).;
    getFacade().getEntityManager().refresh(e); // you cannot use $entity because you need to use the brand new managed entity 
}