Hibernate - 无法访问对象属性

时间:2016-09-15 17:00:07

标签: java hibernate object arraylist

我在Hibernate App中访问对象时遇到问题。

Class Foo:

@echo off
start /wait wusa.exe %~dp0Win7AndW2K8R2-KB3134760-x64.msu /quiet /norestart

foo.hbm.xml

public class Foo {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String name;
Foo() {}
   //getters, setters...
}

班级栏:

<class name="Foo" table="FOO">
        <id name="id" type="long" column="ID" length="20">
            <generator class="assigned" />
        </id>
        <property name="name" column="NAME" type="string"
            length="50">
        </property>
    </class>

bar.hbm.xml

public class Bar{

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    private Date date;
    private Foo foo;
    private long quantity;
    Bar() {}
       //getters, setters...
    }
我有一个表用MyTableModel扩展AbstractTableModel,有一个名为getValueAt的方法。

<class name="Bar" table="BAR">
    <id name="id" type="long" column="ID" length="20">
        <generator class="assigned" />
    </id>
    <many-to-one name="foo" column="foo" class="Foo"
        not-null="true"></many-to-one>
    <property name="date" column="DATE" type="date">
    </property>
    <property name="quantity" column="QUANTITY" type="long"
        length="20">
    </property>
</class>

这个方法工作得很好,但只有当我调用b.getFoo()。getId()时。 当我尝试调用b.getFoo()。getName()时,我收到如下错误:

    public Object getValueAt(int rowIndex, int columnIndex) {
        Bar b = ConsumptionList.get(rowIndex);
        Object[] values = new Object[] { b.getId(), b.getDate(), 
                b.getFoo().getName(), _s.getQuantity() };
        return values[columnIndex];
    }

它与Exception in thread "AWT-EventQueue-0" org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185) at Foo_$$_jvstdd_0.getName(Foo_$$_jvstdd_0.java) at MyModel$MyTableModel.getValueAt(MyModel.java:66) at javax.swing.JTable.getValueAt(Unknown Source) at javax.swing.JTable.prepareRenderer(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source) at javax.swing.plaf.ComponentUI.update(Unknown Source) at javax.swing.JComponent.paintComponent(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JViewport.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintToOffscreen(Unknown Source) at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) at javax.swing.RepaintManager.paint(Unknown Source) at javax.swing.JComponent._paintImmediately(Unknown Source) at javax.swing.JComponent.paintImmediately(Unknown Source) at javax.swing.RepaintManager$4.run(Unknown Source) at javax.swing.RepaintManager$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.access$1200(Unknown Source) at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 一致。我应该怎么做才能访问这个对象(我不想创建第三个表,因为当我需要这个功能时,我的应用程序中有多种情况。感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我会尝试用更简单的例子向你解释。假设您在数据库中有一个类Cat,并且您想要检索它,所以您可以这样做:

Session session = getSessionFactory().openSession();
List<Cat> cats = getAllTheCatsFromDatabase();

此时你会想'我已经把所有的猫都保存在我的名单中了,让我们关闭会议'然后你做了:

session.close();

然后你想使用那些猫,你做的事情如下:

cats.get(0).meow();

你会得到那个错误,因为Hibernate实际上没有得到所有那些猫并把它们放在一个列表中,它只是在你调用cats.get(0)时尝试这样做。即使它确实得到了猫,因为你对它们做了一些事情 - 如果每只猫都提到让我们说CatFood对象呢?并且CatFood对象也可以引用其他对象,并且所有内容都在数据库中,因此Hibernate不能只提取所有内容,因为这可能是很多数据,所以它只会检索它们飞'那是你试图实际使用它们时,如果你已经关闭了会话就不能。我们在这里看不到你的整个代码,但我猜你是这么做的,关闭会话然后尝试使用这些对象。只有在你完成工作后才需要关闭会话,而不是更快。

相关问题