Why not to use Spring’s OpenEntityManagerInViewFilter

As you said, the OpenSessionInView filter is very convenient in web applications. Regarding the limitations you mentioned: 1) Loading several lazy associations will result in multiple database transactions, a possible hit on performance. Yes, going to the DB often might lead to performance problems. Ideally you want to fetch all the data you need in … Read more

Hibernate/Spring: failed to lazily initialize – no session or session was closed

I think you should not use the hibernate session transactional methods, but let spring do that. Add this to your spring conf: <bean id=”txManager” class=”org.springframework.orm.hibernate3.HibernateTransactionManager”> <property name=”sessionFactory” ref=”mySessionFactory” /> </bean> <bean id=”transactionTemplate” class=”org.springframework.transaction.support.TransactionTemplate”> <property name=”transactionManager” ref=”txManager”/> </bean> and then I would modify your test method to use the spring transaction template: public static void main(String[] … Read more

Entity Framework: I set the foreign key, SaveChanges then access the navigation property, but it doesn’t load the related entity. Why not?

To ensure that lazy loading of a navigation property will work after you’ve created the parent you must not create the Survey with the new operator but create it by means of the context instance because it will instantiate a dynamic proxy that is capable to lazily load the related Client. That’s what the DbSet<T>.Create() … Read more

How to trigger an on scroll event without scrolling

Alternatively, you can manually trigger a real scroll event as following: el.dispatchEvent(new CustomEvent(‘scroll’)) Which feels a bit less of a hack (and more performant) than dual scrolling by +1 and -1 pixels… This should run any piece of code listening for a scroll event. Edit: To support IE11 or other legacy browser, consider using a … Read more

Thread-safe cache of one object in java

google collections actually supplies just the thing for just this sort of thing: Supplier Your code would be something like: private Supplier<List<String>> supplier = new Supplier<List<String>>(){ public List<String> get(){ return loadCountryList(); } }; // volatile reference so that changes are published correctly see invalidate() private volatile Supplier<List<String>> memorized = Suppliers.memoize(supplier); public List<String> list(){ return memorized.get(); … Read more

Repository Pattern: how to Lazy Load? or, Should I split this Aggregate?

Am I misinterpreting the intent of the Repository pattern? I’m going to say “yeah”, but know that me and every person I’ve worked with has asked the same thing for the same reason… “You’re not thinking 4th dimensionally, Marty”. Let’s simplify it a little and stick with constructors instead of Create methods first: Editor e … Read more

Hibernate count collection size without initializing

A possible solution other than queries might be mapping children with lazy=”extra” (in XML notation). This way, you can fetch the Parent with whatever query you need, then call parent.getChildren().size() without loading the whole collection (only a SELECT COUNT type query is executed). With annotations, it would be @OneToMany @org.hibernate.annotations.LazyCollection( org.hibernate.annotations.LazyCollectionOption.EXTRA ) private Set<Child> children … Read more