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