Using Oracle XMLType column in hibernate

后端 未结 5 643
情话喂你
情话喂你 2020-12-02 13:29

I need to map Oracle XMLType column to hibernate entity class. There is a working (and I think well-known) solution that involves implementing UserType; however

5条回答
  •  旧巷少年郎
    2020-12-02 14:18

    To simplify Celso's answer further, one can avoid creating a custom function by using Oracle's built-in function

    XMLType.createxml(?)

    that can handle NULLs.

    So the following annotations combined with Celso's custom dialect class works well.

        @Lob
        @ColumnTransformer(read = "NVL2(EVENT_DETAILS, (EVENT_DETAILS).getClobVal(), NULL)", write = "XMLType.createxml(?)")
        @Column(name = "EVENT_DETAILS")
        private String details;
    

    You might also have to register the clob as xmltype in your custom dialect. So effectively you will have the following:

    public class OracleDialectExtension extends org.hibernate.dialect.Oracle10gDialect {
        public OracleDialectExtension() {
            super();
            registerColumnType(Types.CLOB, "xmltype");
        }
    
        @Override
        public boolean useInputStreamToInsertBlob() {
            return false;
        }
    }
    

    Ensure to set your custom dialect in your hibernate configuration's session-factory property list:

    
    

提交回复
热议问题