我正在使用spring和hibernate应用程序,在我的要求中,我必须在项目部署时从数据库加载数据并将数据存储在Map或List中以便在整个应用程序中进一步使用。我已经google了很多但没有找到任何解决方案。
答案 0 :(得分:0)
尝试使用ServletContainerInitializer
如果此ServletContainerInitializer捆绑在应用程序的WEB-INF / lib目录内的JAR文件中,则它的onStartup方法将仅在捆绑应用程序启动期间调用一次。
答案 1 :(得分:0)
您可以使用
<prop key="hibernate.cache.use_query_cache">true</prop>
在 hibernate configuration.xml 中 缓存从数据库访问的数据并存储在对象中......现在,如果在应用程序的任何地方使用相同的对象,它将使用相同的缓存,并且不会再次访问数据库。
另一种方式是
您可以使用xml文件配置缓存,请参阅spring参考手册:
<!-- the service we want to make cacheable -->
<bean id="service" class="x.y.service.MyService"/>
<!-- cache definitions -->
<cache:advice id="cacheAdvice" cache-manager="cacheManager">
<cache:caching cache="books">
<cache:cacheable method="getData" key="#id"/>
<cache:cache-evict method="loadData" all-entries="true"/>
</cache:caching>
</cache:advice>
<!-- apply the cacheable behaviour to all dataService interfaces -->
<aop:config>
<aop:advisor advice-ref="cacheAdvice" pointcut="execution(* x.y.myService.*(..))"/>
</aop:config>
答案 2 :(得分:0)
Better approach is to use Hibernate, In hibernate we have First Level cache.
First-level cache always Associates with the Session object. Hibernate uses this cache by default. Here, it processes one transaction after another one, means wont process one transaction many times. Mainly it reduces the number of SQL queries it needs to generate within a given transaction. That is instead of updating after every modification done in the transaction, it updates the transaction only at the end of the transaction.
First level cache retrieval example
In this example, I am retrieving DepartmentEntity object from database using hibernate session. I will retrieve it multiple times, and will observe the sql logs to see the differences.
//Open the hibernate session
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
//fetch the department entity from database first time
DepartmentEntity department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1));
System.out.println(department.getName());
//fetch the department entity again
department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1));
System.out.println(department.getName());
session.getTransaction().commit();
HibernateUtil.shutdown();
Output:
Hibernate: select department0_.ID as ID0_0_, department0_.NAME as NAME0_0_ from DEPARTMENT department0_ where department0_.ID=?
Human Resource
Human Resource
As you can see that second “session.load()” statement does not execute select query again and load the department entity directly.
答案 3 :(得分:0)
首先you can't use any List or Map as Cache replacement。您可以使用Hibernate二级缓存,甚至是外部缓存解决方案,只要您的数据不会被修改,这可能会导致一致性问题。
因此,如果缓存的数据是只读的,那么您可以安全地缓存它。否则,您需要一个读写缓存,第二级缓存在这种情况下更合适。
答案 4 :(得分:0)
如果需要在加载类时加载集合,可以在Hibernate中使用立即获取或急切加载策略。根据急切的加载,当你把任何收集作为急切加载时,我们假设 - &gt;
如果Foo有一个Collection,并且你把它设置为lazy,那么只有当你需要那个集合的内容时才会选择,加载等等。如果它很渴望,它会加载吧加载Foo的时间。如果您急切地加载急切加载实体集合的实体集合,这可能会有问题,等等。 但是我想在你的情况下,Fetch Type = eager策略会起作用,它会在加载类时加载。
一个例子: @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER) @JoinColumn(名称= “countryId”) 私人设定状态;