JAXB - 从 XSD 生成类 - 将枚举转换为除枚举名称之外的自定义 toString()
使用 JAXB,我们直接生成 Java bean。在 XSD 中,我们有一个枚举类型(比如说):
<xs:simpleType name="ColorType">
<xs:restriction base="xs:string">
<xs:enumeration value="Red"/>
<xs:enumeration value="Blue"/>
<xs:enumeration value="Green"/>
</xs:restriction> </xs:simpleType>
在数据库中,我们可能有 R、B 和 G 等标志,分别代表红色、黑色和绿色。在某种程度上,我们的标志只是 1 个字母的单词。我想要枚举,这样...ColorType.Red.toString()等于到R...或类似的东西,例如我可以将 R 链接到它。现在,我正在使用一些条件语句手动检查枚举类型,然后在插入或任何数据库操作时将其转换回字符串。
我想到了一些愚蠢的解决方案来解决这个问题(愚蠢的是,这些解决方案不好) 我认为解决这个问题的一个解决方案是使用
<xs:enumeration value="R">
,但这并没有告诉我什么是 R。
第二个解决方案可以将 ColorType 设置为字符串,但这意味着我的 ColorType 甚至可以是 Z,这不是数据库中的任何颜色,在某种程度上我意味着它将不受限制。 :( ...
有办法解决这个问题吗?
Using JAXB, we generate our Java beans directly. In the XSD, we have an enumerated type(say):
<xs:simpleType name="ColorType">
<xs:restriction base="xs:string">
<xs:enumeration value="Red"/>
<xs:enumeration value="Blue"/>
<xs:enumeration value="Green"/>
</xs:restriction> </xs:simpleType>
In the database, we may have flags like R, B and G for Red, Black and green. In a way, we have flags as just 1 letter word. I want to enum, such that ... ColorType.Red.toString() is equal to R ... or something similar, such that I can link R to it. Right now I am manually checking for enumtype using some conditional statement, and then while inserting or any database operation I am converting back to string.
Some stupid solution which came to my mind to solve this (Stupid as, these solutions are not good)
I think one solution to solve this using
<xs:enumeration value="R">
but this doesn't tells me what is R.
Second solution can be making ColorType as string, but this means my ColorType can be even Z, which is not any color in database, in a way I mean it will be unrestrictive. :( ...
Any way to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 XJB-Binding 来实现此目的,如下所示
这将生成一个包含所需映射的枚举。您可以通过调用生成的 Enum 的
value()
方法来访问该值。 (不要忘记将正确的架构位置放入绑定中,除非您的 XSD 名为 MySchema.xsd)You could use a XJB-Binding for that, like this
This will generate an Enum containing the desired mapping. You can access the value by calling the generated
value()
-Method of the Enum. (Don't forget to put the correct schema location into the binding unless your XSD is called MySchema.xsd)这可能会有所帮助: http://fusesource.com/docs/framework/2.1/jaxws /JAXWSCustomTypeMappingEnum.html
看来我误读了你的问题。我能看到的唯一解决方案是重写生成的类 toString 方法。将其替换为仅返回枚举值的第一个字母的值。这样 RED 将返回 R。
This might help: http://fusesource.com/docs/framework/2.1/jaxws/JAXWSCustomTypeMappingEnum.html
Appears I misread your problem. The only solution I can see is to override the generated class toString method. Replace it with one that only returns the first letter of your Enum value. That way RED will return R.
目前我正在使用中间解决方案。现在我只是使用另一个 ENUM,其中有一个静态方法来返回 XSD 类型枚举。
现在有 1 个 xsd 枚举(生成):
另一个枚举是在 java 中手动实现的:
现在我们可以获取颜色代码插入数据库,而不是一次又一次地编写 if - else 代码来获取颜色代码。使用另一个枚举作为映射器,提供比字符串等自由类型类型更高的限制。
至少这是我现在已经解决的解决方案,不知道是否存在更好的解决方案,如果存在更好的解决方案,请告诉我,这会有很大的帮助:)
For the time being I am using intermediate solution. For now I am just using another ENUM, with a static method in it to return the XSD type enum.
So now have 1 xsd enum (generated) :
Another enum is manually implemented one in java :
Now we can get color code to insert in database, rather than writing if - else code again and again to get color code. Using another enum as mapper, provide restriction than free typed types like string.
At least this is the solution I have worked out for now, don't know if better solution exists, please let me know if something better exists, it will be of great help :)