2010-03-23 56 views
2

我想在我的应用程序中使用自定义序列生成器,但实体位于与其他应用程序共享的域模型jar中。显然,实体注释可以在orm.xml中被覆盖,但我无法弄清楚正确的XML咒语是否可以使其发挥作用。Hibernate:我可以用自定义生成器使用XML覆盖标识符生成器吗?

我可以修改批注在这样这个实体:

@GenericGenerator(name = "MYGEN", strategy = "MyCustomGenerator") 
@GeneratedValue(generator = "MYGEN") 

但我需要这一点是为了替代原有的注释以某种方式映射到orm.xml。看看orm.xml模式here看来,除了“序列”和“表格”之外,我甚至无法指定生成类型。

我应该提到,我正在使用JPA和Hibernate,如果这很重要。

回答

4

你看过hibernate注释文档吗? http://www.redhat.com/docs/manuals/jboss/jboss-eap-4.3/doc/hibernate/Annotations_Reference_Guide/Overriding_metadata_through_XML.html

它解释得很好如何重写的ORM个XML注释配置,

对于〔实施例,考虑这个实体:

@Entity 
@Table(name = "API_USERS") 
public class ApiUser { 

    @Id 
    @Column(name = "ID", unique = true, nullable = false, precision = 6, scale = 0) 
    private Long id; 
    ... 
} 

首先,我使用了序列发生器覆盖ID字段:

<?xml version="1.0" encoding="UTF-8"?> 

<entity-mappings 
    xmlns="http://java.sun.com/xml/ns/persistence/orm" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd" 
    version="1.0"> 
    <entity class="com.muzicall.apiusers.entity.ApiUser" access="FIELD"> 
     <attributes> 
      <id name="id"> 
       <column name="id"/> 
       <generated-value generator="apiUserIdGen" strategy="SEQUENCE"/> 
       <sequence-generator name="apiUserIdGen" sequence-name="api_users_seq" allocation-size="1"/> 
      </id> 
     </attributes> 
    </entity> 
</entity-mappings> 
+0

谢谢,这可能会工作,但我不再在这个项目上工作。 – 2010-05-27 13:38:19

+2

没问题,我只是有同样的问题,花了半天的时间找到解决方案,所以我想我会回答,以便其他人可能会更容易地找到它下一次:) – 2010-05-31 09:22:56