lazy-initialization

Lazy initialisation and retain cycle

淺唱寂寞╮ 提交于 2019-12-28 05:37:31
问题 While using lazy initialisers, is there a chance of having retain cycles? In a blog post and many other places [unowned self] is seen class Person { var name: String lazy var personalizedGreeting: String = { [unowned self] in return "Hello, \(self.name)!" }() init(name: String) { self.name = name } } I tried this class Person { var name: String lazy var personalizedGreeting: String = { //[unowned self] in return "Hello, \(self.name)!" }() init(name: String) { print("person init") self.name =

LazyInitializationException on getId() of a @ManyToOne reference

淺唱寂寞╮ 提交于 2019-12-24 15:53:33
问题 I'm facing LazyInitializationException when I'm trying to access ID of a lazy @ManyToOne reference of a detached entity. I do not want to fetch the refrence completely, but just need the ID (which should be exist in original object in order to fetch refrence in a lazy/deferred manner). EntityA ea = dao.find(1) // find is @Transactional, but transaction is closed after method exits ea.getLazyReference().getId() // here is get exception. lazyReference is a ManyToOne relation and so the foreight

Lazy initialization and deinit

五迷三道 提交于 2019-12-24 06:49:56
问题 I would to know if it's possible, in my view controller, use a lazy property and in deinit method call a method of my lazy property only if it was initialized. Below some code: fileprivate lazy var session: MySession = { let session: MySession = MySession() session.delegate = self return session }() deinit { session.delete() } In this way, when session.delete() in the deinit method is called and session hasn't been used (so is still nil ), it's initialized and then delete is called. I don't

Dozer throwing lazyInitializationException

心不动则不痛 提交于 2019-12-23 04:36:12
问题 I'm trying to map my POJO to a DTO. I do not have any custom field mappers as all my fields in my DTO are same as in my POJO. But my POJO has multiple level of mapping involved. My problem is when i'm trying to copy the contents of my POJO to the DTO, i'm getting LazyInitializationException. Here is the code where the exception is thrown. public TestInfoDTO startTest(long testId) { TestInfo info = testDAO.startTest(testId); Mapper mapper = new DozerBeanMapper(); try { // Exception thrown here

Is this a good implementation for a Thread Safe Lazily Loaded Singleton in Java?

蓝咒 提交于 2019-12-23 00:29:08
问题 I want to implement Thread Safe Lazily Loaded Singleton in Java. Is this a good design? public final class ThreadSafeLazySynchronizedSingleton { private ThreadSafeLazySynchronizedSingleton(){} private static ThreadSafeLazySynchronizedSingleton instance; public static ThreadSafeLazySynchronizedSingleton getSynchronizedInstance(){ synchronized(ThreadSafeLazySynchronizedSingleton.class){ if (instance==null){ instance = new ThreadSafeLazySynchronizedSingleton(); } return instance; } } } I also

Should C# have a lazy key word

人盡茶涼 提交于 2019-12-21 04:11:42
问题 Should C# have a lazy keyword to make lazy initialization easier? E.g. public lazy string LazyInitializeString = GetStringFromDatabase(); instead of private string _backingField; public string LazyInitializeString { get { if (_backingField == null) _backingField = GetStringFromDatabase(); return _backingField; } } 回答1: I don't know about a keyword but it now has a System.Lazy<T> type. It is officially part of .Net Framework 4.0 . It allows lazy loading of a value for a member . It supports a

Hibernate could not initialize proxy - no Session

喜欢而已 提交于 2019-12-21 03:45:40
问题 My code retrieves all information related to the user: SessionFactory sessionFactory = HibernateUtilities.configureSessionFactory(); Session session = sessionFactory.openSession(); UserDetails ud = null; Set<Address> userAddress = null; try { session.beginTransaction(); ud = (UserDetails) session.get(UserDetails.class, 1); userAddress = ud.getAddresses(); session.getTransaction().commit(); } catch (HibernateException e) { e.printStackTrace(); session.getTransaction().rollback(); } finally {

Is it normal that lazy var property is initialized twice?

ⅰ亾dé卋堺 提交于 2019-12-21 03:38:44
问题 I have met very weird case of using an property with lazy keyword. I know this keyword indicates that an initialization of an property is defered until the variable actually being used and just runs once . However, I found a case running an initialization twice. class TestLazyViewController: UIViewController { var name: String = "" { didSet { NSLog("name self = \(self)") testLabel.text = name } } lazy var testLabel: UILabel = { NSLog("testLabel self = \(self)") let label = UILabel() label

How to create decorator for lazy initialization of a property

喜你入骨 提交于 2019-12-21 00:23:13
问题 I want to create a decorator that works like a property, only it calls the decorated function only once, and on subsequent calls always return the result of the first call. An example: def SomeClass(object): @LazilyInitializedProperty def foo(self): print "Now initializing" return 5 >>> x = SomeClass() >>> x.foo Now initializing 5 >>> x.foo 5 My idea was to write a custom decorator for this. So i started, and this is how far I came: class LazilyInitializedProperty(object): def __init__(self,

Initialize-On-Demand idiom vs simple static initializer in Singleton implementation

可紊 提交于 2019-12-20 02:32:38
问题 Is the Initialize-On-Demand idiom really necessary when implementing a thread safe singleton using static initialization, or would a simple static declaration of the instance suffice? Simple declaration of instance as static field: class Singleton { private static Singleton instance=new Singleton(); private Singleton () {..} public static Singleton getInstance() { return instance; } } vs class Singleton { static class SingletonHolder { static final Singleton INSTANCE = new Singleton(); }