openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adam Hardy <adam....@cyberspaceroad.com>
Subject Re: enum = null becomes blank string in db
Date Sun, 05 Apr 2009 23:10:27 GMT
OK after a bit of cursory testing, it seems that putting the nullable=false 
attribute on the column metadata prevents the situation.

Do you consider this a bug? Do you want a bug report?


Adam Hardy on 04/04/09 12:39, wrote:
> OpenJPA is not persisting null as you had thought, it is changing the 
> null value to an empty string.
> 
> I also realised I wrote some logic into the enum which might be causing 
> the problem, since I stripped the logic out and it now throws an 
> 'ArgumentException: Field 
> "org.permacode.patternrepo.domain.entity.TestRun.timeFrame" of 
> "org.permacode.patternrepo.domain.entity.TestRun@a82a10" can not be set 
> to "null" value.' So that is good.
> 
> However I don't know why my logic in the enum should affect this. Here's 
> what I've got:
> 
> public enum PatternRepoTimeFrame
> {
>     NOT_USED("NOT USED"),
>     DAILY("DAILY");
>     private String period;
> 
>     private PatternRepoTimeFrame(String newPeriod)
>     {
>         this.period = newPeriod;
>     }
> 
>     public static PatternRepoTimeFrame get(String whichPeriod)
>     {
>         for (PatternRepoTimeFrame value : PatternRepoTimeFrame.values())
>         {
>             if (value.period.equals(whichPeriod))
>             {
>                 return value;
>             }
>         }
>         return NOT_USED;
>     }
> }
> 
> 
> 
> 
> Adam Hardy on 04/04/09 12:24, wrote:
>> I followed your suggestion and placed the 'nullable' attribute on the 
>> column, but it had no impact.
>>
>> I'm having problems grasping the logic behind that nullable attribute.
>>
>> If the column is not nullable and I also configure JPA with 
>> nullable=false, then surely OpenJPA should throw an exception before 
>> it tries to execute a prepared statement with that field set to null?
>>
>> Failing that, OpenJPA should try to persist the entity with that 
>> column = null, but the database should throw a constraint violation 
>> exception.
>>
>> However in my case here, I don't see either behaviour - what I am 
>> seeing is the value of the data changed from null to an empty string.
>>
>> Surely a bug?
>>
>>
>>
>> Michael Dick on 03/04/09 21:52, wrote:
>>> Have you tried the following ?
>>>
>>> <basic name="numericDisplay">
>>>      <column name="NUMERIC_DISPLAY" nullable="false"/>
>>>      <enumerated>STRING</enumerated>
>>> </basic>
>>>
>>> By default OpenJPA doesn't check for constraints on your columns. So 
>>> if your
>>> mappings (or annotations) aren't consistent with the constraints in the
>>> database you can run into problems.
>>>
>>> Alternatively you can configure OpenJPA to read the data from the 
>>> database
>>> by adding this property :
>>> <property name="openjpa.jdbc.SchemaFactory" value="native"/>
>>>
>>> If you've tried either of those and we're still persisting a null 
>>> value then
>>> it's definitely a bug.
>>>
>>> -mike
>>>
>>> On Fri, Apr 3, 2009 at 1:51 PM, Craig L Russell 
>>> <Craig.Russell@sun.com>wrote:
>>>
>>>> Hi Adam,
>>>>
>>>> Sounds like a bug. Can you please file a JIRA?
>>>>
>>>> Thanks,
>>>>
>>>> Craig
>>>>
>>>>
>>>> On Apr 3, 2009, at 9:26 AM, Adam Hardy wrote:
>>>>
>>>>  Just tested this with static enhancement against mysql and have the 
>>>> same
>>>>> problem. OpenJPA is inserting a blank string into the not-null 
>>>>> field when
>>>>> the the enum variable is null.
>>>>>
>>>>> Is this a bug or to be expected?
>>>>>
>>>>> Regards
>>>>> Adam
>>>>>
>>>>> Adam Hardy on 01/04/09 17:38, wrote:
>>>>>
>>>>>> I have an entity bean with this property in v1.2.0 and H2 db:
>>>>>> <basic name="numericDisplay">
>>>>>>  <column name="NUMERIC_DISPLAY"/>
>>>>>>  <enumerated>STRING</enumerated>
>>>>>> </basic>
>>>>>> I just discovered that I can set the property on the bean to null
and
>>>>>> save it to a field in the DB with a not-null constraint. It saves
a
>>>>>> zero-length string.
>>>>>> On reading back the row however OpenJPA throws this:
>>>>>> <openjpa-1.2.0-r422266:683325 nonfatal general error>
>>>>>> org.apache.openjpa.persistence.PersistenceException: No enum const

>>>>>> class
>>>>>> org.permacode.patternrepo.PatternRepoNumericDisplay.
>>>>>> Surely this is inconsistent? Shouldn't I get an error when trying

>>>>>> to do
>>>>>> the write first of all?
>>>>>> Admittedly I have yet to test it with pre-enhanced beans but I 
>>>>>> figured it
>>>>>> would be the same (or is that a completely different code base?)



Mime
View raw message