1

I know that it is a trite question, but I could not find a solution. I have two beans and one of them has HashMap collection. I'm getting an exception when trying to read this collection. Mapping config had been specified to load this collection eagerly.

My environment is :

  • Hibernate 4.2.0
  • mysql-connector-java 5.1.24

Also I have two beans:

public class FeaturedDoc {
    private Long id;
    private Map<Feature, Float> features;

    public FeaturedDoc() {
        features = new HashMap<Feature, Float>();
    }
   (getters and setters)
}

and

public class Feature {
    private Long id;
    private String name;
    private Long internalId;
    (getters and setters)    
}

This beans have mapping:

<class name="Feature" table="FEATURE">
    <id name="id" type="long" column="ID">
        <generator class="increment"/>
    </id>
    <property name="name" length="255" type="string" unique="true" column="NAME" index="INDEX_NAME"/>
    <property name="internalId" type="long" unique="true" not-null="false" column="INTERNAL_ID" index="INDEX_INTID"/>
    <sql-insert>insert into FEATURE (NAME, INTERNAL_ID, ID) values (?, ?, ?) on duplicate key update ID = ID</sql-insert>
</class>

<class name="FeaturedDoc" table="FEATURED_DOC">
    <id name="id" type="long" column="ID">
        <generator class="increment"/>
    </id>
    <map  name="features" table="DOC_FEATURE" cascade="all" lazy="false" fetch="join">
        <key column="ID"></key>
        <map-key-many-to-many column="FEATURE_ID" class="Feature"/>
        <element column="value" type="float"/>
    </map>
</class>

Also I have DAO layer with method:

public FeaturedDoc read(long id) {
    FeaturedDoc fd = null;
    try {
        session.beginTransaction();
        fd = session.get(FeaturedDoc.class, id);
        session.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        session.getTransaction().rollback();
    } finally {
        close();
    }
    return fd;
}

When I'm trying to do something like this:

FeaturedDoc fd = daoService.read(26);
System.out.println(fd.getFeatures());

I'm getting an exception

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

Do you know how should I fix this error?

1
  • Do you know how should I fix this error? Commented Apr 19, 2013 at 10:47

2 Answers 2

1

Assuming that everything else is okay (in the mappings), have you tried putting lazy="false" before cascade="all". I found that this was a problem in my mapping which resulted in this LazyInitializationException error.

This ordering is shown in the following Hibernate Reference: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/collections.html

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I've tried, but it doesn't work. Actually I don't think that the order of attributes is the matter.
0

I have solved this problem! The reason was in Feature class. It had not hashCode and equals functions. After implementation of these functions everything has become ok.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.