Persistence provider for Java that supports final fields

前端 未结 5 953
梦如初夏
梦如初夏 2021-01-12 00:52

I\'m very new to Java but I\'ve been developing a habit to use final wherever possible declaring immutability which i think is a good thing. (Consider f#)

I\'ve read

5条回答
  •  失恋的感觉
    2021-01-12 01:24

    Object immutability (note the difference between an immutable object, and declaring a field final - an object is ONLY immutable if ALL fields are final, thus the object's state can't change after creation) is a very sensitive topic. I like them myself, and hibernate supports them through @Immutable.

    Don't know about it's status in JPA 2, but to answer the question about final fields: you CAN change their values using reflection - but reflection is severly limited in a Java EE environment.

    To enlighten the main problem: if your POJOs are immutable, then how would a persistent solution recreate the objects? Let's say you have two final int fields, and a constructor to initialize them. The persistence layer cannot have any information about their order, or their names (as field and parameter names are erased during compiling).

    Koshuke posted a blog about this (in relation to JAXB supporting immutable beans), but can't find it right now.

提交回复
热议问题