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
  • 33,847
  • 91
  • 226
  • 299
  • 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
  • 68,356
  • 14
  • 108
  • 175
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,678
  • 5
  • 79
  • 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
  • 588,226
  • 146
  • 1,060
  • 1,140
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
  • 792
  • 1
  • 6
  • 19