本文介绍了如何定义可重用的< generator> Hibernate映射中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Hibernate hbm.xml 映射文件,它定义了大量的类映射。其中每一个都需要< id> 定义和相应的< generator> 定义。

在这个特定的应用程序中,< generator> 定义非常复杂,包括设置一些参数例如增量大小,优化器等)。不得不为每个类重复这个定义是非常乏味的,并且会使映射混乱。

如果我使用JPA注释,我可以在包级别定义ID生成器。但是我没有使用JPA(也不是一种选择),并且它似乎最能够在JPA中做某些事情,而不是原生Hibernate。



我可以在映射中指定生成器并在映射中多次引用它?

解决方案

这是遗憾的是Hibernate Annotations支持,但Hibernate Core不支持。我过去解决这个问题的方式是:


  1. 编写我自己的 IdentifierGenerator 执行;使其实现可配置
  2. 让它拥有由唯一名称键入的生成器属性的静态映射(每个映射条目将表示一个预定义的生成器配置)。
  3. 所有配置在Spring上下文中使用这个相同的类声明为bean(或者,如果你想/需要一些类型安全性,你可以使它们成为实际bean与setters)。创建后回调(例如, afterPropertiesSet())使用bean id作为关键字将bean属性添加到上述映射中。基础生成器策略就是其中一个属性。

  4. Configurable.configure()通过<$ c $获取底层生成器c> IdentifierGeneratorFactory.getIdentifierGeneratorClass(),并使用传递给 configure()的Properties实例对它进行初始化。 >

因此,在Hibernate映射中,您可以执行以下操作:

 < generator class =com.mypackage.PredefinedGenerator> 
< param name =preset> preset1< / param>
< / generator>

在您的Spring上下文中:

 < bean id =preset1class =com.mypackage.PredefinedGenerator> 
< property name =parameters>< props>
< prop key =generatorStrategy> seqhilo< / prop>
< prop key =sequence> my_sequence< / prop>
< prop key =max_lo> 500< / prop>
< /道具>< /属性>
< / bean>

当然,如果您不需要多个预设,这可以被简化。出于法律原因,我不能发布实际的代码;但如果您对这种方法感兴趣,但上述内容尚不清楚,请发表评论,我会尽力澄清。


I have a Hibernate hbm.xml mapping file which defines a large number of class mappings. Each of these needs an <id> definition and a corresponding <generator> definition.

In this particular application, the <generator> definition is quite involved, including setting a number of parameters (e.g. increment size, optimizer, etc). Having to repeat this definition for every class is tedious and clutters up the mapping.

If I were using JPA annotations, I could define the ID generator at the package level. But I'm not using JPA (nor is it an option), and it seems most peculiar to be able to do something in JPA but not native hibernate.

Can I specify the generator elsewhere, and refer to it multiple times in the mapping?

解决方案

This is sadly one of very few things Hibernate Annotations supports but Hibernate Core does not. The way I've worked around this issue in the past was to:

  1. Write my own IdentifierGenerator implementation; make it implement Configurable as well.
  2. Have it hold a static map of generator properties keyed by unique name (each map entry will represent a "predefined" generator configuration).
  3. Said configurations are declared as beans in Spring context using this very same class (or , if you want / need some type safety you can make them actual beans with setters). Post-creation callback (e.g. afterPropertiesSet()) adds bean properties to the above map using bean id as key. The underlying generator "strategy" is one of those properties.
  4. Configurable.configure() grabs the underlying generator via IdentifierGeneratorFactory.getIdentifierGeneratorClass() and initializes it with Properties instance passed to configure() merged with properties retrieved from the above map.

So, in Hibernate mappings you'd do something like:

<generator class="com.mypackage.PredefinedGenerator">
  <param name="preset">preset1</param>
</generator>

In your Spring context:

<bean id="preset1" class="com.mypackage.PredefinedGenerator">
  <property name="parameters"><props>
    <prop key="generatorStrategy">seqhilo</prop>
    <prop key="sequence">my_sequence</prop>
    <prop key="max_lo">500</prop>
  </props></property>
</bean>

This can be simplified, of course, if you don't need multiple presets. I can't post the actual code for legal reasons; but if you're interested in this approach but the above isn't clear please comment and I'll try to clarify.

这篇关于如何定义可重用的&lt; generator&gt; Hibernate映射中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 19:45