Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Saving enum name in native query

Is it possible with the entity manager to save the name of the enum without using the name method? I need it because sometimes my enum will be null and then the name method would return a nullpointer. Now I have to do this:

entityManager.createNativeQuery(
                "INSERT INTO car(enum_column) " +
                        "VALUES(enumColumn)")
        .unwrap(NativeQuery.class)
        .setParameter("enumColumn", entity.getEnumColumn.name)
        .executeUpdate();

i need to do this something like this and set enum name:

entityManager.createNativeQuery(
                "INSERT INTO car(enum_column) " +
                        "VALUES(enumColumn)")
        .unwrap(NativeQuery.class)
        .setParameter("enumColumn", entity.getEnumColumn)
        .executeUpdate();

unfortunelly, now result is : \xaced00057e72002c636f6dffddffd...

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The easiest one, still quite readable:

entityManager.createNativeQuery(
    "INSERT INTO car(enum_column) " +
    "VALUES(:enumColumn)")
    .unwrap(NativeQuery.class)
    .setParameter("enumColumn", entity.getEnumColumn() == null ? null : entity.getEnumColumn().name())
    .executeUpdate();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading