Tag: lazy loading

带有2个数据库配置的Spring Boot – 使用第二个配置延迟加载不起作用

我有Spring Boot项目,有2个数据库配置。 主数据库配置: @Configuration @EnableTransactionManagement @EnableJpaRepositories(transactionManagerRef = “primaryTransactionManager”, entityManagerFactoryRef = “primaryEntityManagerFactory”, basePackages = { “com.example.repository.primary” }) public class PrimaryDbConfig { @Primary @Bean(name = “primaryDataSource”) @ConfigurationProperties(prefix = “spring.primary.datasource”) public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Primary @Bean(name = “primaryEntityManagerFactory”) public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier(“primaryDataSource”) DataSource dataSource) { return builder.dataSource(dataSource).packages(“com.example.domain.primary”).persistenceUnit(“primary-persistence-unit”).build(); } @Primary @Bean(name = “primaryTransactionManager”) public PlatformTransactionManager […]

使用Spring HibernateDaoSupport进行延迟加载?

问候我正在使用Spring + Hibernate开发一个非Web应用程序。 我的问题是HibernateDaoSupport如何处理延迟加载,因为在调用DAO之后,Session被关闭了。 看看下面的伪代码: DAO就像: CommonDao extends HibernateDaoSupport{ Family getFamilyById(String id); SubFamily getSubFamily(String familyid,String subfamilyid); } 域模型如下: Family{ private List subfamiles; public List getSubFamiles(); } SubFamily{ private Family family; public Family getFamily(); } 在应用程序中,我从app-context获取DAO并希望执行以下操作。这可能与延迟加载有关,因为AFAIK在每个方法(getFamilyById(),getSubFamily())之后关闭会话。 CommonDAO dao=//get bean from application context; Family famA=dao.getFamilyById(familyid); // //Do some stuff List childrenA=fam.getSubFamiles(); SubFamily asubfamily=dao.getSubFamily(id,subfamilyid); // //Do some other […]

Hibernate:Hibernate总是使用对象代理吗?

我认为仅当类具有Collection类型的字段并且使用Lazy fetching时才使用对象代理。 但是一些消息来源似乎暗示Hibernate 3对所有对象使用代理,而不管对象是否具有集合类型字段。 当Hibernate使用对象代理时,有人可以解释一下吗? 是在所有的时间,还是只是在某些情况下?

单身懒惰加载模式

我正在尝试编写Singleton Lazy Loading Pattern。 这是class级: public class IMDBLookup { private static class LazyLoad { private static final IMDBLookup IMDB_LOOKUP; static { IMDB_LOOKUP = new IMDBLookup(); } } public static IMDBLookup getInstance() { return IMDBLookup.LazyLoad.IMDB_LOOKUP; } } 我想知道我是否以正确的方式做到了这一点? 提前致谢。

Java-EE6:FetchType.LAZY与静态编织抛出奇怪的exception

我的解决方案包含3个不同的项目: 使用Netbeans自动生成Facade的EJB项目来管理实体类和persistence.xml 包含所有@Entity注释和静态编织数据库类的类库,以及用于外观ejb的远程接口(在EJB和独立客户端之间共享) 独立客户端,主要由Swing GUI类组成 我使用Glassfish 3.1.2,Eclipselink 2.3作为JPA-provider,Netbeans 7.1.1和MySQL数据库。 我配置了一个Ant-task,它基于persistence.xml静态编织我的实体类。 我有几个@OneToOne,@ ManyToOne和@ManyToMany用fetch = FetchType.LAZY装饰的实体之间的注释关系。 现在我得到了以下错误: Exception in thread “Mainframe Loader” Local Exception Stack: Exception [EclipseLink-7242] (Eclipse Persistence Services – 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.ValidationException Exception Description: An attempt was made to traverse a relationship using indirection that had a null Session. This often occurs when an entity with […]

如何懒惰地在Spring应用程序中加载LDAP配置。

我有application-context.xml,它有如下所示的bean。 和一个context.xml一样 ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++ 如果您在我的context.xml中注意到我将资源名称保留为DB_NAME1 我还在application-context.xml文件顶部的beans标记中保留了default-lazy-init =“true” 。 我仍然低于错误 javax.naming.NameNotFoundException: Name [DB_NAME] is not bound in this Context. Unable to find [DB_NAME]. 所以我的问题是,如何懒洋洋地加载我的jdbcTemplate / dataSource。 因为在我的应用程序中,一些服务正在攻击数据库,而一些服务正在攻击其他服务。 因此,即使数据库已关闭,其他服务也不应停止工作。

在Java 8中懒惰地返回第一个非空列表

我有N个列表从存储库返回数据。 我想返回这三个列表中的第一个非空(每个列表执行不同的SQL来获取数据)。 问题是我想懒得这样做,所以如果我已经找到了可接受的结果,我就不需要在数据库上执行SQL。 我的代码是(修改过的) @Override public List dataService(Data data) { return firstNonEmptyList(repository.getDataWayOne(data.getParameter()), repository.getDataWayTwo(data.getParameter()), repository.getDataWayThree(data.getParameter().getAcessoryParameter()) Collections.singletonList(repository.getDefaultData(data.getParameter())); } @SafeVarargs private final List firstNonEmptyList(List… lists) { for (List list : lists) { if (!list.isEmpty()) { return list; } } return null; } 这有效,但它不是懒惰的。 有任何想法吗?

spring@Autowired @Lazy

我正在使用Spring注释,我想使用延迟初始化。 我遇到了一个问题,当我想从另一个类导入一个bean时,我被迫使用@Autowired似乎没有使用lazy init。 反正有没有强制这种懒惰的初始化行为? 在这个例子中,我不希望看到“正在加载父bean”,因为我只加载了对lazyParent没有依赖性的lazyParent 。 @Configuration public class ConfigParent { @Bean @Lazy public Long lazyParent(){ System.out.println(“Loading parent bean”); return 123L; } } @Configuration @Import(ConfigParent.class) public class ConfigChild { private @Autowired Long lazyParent; @Bean public Double childBean() { System.out.println(“loading child bean”); return 1.0; } @Bean @Lazy public String lazyBean() { return lazyParent+”!”; } } public […]

Swing和延迟加载组件

我使用Eclipse插件Visual Editor来构建Java Swing接口。 由于我不是代码WYSIWYG(UI)编辑器生成的忠实粉丝,我想优化它,当我注意到,编辑器使用延迟加载实现所有元素,如下所示: private JPanel getSomePanel () { if ( somePanel == null ) { somePanel = new JPanel(); // construct the panel } return somePanel; } 我知道,当不立即使用有问题的对象时,延迟加载用于获得更好的性能。 然而,对于大多数用户界面而言,这没有多大意义,因为例如窗口通常应该从一开始就显示其上的所有组件。 在我的情况下也是这种情况,我有一个相当简单的清晰布局,其中所有组件在显示窗口时都应该存在。 Visual Editor在根容器的构造函数中添加了一个initialize调用,其中构造了根面板并添加了所有其他元素(通过延迟加载)。 所以实际上所有组件都是在构造根容器时创建的,只是嵌套在多个方法中。 在这种情况下,是否有任何延迟加载的用途? 在哪些UI情况下我应该使用延迟加载? 当使用延迟加载时,我实际上甚至允许直接访问成员变量 – 或者我应该每次都调用getter? 谢谢!

使用基于注释的配置创建延迟初始化的Spring bean

我使用Spring的@Component注释来配置Spring 3.0应用程序中的许多bean。 我想知道是否可以懒惰地构建一些这些豆 – 特别是prototype豆?