在Spring上下文中配置自定义的休眠ID生成器 [英] Configure custom hibernate id generator in spring context

查看:55
本文介绍了在Spring上下文中配置自定义的休眠ID生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring和Hibernate,最近我创建了一个自定义ID生成器.现在,在我的Java域对象上,我有:

I am using Spring and Hibernate and I recently created a custom Id generator. Right now, on my Java domain object I have:

@GenericGenerator(name="...", strategy="com.acme.MyIdGenerator", ...)

我可以在我的spring上下文中创建/配置 com.acme.MyIdGenerator ,并且以某种方式让Hibernate使用它而不是让Hibernate实例化它吗?

Can I create/configure a com.acme.MyIdGenerator in my spring context and somehow have Hibernate use it instead of having Hibernate instantiate it?

推荐答案

如果您只是试图将生成器与使用者类(或多个类)隔离开,则可以在package-info.java类中定义生成器.参见测试套件中的示例.

If you are just trying to isolate your generator from the consumer class (or classes), you can define the generator in a package-info.java class. See this example in the test suite.

但是,如果您真的想将其映射为XML,则必须将该特定实体映射为hbm.xml(

But if you really want to map it in the XML, then you'll have to map that specific entity as a hbm.xml (as seen here) and add it to your Hibernate configuration in the Spring context.

这篇关于在Spring上下文中配置自定义的休眠ID生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆