使用hibernate的数据库中的Unicode字符

时间:2012-04-11 10:37:01

标签: hibernate

我可以使用hibernate防止某些字符在数据库中持久存在。例如,如果我有一个注意id和注释文本作为2个属性的类Note。我可以防止像FFFE这样的字符Unicode字符在数据库中持久存在。当用户输入像FFFE这样的字符时,它应该替换为AAAA

1 个答案:

答案 0 :(得分:0)

最简单的解决方案是将text属性配置为accessed by property。然后在text属性的getter中,将FFFE替换为AAAA

@Entity
public class Note {

   private Integer id;
   private String text;

   @Id
   public Integer getId(){ }

   public void getText(){
       /**replace() is the function to replace `FFFE` with  `AAAA` ***/
       this.text = replace(inText);
       return  this.text;
   }
}

由于属性访问text属性,hibernate将从其getter获取值保存在DB中。