Should I use primitives or wrappers in JPA2.0?

I saw this question in SO, which usually leads to Primitives, and also saw this from coderanch , which usually leads to a wrapper. Both are also a bit old.

I have no special needs, just want to know the standard good practice.

Examples on the Internet are also mixed. for example, some of them look like this:

@Id @Column(name = "CUSTOMER_ID") public long customerId; 

Others with Wrappers:

 @Id @Column(name = "CUSTOMER_ID") public long customerId; 
+6
source share
5 answers

The difference between these two values ​​is invalid. a primitive type cannot be null, and a Wrapped type can be null.

I prefer to use a wrapped type, as you can see if the object was saved / loaded to / from the database, regardless of whether id is null.

I don’t think there is a “best practice” here, maybe a style question?

+7
source

Hibernate recommends you:

We recommend that you declare the properties of an identifier with the same name on constant classes and that you use a NULL (i.e., non-primitive) type. more details

+3
source

If you use primitives, it will always have a default value, in this case 0L for a long time, even if this value is not in the database. And if you use a wrapper object, it will have a null value if the value is not in the database or the object is not yet saved.

+1
source

I think the answer is included in the nullable element in the @Column annotation. If it can be null than a wrapped primitive, this is normal. But with nullable = false columns (as ID) primitives are better. You will get extra validation because null cannot be passed int / long.

0
source

From the standpoint of sleep mode, this does not change anything, since Hibernate uses the same type of Hibernate to represent them.

However, as Bytecode Ninja points out, you cannot distinguish the default value of a primitive int 0 from the assigned 0, while there is no ambiguity with zero (a null identifier always means a new object), so I prefer to use a nullable shell type.

And this is the recommendation of Hibernate. From the reference documentation:

4.1.2. Specify an identifier property (optional)

Cat has a property called id. This property maps to the primary key column of the database table. A property could be called by anything, and its type could be any primitive type, any primitive wrapper type, java.lang.String or java.util.Date. If the legacy database table contains compound keys, you can use a custom class with the properties of these types (see the section on compound identifiers later in the chapter.)

The identifier property is strictly optional. You can leave them and let Hibernate track object identifiers within the company. However, we do not recommend this.

In fact, some functions are only available for classes that declare an identifier property:

Transitive re-binding for individual objects (cascading updates or merging cascades) - see section 10.11 "Transitive persistence" Session.saveOrUpdate () Session.merge () We recommend that you declare identifier properties with the same name in constant classes and use a type with a null value (i.e. Not primitive).

And I really use this in my base class:

 @MappedSuperclass public class BaseEntity implements Serializable { private static final long serialVersionUID = 1L; private Long id; @Id @GeneratedValue(strategy = GenerationType.AUTO) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Transient public boolean isNew() { return (this.id == null); } } 

Please check more details here: https://stackoverflow.com/posts/3537407/edit

0
source

Source: https://habr.com/ru/post/907681/


All Articles