简体   繁体   English

Hibernate - 如何通过Hibernate将java.net.URL存储到数据库中

[英]Hibernate - How to store java.net.URL into a database through Hibernate

I have a field URL countryURL; 我有一个字段URL countryURL; in a Country class. Country课程中。 I want to store its data into a COUNTRY table in a database through Hibernate. 我想通过Hibernate将其数据存储到数据库中的COUNTRY表中。

Which Hibernate type I should use in the hibernate mapping file 我应该在hibernate映射文件中使用哪种Hibernate type

<property column="COUNTRY_URL" name="countryURL" type="..."/>

It is not excepting string and text type. 它不是stringtext类型。

You could write your own user type, but it might be easier to perform the conversion String <-> Url in property getters and setters: 您可以编写自己的用户类型,但在属性getter和setter中执行转换String < - > Url可能更容易:

private void setRawUrl(String s) {
    this.url = new Url(s);
}

private String getRawUrl() {
    return url.toString();
}

and map the property rawUrl with type string . 并使用类型string映射属性rawUrl

You can create a org.hibernate.UserType for URL which maps to a varchar column. 您可以为映射到varchar列的URL创建org.hibernate.UserType See Custom value types in the reference documentation. 请参阅参考文档中的自定义值类型

似乎现在有一些时候由Hibernate自己提供的专用基本值类型: org.hibernate.type.UrlType

I'd store it as String . 我将它存储为String No need to make your life harder by implementing a custom type. 无需通过实现自定义类型来改变您的生活。 That is, change URL to String in Country . 也就是说,将URL更改为Country String

没有..存储为字符串做转换我们的自我是错误的OOAD方法..一旦它是一个字符串用户总是可以给任何字符串,这可能不是URL ..更好地使用自定义T

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

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