JPA属性java.net.URL [英] JPA property java.net.URL
问题描述
HI,
我有一个对象,其中包含一段与之关联的URL信息。
目前我将此URL保存在一个简单的String属性中,但是java.net.URL会为
提供额外的好处,例如检测格式错误的URL等。
I have an object which has a piece of URL-Information associated with it. 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.
另一方面,如果JPA只是为URL-Object创建了一个LOB,我认为它非常难看。有没有人知道java.net.URL类型的属性将如何通过兼容的JPA提供程序持久保存到
数据库?
On the other hand I would consider it very ugly, if JPA simply created a LOB for the URL-Object. Does anyone know how a property of the type java.net.URL will be persisted to the database by compliant JPA providers?
推荐答案
根据JPA规范:
的持久性字段或属性实体可能具有以下类型:
Java原始类型; java.lang.String中;
其他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 []和字符[]);
enums;实体类型和/或
实体类型集合;和
嵌入类(参见
2.1.5节)。
The persistent fields or properties of an entity maybe of the following types: Java primitive types; 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[]); enums; entity types and/or collections of entity types; and embeddable classes (see section 2.1.5).
加上对集合的支持。但没有 URL
的原始支持。然而,它们将被支持为 Serializable
,我想这将导致你提到的LOB。
Plus the support for collections. But no primitive support of URL
. They would however be supported as Serializable
, which I guess would result in a LOB as you mentioned.
但您应该能够轻松规避:您可以在一个字段中将URL作为 String
但是,getter / setter可以从 String
转换为 URL
。然后使用注释映射该字段。
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. Then you map the field with the annotation.
或相反:字段中的 java.lang.URL
,以及从<转换的getter / setter code> URL 到 String
,然后使用注释映射getter / setter。我认为它也有效。
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. I think it works as well.
这篇关于JPA属性java.net.URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!