对于非列对象,Hibernate @Entity与Spring @Autowired发生冲突 [英] Hibernate @Entity conflict with Spring @Autowired for non-column object
问题描述
我有一张表格,其中包含项目描述.商品的价格历史记录可能非常广泛.正是这最后一点使我避免使用带有延迟加载的正常的一对多Hibernate映射.想一想价格历史记录,例如证券交易所的报价,历史记录很多.
I have a table which contains item descriptions. Items have a price history which can be very extensive. It's that last bit that leads me to avoid using a normal one-to-many Hibernate mapping with lazy loading. Think a price history like ticks on a stock exchange, lots of history.
所以我有一个运行良好的缓存,它全部与Spring相连,已注入DAO,该缓存管理需要查询的内容与已经知道的内容.
So I have a cache which works well, it's all wired with Spring, the DAO is injected, the cache manages what needs to be queried vs what it already knows.
因此,自然"的事情是能够询问某个商品的价格历史记录.这是一些代码,是真实内容的精简版:
So, the "natural" thing is to be able to ask an item about it's price history. Here's some code, which is a slimmed down version of the real thing:
@Entity @Table(name="item")
public class Item {
@Id
@Column(name="id")
private long id;
@Column(name="name")
private String name;
@Autowired
private PriceCache priceCache;
/* ...setters, getters for id, name ... */
public NavigableMap<LocalDateTime,SecurityValue> getPrices(LocalDateTime begTime, LocalDateTime endTime) {
return priceCache.get(id, begTime, endTime);
}
}
我的原始版本在PriceCache中使用了所有静态方法;我想部分转而使用注入的Bean,因为这意味着我可以将缓存重写为接口的实现,从而可以更轻松地为示例中未包含的某些位设置单元测试.我可以创建一个测试缓存对象,以不需要测试的任何方式提供我的价格历史记录,而无需访问数据库.
My original version used all static methods with PriceCache; I want to switch to using an injected bean in part because it will mean I can rewrite the cache as an implementation of an interface which makes it easier to set up unit tests for some bits that aren't in the example; I can create a test cache object that supplies my price history in whatever way I need to the test without ever going to the database.
问题在于,当Spring和Hibernate扫描软件包时,它们似乎在处理@Autowired字段时发生冲突;我得到以下内容,并具有一些可读性的格式); dbEMF是我的EntityManagerFactory:
The problem is that when Spring and Hibernate scan the packages, they seem to conflict over what to do with that @Autowired field; I get the following with some formatting for readability); dbEMF is my EntityManagerFactory:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dbEMF' defined in class path resource [applicationContext.xml]:
Invocation of init method failed;
nested exception is javax.persistence.PersistenceException:
[PersistenceUnit: default] Unable to build Hibernate SessionFactory;
nested exception is org.hibernate.MappingException:
Could not determine type for: com.example.cache.PriceCache, at table: item, for columns: [org.hibernate.mapping.Column(priceCache)]
同样,如果我仅将静态方法与PriceCache配合使用,则可以将其作为单例手动"创建,那么基本代码和缓存也可以正常工作.将其转换为让Spring处理其他地方的创建和注入也可以正常工作.只有当我同时使用Hibernate和Spring时,我才遇到问题.
Again, the basic code and cache work fine provided I use only static methods with the PriceCache, where I create it as a singleton "manually". Converting it to let Spring handle the creating and injection elsewhere works just fine, too. It's only when I have this mix of Hibernate and Spring that I run into a problem.
我还没有尝试过将外部XML文件用于hibernate配置,这可能会解决问题,或者不会解决问题.
I haven't tried going back to using an external XML file for the hibernate config which might solve the issue, or not.
有没有办法告诉Hibernate这不是列?还是我应该遵循不同的模式来执行此类操作,也许是Item对象的某种代理?
Is there a way to tell Hibernate this is not a column? Or is there a different pattern I should be following to do this sort of thing, maybe some sort of proxy for the Item objects?
推荐答案
您可以使用@Transient
批注,以指示不应将其持久化到数据库中.
you can use @Transient
annotation, to indicate it should not be persisted into DB.
一般来说,我认为如果这是一个实体,则不应有不属于它的任何自动装配的缓存,但这是另外一回事了
Generally speaking, I think if this is an entity, it should not have any autowired cache which is not part of it, but that's a different story
这篇关于对于非列对象,Hibernate @Entity与Spring @Autowired发生冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!