Mapping a FunctionalJava Option with Hibernate

后端 未结 3 1771
萌比男神i
萌比男神i 2020-12-19 08:46

I have a hibernate-mapped Java object, JKL, which is full of a bunch of normal hibernate-mappable fields (like strings and integers).

I\'m added a new e

3条回答
  •  庸人自扰
    2020-12-19 09:09

    I would suggest introducing FunctionalJava's Option in the accessors (getter and setter), while leaving Hibernate to handle a simple java field which is allowed to be null.

    For example, for an optional Integer field:

    // SQL
    CREATE TABLE `JKL` (
        `JKL_ID` INTEGER PRIMARY KEY,
        `MY_FIELD` INTEGER DEFAULT NULL
    )
    

    You can map a Hibernate private field directly:

    // Java
    @Column(nullable = true)
    private Integer myField;
    

    You could then introduce Option at the accessor boundary:

    // Java
    public fj.data.Option getMyField() {
        return fj.data.Option.fromNull(myField);
    }
    
    public void setMyField(fj.data.Option value) {
        myField = value.toNull();
    }
    

    Does that work for your needs?

提交回复
热议问题