2

I have a field URL countryURL; in a Country class. I want to store its data into a COUNTRY table in a database through Hibernate.

Which Hibernate type I should use in the hibernate mapping file

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

It is not excepting string and text type.

Amit
  • 32,630
  • 90
  • 219
  • 295
  • Possible dup: http://stackoverflow.com/questions/2355260/jpa-property-java-net-url/2355298#2355298 – ewernli Mar 13 '10 at 13:14

5 Answers5

5

You could write your own user type, but it might be easier to perform the conversion String<->Url in property getters and setters:

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

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

and map the property rawUrl with type string.

meriton
  • 65,263
  • 14
  • 100
  • 169
5

You can create a org.hibernate.UserType for URL which maps to a varchar column. See Custom value types in the reference documentation.

Lachlan Roche
  • 25,340
  • 5
  • 80
  • 77
4

it seems that there is for some times now a dedicated basic value type provided by Hibernate himself : org.hibernate.type.UrlType

mtlx
  • 218
  • 2
  • 8
0

I'd store it as String. No need to make your life harder by implementing a custom type. That is, change URL to Stringin Country.

Bozho
  • 572,413
  • 138
  • 1,043
  • 1,132
0

Nope.. storing as String doing conversion our self is wrong by OOAD approach.. Once it is a string user can always give any string, which may not be URL.. better to use Custom T

Raja Nagendra Kumar
  • 762
  • 1
  • 6
  • 19