Oracle 10g automatically converts empty strings to NULL. So if you execute:
SELECT NVL('', 'Input string converted to NULL') FROM dual;
the result is 'Input string converted to NULL', not ''. I think this is relevant to the problem.
When Hibernate reads a record where the TIN value is 9 spaces (or any number of spaces) with no other character, it stores the value in memory as ''. Hibernate then seems to trick itself into thinking, so to speak, that the value has changed from 9 spaces to an empty string. Then if the record is written back to the database, Hibernate tries to to write an empty string rather than 9 spaces and Oracle apparently converts this to null and then throws a not-null constraint violation.
供参考,这是该列的HBM:
<property
name="tin"
type="java.lang.String"
column="TIN"
not-null="true"
length="9">
我的问题是,如何指示Hibernate不将仅包含空格的值转换为空字符串?更新1:我刚刚回去测试了像“ text”这样的字符串,并发现Hibernate也修剪了这些空格,使字符串为“ text”,并自欺欺人地认为值已更改。我一定是错过了什么。这似乎不是默认行为。更新2:看起来将HBM转换为Java源代码的hbm2java Ant任务可能是String修剪的源头。仍在调查中。谢谢,Jeff。编辑:更改问题措辞以更加精确。