Best practice and implementation of the builder pattern when using JPA

I have a class suitable for the builder pattern, there are many parameters, and I would prefer not to use a ton of telescopic constructors.

My problem is that this class is a JPA entity and it is very new to me.

The presence of closed finite data elements causes an error because I am not initialized in the constructor, and as far as I know, JPA requires an empty protected constructor.

Can anyone help? The example would be fantastic, I included the basic code example below, but it is very general. I saved many members and data members to save space / time.

@Entity//(name= "TABLE_NAME") //name of the 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 by 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); } } } 
+4
source share
5 answers

Custom fields marked as final must have the value assigned at build time, and this value is final (i.e. cannot change). As a result, all declared constructors must assign values ​​to all final fields.

This explains your compiler error.

From JLS :

A pure final instance variable must be definitely assigned at the end of each constructor of the class in which it is declared, or a compile-time error occurs (§8.8, §16.9).

+2
source

I'm not sure what you had in mind for this, but having immutable objects is a great idea when working in Hibernate (not to say you can't, or shouldn't).

Think about it because Hibernate / JPA defines a " state " for the objects they need to modify; otherwise you will have a static database or something like insert-once-and-never-modify database.

An immutable concept is a very well-known concept (now), borrowed mainly from functional programming, which in fact is not applied similarly to OOP. And if you work with Hibernate, you should not have immutable objects ... at least until today.

UPDATE

If you want to have what they call read-only objects, you can use the @Immutable annotation from Hibernate itself. Pay particular attention to collections as members of the organization.

+2
source

Not sure why you want to do this. It might be better to define a member variable as @Column(name = "id", nullable = false, updatable = false) for example

+2
source

JPA 2.1 Specification, Section "2.1. Entity Class", states:

No methods or constant variables of an instance of an entity class can be final.

.. means you cannot build a truly immutable JPA entity. But I really don’t see how this can be such a big problem. Just don't let an entity class publish public setters?

+2
source

Entities must be volatile when it comes to Java's strict immutability. For example, lazily loaded associations will change the state of the object after accessing the association.

If you need to use these entities in real immutable mode (for example, for multi-threaded purposes), consider using DTO (as entities are not intended for access at the same time).

+1
source

All Articles