[英]JPA property java.net.URL
HI, HI,
I have an object which has a piece of URL-Information associated with it. 我有一个对象,它有一段与之相关的URL信息。 Currently I save this URL in a simple String property, but java.net.URL would provide me with additional goodies such as detection of malformed URLs etc. 目前我将此URL保存在一个简单的String属性中,但是java.net.URL会为我提供额外的好处,例如检测格式错误的URL等。
On the other hand I would consider it very ugly, if JPA simply created a LOB for the URL-Object. 另一方面,如果JPA只是为URL-Object创建了一个LOB,我认为它非常难看。 Does anyone know how a property of the type java.net.URL will be persisted to the database by compliant JPA providers? 有谁知道兼容的JPA提供程序如何将java.net.URL类型的属性持久化到数据库?
As per the JPA spec: 根据JPA规范:
The persistent fields or properties of an entity maybe of the following types: Java primitive types; 实体的持久字段或属性可以是以下类型:Java原始类型; java.lang.String; java.lang.String中; other Java serializable types (including wrappers of the primitive types, java.math.BigInteger, java.math.BigDecimal, java.util.Date, java.util.Calendar[7], java.sql.Date, java.sql.Time, java.sql.Timestamp, user-defined serializable types, byte[], Byte[], char[], andCharacter[]); 其他Java可序列化类型(包括基本类型的包装,java.math.BigInteger,java.math.BigDecimal,java.util.Date,java.util.Calendar [7],java.sql.Date,java.sql.Time ,java.sql.Timestamp,user-de fi ned serializable types,byte [],Byte [],char [],andCharacter []); enums; 枚举; entity types and/or collections of entity types; 实体类型和/或实体类型集合; and embeddable classes (see section 2.1.5). 和可嵌入的类(参见第2.1.5节)。
Plus the support for collections. 加上对收藏品的支持。 But no primitive support of URL
. 但没有URL
原始支持。 They would however be supported as Serializable
, which I guess would result in a LOB as you mentioned. 然而,它们将被支持为Serializable
,我想这将导致你提到的LOB。
But you should be able to easily circumvent that: you can have the URL as a String
in a field and a getter/setter that convert from String
to URL
though. 但是你应该能够轻松地避开:你可以在一个字段中将URL作为String
,然后将一个getter / setter从String
转换为URL
。 Then you map the field with the annotation. 然后使用注释映射字段。
Or the opposite: the java.lang.URL
in a field, and getter/setter to convert from URL
to String
, then you map the getter/setter with the annotation. 或者相反:字段中的java.lang.URL
,以及从URL
转换为String
的getter / setter,然后使用注释映射getter / setter。 I think it works as well. 我认为它也有效。
You can also use JPA AttributeConverter
such as explain here to map a URL
object into a String
column (Hibernate does that out of the box) 你也可以使用JPA AttributeConverter
这里解释把URL
对象映射到String
列(Hibernate开箱即用)
Or, if you are only interrested in validation, you may use BeanValidation. 或者,如果您只对验证感兴趣,则可以使用BeanValidation。 There is a @URL
annotation in HibernateValidator extension you can also create your own constraint such as explain here 有一个@URL
在HibernateValidator扩展注释您也可以创建自己的约束,如解释在这里
I personnaly prefer the BeanValidation solution because considering url as java.net.URL
bring some complexity : need for JPA AttributeConverter
, JSF Converter
, JAXB Adapter
... while it is just for validation 我个人更喜欢BeanValidation解决方案,因为考虑到url作为java.net.URL
带来一些复杂性:需要JPA AttributeConverter
,JSF Converter
,JAXB Adapter
...而它只是用于验证
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.