Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can Locale object be stored in database with hibernate

I have following entity:

@RooEntity
Class X {
    @NotNull
    private Locale locale;
}

Is it possible to store toString() representation of Locale object in database and when retrieving I can still get Locale object?

Can I use @Basic annotation here?

like image 820
hrishikeshp19 Avatar asked Dec 04 '22 05:12

hrishikeshp19


1 Answers

Yes, it can be stored without any additional configuration. Hibernate has built-in basic type LocalType that converts java.util.Locale to jdbc VARCHAR type and vice versa.

Example:

...
import java.util.Locale;


@Entity
@Table(name = "user")
public class User {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;     

   @Basic(optional = false)
   private Locale locale;
    
   // getters and setters

}

SQL (tested on MySQL):

create table user
(
  id                bigint auto_increment primary key,
  locale            varchar(5)   not null
);

Result: data stored in Java locale format: "en_US"

like image 65
Alexander Avatar answered Dec 06 '22 20:12

Alexander