Home Lazy load doesn't work - I Only need the parent object, not all associations
Reply: 0

Lazy load doesn't work - I Only need the parent object, not all associations

user2564
1#
user2564 Published in June 25, 2018, 12:00 am

I have a problem to get the object I want without all childs associations.
I have my class Site


    @Entity
    @Table(name = "Sites")
    public class Site  {


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Id_Site", unique = true, nullable = false)
    private long Id_Site;
    private String ...;
    private boolean ...;
    private long ...; 
    private Date ...;
    private Date ...;
    private String ...;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set sequences = new HashSet();

    @ManyToOne
    private ... ...;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set ... = new HashSet();

    @ManyToOne
    private ... ...;

    public constructor...

    public set..
    public get..

so, I only need Site object without sequences Association
in my Sequence Table I have




    @Entity
    @Table(name = "Sequences")
    public class Sequence {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Id_Sequence", unique = true, nullable = false)
    private long Id_Sequence;
    private Date ....;
    private Date ....;
    private String ....;
    private String ....;
    private String ....;
    private int ....;
    private int ....;
    private double ....;
    private double ....;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Set traceSequences = new HashSet();
    @ManyToOne(cascade = CascadeType.ALL)
    private Site site;

    public constructor...

    public set..
    public get..



so, when I use FetchType.Lazy, and call my method :


    @Override
        public Site findSiteByName(String Name_Site) {
            List sites = entityManager.createQuery("SELECT s FROM Site s").getResultList();
            for (Site item : sites) {
                if (item.getNom_Site().equals(Name_Site)) {
                    return item;
                }
            }

            return null;
        }

I get this errors :

failed to lazily initialize a collection of role: xxx.xxx.xxx.xxx.xxx.site.Site.sequences, could not initialize proxy - no Session

And when I use FetchType.EAGER, I get Site object, but I get also all sequences objects, and all objects of others associations (It's normal I know)

Please, some one knows why lazy annotation doesn't work, and how resolve this problem

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.324357 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO