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?
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With