java - Best practice and implementation of a builder pattern when using JPA -
i have class suitable builder pattern, there many params , i'd rather not use ton of telescopic constructors.
my problem class jpa entity , new me.
having private final data members throwing error not initialised in constructor , far i'm aware, jpa requires empty protected constructor.
can please? example fantastic, i've included basic example of code below it's generic. i've emoted many of accessors , data members save space/time.
@entity//(name= "table_name") //name of entity / table name public class bean implements serializable { private static final long serialversionuid = 1l; @id //primary key @generatedvalue long id; private final datetime date; private final string title; private final string intro; //used jpa protected bean(){} private bean(bean builder beanbuilder){ this.date = beanbuilder; this.title = beanbuilder; this.intro = beanbuilder; } public datetime getdate() { return date; } public string gettitle() { return title; } public static class beanbuilder builder{ private final datetime date; private final string title; //private optional public beanbuilder(datetime date, string title) { this.date = date; this.title = title; } public beanbuilder intro(string intro){ this.intro = intro; return this; } public beanbuilder solution(string solution){ this.intro = solution; return this; } public bean buildbean(){ return new bean(this); } } }
member fields marked final
must have value assigned during construction , value final (i.e. cannot change). consequence, all declared constructors must assign value all final
fields.
this explain compiler error.
from jls:
a blank final instance variable must assigned @ end of every constructor of class in declared, or compile-time error occurs (§8.8, §16.9).
Comments
Post a Comment