简体   繁体   中英

Hibernate - unable access to object property

I have a problem with access to the object in my Hibernate App.

Class Foo:

public class Foo {

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

foo.hbm.xml

<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>

Class Bar:

public class Bar{

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    private Date date;
    private Foo foo;
    private long quantity;
    Bar() {}
       //getters, setters...
    }

bar.hbm.xml

<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>

I have a table with MyTableModel extends AbstractTableModel, there is method called getValueAt.

    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];
    }

This method is working very fine, but only, when I call b.getFoo().getId(). When I tried to call b.getFoo().getName(), then I received an error as following:

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)

It occours in line with Object[] values . What should I do for having access to this object (I don't want to create third table, because I have multiple situations in my app when I need this feature. Thanks for help

I'm gonna try to explain it to you on a simpler example. Let's say you have a class Cat in a database and you want to retrieve it, so you do:

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

At this moment you think 'I already got all the Cats saved in my list, let's close the session' and you do:

session.close();

Then you want to use those cats, you do something like:

cats.get(0).meow();

And you're gonna get that error, because Hibernate didn't actually got all those cats and put them in a list, it tried to do that only when you invoked cats.get(0) . Even if it did get the cats, because you did something with them - what if every cat has a reference to let's say CatFood object? And the CatFood objects can have references to other objects aswell, and everything is in a database, so Hibernate can't just pull everything, because this may be a lot of data, so it will only retrieve them 'on the fly' that is when you try to actually use them and it can't if you already closed the session. We can't see your whole code here, but my guess is you did exactly that, closed the session and then tried to work with those objects. You need to close the session only when you finished your job with them, not sooner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM