Questions tagged [lazy-initialization]

Lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed.

This is typically accomplished by maintaining a flag indicating whether the process has taken place. Each time the desired object is summoned, the flag is tested. If it is ready, it is returned. If not, it is initialized on the spot.

In case of Hibernate, the lazy initialization is made in the proxy (created in place of collection) using the hibernate session from which the object was obtained. If the collection is accessed when the original session is closed (for example, outside a @Transactional scope), the LazyInitializationException is thrown.

To initialize the collection, it is enough to call size() or iterate through all elements.

705 questions
-1
votes
1 answer

Hibernate circled entities

straight to the point: I have a group that contains projects. I want that association to be handle with a foreign key, which is why it has a mappedby tag. My issue is that if I query for groups I get into an inifinite loop where the group lists the…
Barry
  • 337
  • 1
  • 2
  • 15
-1
votes
1 answer

How to Initialise view as lazy var in Objective C

Well! I had worked with Lazy var in swift. Nevertheless, I want to use the lazy var type to my accessory view in one of my Objective C projects. I couldn’t find the exact answer for declaring UIView as lazy var type. So, Share your ideas, if you had…
-1
votes
2 answers

async Lazy getting results right away

I have the following: public class User { private readonly Lazy>> _reminders; public SmsUserDb() { // Get _reminderReader from IoC _reminders = new…
ShaneKm
  • 20,823
  • 43
  • 167
  • 296
-1
votes
2 answers

Pros and Cons of javascript lazy initialization

Hi i wanted to hear any pros and cons considering javascript lazy initialization, when script is loaded only when object is used. Any comments would be highly appreciated.
Donatas Cereska
  • 515
  • 1
  • 5
  • 11
-1
votes
2 answers

How to avoid StackOverflowException in static constructor with `Lazy` initialization of large lists

The code below is throwing System.StackOverflowException in VS2015 Update 3 (Resharper 2016, Windows 10 x64, .NET 4.5). IMO, it is due to the cyclic initialization in the static constructors of City -> Authority -> Country -> City. Strange enough,…
Dio Phung
  • 5,944
  • 5
  • 37
  • 55
-1
votes
1 answer

How to add a Lazy implementation to an existing public property (C#)

So I'm having this class with its public property: public static class DataStore { public static readonly List Items; static DataStore { Items = new List { new Item {Name = "A"}, …
Dio Phung
  • 5,944
  • 5
  • 37
  • 55
-1
votes
4 answers

What's the different from new Lazy() to new Lazy(() => new Foo())?

I was reading some .NET 4+ code where it use a lambda in Lazy's constructor, like this new Lazy(() => new Foo()). How does it differ from new Lazy() when no arguments are passed to Foo's constructor?
Jack
  • 16,276
  • 55
  • 159
  • 284
-1
votes
1 answer

How do I (elegantly) assign reference members according to a char alias?

Disclaimer I don't actually propose to apply this design anywhere, but I've been curious nonetheless how one would implement this in C++, in particular given C++'s lack of reflection. (I'm simultaneously learning and experimenting with C++11…
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
-1
votes
1 answer

Lazy initialization of object passed as argument

Let's say I have a method that takes a very long time to create an object, public class Foo implements Serializable { public static Foo create(...){ /* (takes a long time) */ } } I also have a helper class that serializes and…
jedwards
  • 29,432
  • 3
  • 65
  • 92
-1
votes
1 answer

JPA LazyInitializationException?

I am using jquery for UI , Struts2 as Action , JPA for Data Access Layer. The issue I am facing is that I am getting LazyInitializationException when I am trying to retrieve values from the database . I am getting the below stacktrace…
Esh
  • 836
  • 5
  • 16
  • 43
-2
votes
2 answers

How to do lazy instantiation in python?

in the class below variables with str type are used but without declaring value from abc import ABC class User(ABC): first_name: str last_name: str is this lazy instantiation?
-2
votes
2 answers

How to use lazy initialization with getter/setter method?

How i can use lazy initialization with get and set() closure. Here is lazy initialization code: lazy var pi: Double = { // Calculations... return resultOfCalculation }() and here is getter/setter code : var pi: Double { …
Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32
-2
votes
2 answers

Strange Lazy initialization exception

I am getting a lazy initialization exception that I don't understand... I work with Java, Hibernate, Spring and Wicket. So, from the save method of the Form (extends wicket Form) I get a LazyInitializationException when accesing a colletion of an…
diminuta
  • 1,545
  • 8
  • 32
  • 55
-2
votes
1 answer

NHibernate Lazy loading lazy=extra no session or session was closed

I have a strange behaviour which I can't find any solution for days.. The behaviour I experience is the classic LazyInitializationException with no session or session was closed which is usual when there's no contextual session and trying to access…
-4
votes
2 answers

Remove null check after lazy initialization

When one decides to use lazy initialization, he usually has to pay for it. class Loafer { private VeryExpensiveField field; private VeryExpensiveField LazyInitField() { field = new VeryExpensiveField(); // I wanna here…
Danatela
  • 349
  • 8
  • 28
1 2 3
46
47