简体   繁体   English

Hyperjaxb3:从枚举元素创建查找表

[英]Hyperjaxb3: Create lookup table from enumeration element

I have an xsd file which includes an element with an enumeration constraint: 我有一个xsd文件,其中包含带有枚举约束的元素:

<xs:complexType name="Request">
    <xs:sequence>
        <xs:element name="CommsAddress" type="xs:string" />
        <xs:element name="CommsAddressType">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="EMAIL"/>
                    <xs:enumeration value="PHONE"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        ...

I would like the CommsAddressType field in the generated Java class to be a generated enum with the values EMAIL and PHONE . 我希望生成的Java类中的CommsAddressType字段是带有值EMAILPHONE的生成的枚举。 In turn, I would like Hibernate to automatically generate my database schema with a CommsAddressType table containing two rows with the values EMAIL and PHONE . 反过来,我希望Hibernate使用CommsAddressType表自动生成数据库模式,该表包含两行,值EMAILPHONE The Request table can then simply reference these with a CommsAddressTypeId column. 然后, Request表可以使用CommsAddressTypeId列简单地引用它们。

Currently, Hyperjaxb3 generates my Request class with a CommsAddressType field of type String : 目前,Hyperjaxb3使用类型为StringCommsAddressType字段生成我的Request类:

@XmlElement(name="CommsAddressType")
protected String commsAddressType

and the schema is generated so that the Request table has a CommsAddressType column of type VARCHAR . 并生成架构,以便Request表具有VARCHAR类型的CommsAddressType列。 This will obviously result in a lot of unnecessary duplicated data. 显然,这将导致大量不必要的重复数据。

Is there any way of achieving what I described above? 有什么方法可以实现我上面描述的内容? Also, as I am exposing the xsd to my customer, I would like to avoid including any jaxb or hyperjaxb tags in the schema if possible. 另外,当我向客户展示xsd时,如果可能的话,我希望避免在模式中包括任何jaxb或hyperjaxb标记。

Disclaimer: I'm the author of Hyperjaxb . 免责声明:我是Hyperjaxb的作者。

If you make your enumerated types to become enums in Java, Hyperjaxb will handle them as enums, not as strings. 如果您使枚举类型成为Java中的枚举,Hyperjaxb将把它们作为枚举而不是字符串处理。 For this you can, for instance, customize your simple type with: 为此,例如,您可以使用以下方法自定义简单类型:

<jaxb:typesafeEnumClass/>

See this example . 请参阅此示例

Hyperjaxb will map corrsponding properties as @Enumerated . Hyperjaxb会将对应的属性映射为@Enumerated

No, you can't generate a lookup table but you can customize how the enum is mapped in the database. 不,您不能生成查找表,但是可以自定义枚举在数据库中的映射方式。 For instance, you can make it mapped as EnumType.ORDINAL : 例如,您可以将其映射为EnumType.ORDINAL

<hj:basic><orm:enumerated>ORDINAL</orm:enumerated></hj:basic>

Example . 例子

Actually, with enums you don't really need lookup tables for JPA. 实际上,使用枚举,您实际上并不需要JPA查找表。 JPA will map enums correctly according to the speified EnumType mapping. JPA将根据指定的EnumType映射正确地映射枚举。

But you may need a lookup table for your own purposes - like being able to produce human-readable values in joined queries. 但是您可能需要一个用于您自己目的的查找表-例如能够在联接的查询中产生易于理解的值。

In this case I'd recommend mapping the enum as ORDINAL and adding a lookup table plus corresponding foreign keys on your own DDL scripts. 在这种情况下,我建议将枚举映射为ORDINAL ,并在您自己的DDL脚本上添加查找表以及相应的外键。

And yes, you can do all of the described customizations in a separate bindings file (normally bindings.xjb ), you don't have to do it directly in the schema. 是的,您可以在单独的绑定文件(通常为bindings.xjb )中进行所有描述的自定义操作,而不必在架构中直接进行。 See test projects here , there is plenty of examples. 在此处查看测试项目, 这里有很多示例。 Just look for *.xjb files in src/main/resources directories. 只需在src/main/resources目录中查找*.xjb文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM