如何在hibernate注释中设置Arraylist 的数据类型?

问题描述:

我想设置Arraylist在保存到数据库时的数据类型。 例如: 我用注释的字符串变量,当它被保存到数据库中,它的数据类型是TEXT如何在hibernate注释中设置Arraylist <String>的数据类型?

@Column(columnDefinition = "TEXT") 
String matKhau; 

所以,我不知道任何方式如ArrayList做同样的事情:

@CollectionOfElements 
//add some annotation to set data type in here 
ArrayList<String> ghiChu = new ArrayList<String>(); 

因为我救越南字符串到数据库中,所以它具有的charset = UTF-8,但是当我尝试了上述这段代码,它保存和数据类型是BLOG,这就是问题。我想将BLOG更改为LONGTEXT,那么我该如何处理这个问题?对不起,我的坏英文。

+0

不要将具体类型作为变量来使用,*绝对*不要在JPA中使用它们;改用'List '代替。大多数JPA提供者将使用动态代理来显着提高性能和功能。 – chrylis

请勿使用@CollectionOfElements(已弃用)。使用JPA的@ElementCollection。您可以使用相同的@Column注释为基本类型集合(如String)指定列定义。

所以这里是一个示例。

@ElementCollection 
@Column(columnDefinition="TEXT") 
List<String> ghiChu = new ArrayList<String>(); 
+0

我试过使用你的指令,但是当我在下面运行这个测试时,它给我一个错误:错误的字符串值:'\ xAC \ xED \ x00 \ x05sr ...'在第1行的列'ghiChu'。测试是: ArrayList string = new ArrayList (); string.add(“这是一个测试,不要给我任何错误PLZ”); TestList test = new TestList(); test.setId(“02”); test.setGhiChu(string); TestListDAO.saveOrUpdate(test); –

我想我通过@Column(columnDefinition =“LONGBLOB”)修改解决了它的成功和在这个类中,我推get方法使用@ElementCollection(targetClass = String.class)和我感到很满意这个。

+0

如果您使用泛型,则不需要指定targetClass。 – Ish

+0

感谢您的帮助