Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to store null value of an enumerated attribute to database?

I am trying to store null value of an enumerated attribute of an entity in Java to Oracle database using JPA TopLink. The code is executed with no exception or warning, but the entity is not stored in the database.

The entity is defined as follows:

@Entity
public class LetterDoc {
    ...
    @Column(name = "delivery_type", columnDefinition = "VARCHAR2(20)", nullable = true)
    @Enumerated(EnumType.STRING)
    private DocDeliveryTypeEnum deliveryType;
    ...
}

The Enum class DocDeliveryTypeEnum have only required values.

public enum DocDeliveryTypeEnum {
    NORMAL,
    RECOMMENDED,
    ACKNOWLEDGEMENT
}  

In my situation, I have instances of entities where the deliveryType makes sence and is required. However, for some cases, the deliveryType is irrelevant and I want to leave it null. I don't want to set a default value as there is no adequate value for this case.

The entity instance has the value set to null, but when I try to persist and flush the instance in the database, it is just not stored, although the column is defined as VARCHAR2(20) with NULL value enabled.

The situation is something like this:

LetterDoc let = new LetterDoc();
// settery ostatnich atributu
let.setDeliveryType(null);
try {
    emptyDocDAO.persist(let);
    emptyDocDAO.flush();
} catch (Exception e) {
    // no exception is really catched :(
    log.error(e);
}
// here I have the id of the entity

Really no exception is thrown while persisting, just the entity is not stored. When I try to get the entity later in another thread by the ID, it fails with NoResultException.

Is there any chance to make this work, or do I really have to set a value for all enumerated attributes before persisting? Do I really have to create a default value in the Enum class?

I have solved my problem by setting a default value that does not make much sense in the particular situation. However, I wonder if there is any other option.

Thank you very much for your experience and advice.

like image 894
Sharg Avatar asked Jul 11 '12 09:07

Sharg


People also ask

Can enum value be null?

An enum can be null. When an enum (like Color in this program) is a field of a class, it is by default set to null.

Can enum be null in mysql?

If an ENUM column is declared to permit NULL , the NULL value is a valid value for the column, and the default value is NULL . If an ENUM column is declared NOT NULL , its default value is the first element of the list of permitted values.

What is enum in database?

Enumerated (enum) types are data types that comprise a static, ordered set of values. They are equivalent to the enum types supported in a number of programming languages. An example of an enum type might be the days of the week, or a set of status values for a piece of data.

What is an enum value?

An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type.


2 Answers

Yes, you can store null for a enum attribute.

I can't see how you could just get nothing, is an exception occurring that your are not catching?

Enable logging on finest and include the log and your code, and catch any exceptions and include the stack trace.

like image 53
James Avatar answered Oct 05 '22 17:10

James


The code should throw NullPointerException as you are trying to save null value to enum. The Enum.valueOf() method throws NullPointerException if name is null.

let.setDeliveryType(null);

like image 34
punita.gosar Avatar answered Oct 05 '22 17:10

punita.gosar