0

I got a problem, with my hibernate database connection. At first I post some data:

Objects.java:

@Entity
@Table (name = "objects", schema="genmeta")
public class Objects {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int o_id;
    private String o_name;
    private String o_desc;
    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinTable(name = "genmeta.object_tg_assc", joinColumns = { @JoinColumn(name = "o_id") }, inverseJoinColumns = { @JoinColumn(name = "tg_id") })
    private Set<TemplateGroup> templateGroups;
    @Transient
    private boolean templateGroupsLoaded = false;

    /**
     * Getters and Setters 
     */
}

TemplateGroup.java:

public class TemplateGroup {
    @Id
    @Column(unique=true, nullable=false)
    private int tg_id;
    private String tg_name;
    private String tg_desc;
    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "templateGroups")
    private Set<Objects> objects;

    /**
     * Getters and Setters 
     */
}

My Problem is, how I could get from my class Object later the data?

Here is my first try: Code Example:

public Set<TemplateGroup> loadTemplateGroups(Objects myObjects) {
    Session session = factory.openSession();
    Set<TemplateGroup> myTG = myObjects.getTemplateGroups();
    session.close();
    return myTG;
}

Thanks a lot.

Best regards Björn

Björn
  • 47
  • 1
  • 12
  • You do not need to explicitly retrieve the TemplateGroup list. When you access the `templateGroups` set from the `Objects` instance, hibernate will fire a query based on the mapping and populate the list with the right data. Are you getting an empty set? – Priyesh May 20 '14 at 13:01
  • Yes. Because of this Set I thought, that I have to load the data like this way. Did you know another way for me? – Björn May 20 '14 at 13:21
  • You will have to enable hibernate sql logging and check the SQL generated and logged by hibernate when you access `templateGroups`. – Priyesh May 20 '14 at 13:23
  • Do you have an example for me? Didn't know what you mean. Everybody said: "When you acces templateGroups". How? – Björn May 20 '14 at 14:02
  • If you do a simple `object.getTemplateGroups()`, hibernate will fire a query to get the contents of the set. In hibernate configuration file, you can enable sql logging. e.g., `true`. Check this link for further details, http://stackoverflow.com/questions/2536829/hibernate-show-real-sql. – Priyesh May 20 '14 at 14:32
  • Ok, I changed my example. Now I got this error: `failed to lazily initialize a collection of role: db.hibernate.classes.Objects.templateGroups, could not initialize proxy - no Session`. I found this Posting [link](http://stackoverflow.com/questions/5404599/failed-to-lazily-initialize-a-collection-of-role). What did he mean with "accessing the lazy collection within a transaction" – Björn May 20 '14 at 15:03
  • This error is thrown because you are retrieve the `Objects` instance in one transaction and calling `getTemplateGroups()` in another. – Priyesh May 20 '14 at 15:10
  • But I didn't know at the time, where I want to load my object, that I want to load the templateGroup, too. I want only load it, if I need it. – Björn May 20 '14 at 15:17
  • This cannot be achieve in different transactions. Both should happen in the same transaction. If you want to load the template groups in another transaction, you will have to retrieve the `Objects` again from the database. – Priyesh May 20 '14 at 15:23
  • Or try Session.refresh(object). – Priyesh May 20 '14 at 15:30

1 Answers1

1

The failed to lazily initialize a collection of role: db.hibernate.classes.Objects.templateGroups, could not initialize proxy - no Session exception is thrown because you are retrieving the Objects instance in one transaction and calling getTemplateGroups() in another(or after the first transaction is committed). You should not open another session to get the template groups.

You need to do something like below:

Session session = factory.openSession();
Objects object = <Code to retrieve the Objects instance>
Set<TemplateGroup> templateGroups = object.getTemplateGroups();
templateGroups.size(); // as an example
session.close();
Priyesh
  • 1,963
  • 1
  • 15
  • 23