ChatGPT解决这个技术问题 Extra ChatGPT

Java Persistence / JPA: @Column vs @Basic

What is the difference between @Column and @Basic annotations in JPA? Can they be used together? Should they be used together? Or does one of them suffice?


A
AdrieanKhisbe

@Basic signifies that an attribute is to be persisted and a standard mapping is to be used. It has parameters which allow you to specify whether the attribute is to be lazily loaded and whether it's nullable.

@Column allows you to specify the name of the column in the database to which the attribute is to be persisted.

If you specify one without the other then you get default behaviour which is sensible, so commonly folks use only one with the exception of special cases.

So if we wanted a lazy loading of an attribute and to specify a column name we can say

 @Basic(fetch=FetchType.LAZY)
 @Column(name="WIBBLE")

If we neeed the default, non-lazy behaviour then just the @Column would have been sufficient.


This is a clear answer. Thank you. So I assume one can use @Basic without @Column, which is why the optional and nullable properties exist in both. Am I right?
I don't claim to fully understand the JPA spec on the differences between @Basic's nullable and @Column's nullable and the effect of specifying neither, either or both. @Basic nullable is described as a "hint" with relevence to schema generation. @Column nullable is described as defining the characteristics of the column in the database. My practice would be to use the @Column case.
@Basic(optional) is (should be) checked on runtime by persistence provider before saving to DB. @Column is a column definition in database and is used for schema generation : stackoverflow.com/questions/2899073/…
that 'non-lazy', calls EAGER. LOL
The JPA annotations can be divided in two groups: the pure relational annotations versus the implementation annotations. The pure relational annotations contain: Entity, Basic, OneToOne, OneToMany, ManyToMany, ... while the implementation annotations give clues on how to map the pure theoretical model to a real database: Table, Column, ... In your question 'Basic' is an annotation to provide information about the relational entity attribute while 'Column' provides information on how to map it to a database column.
H
Hosam Aly

In addition to @djna's answer, it is worth noting that @Basic should be compared with @OneToMany, @ManyToOne and @ManyToMany. Only one of these can be specified on any property.

@Column and @JoinColumn can be specified along with any of these to describe the database column properties.

These are two sets of annotations that can be used together, but only one annotation of each set can be used at a time.


C
Community

It is worth noting that Basic is designed for primitive fields

http://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes

A basic attribute is one where the attribute class is a simple type such as String, Number, Date or a primitive. A basic attribute's value can map directly to the column value in the database. The types and conversions supported depend on the JPA implementation and database platform. Any basic attribute using a type that does not map directly to a database type can be serialized to a binary database type. The easiest way to map a basic attribute in JPA is to do nothing. Any attributes that have no other annotations and do not reference other entities will be automatically mapped as basic, and even serialized if not a basic type. The column name for the attribute will be defaulted, named the same as the attribute name, as uppercase.


No, If no Column annotation is specified, the default values apply (name will be the attribute one).
S
Sonu patel

The @Basic annotation are applied to JPA entities, and the of @Column are applied to the database columns @Basic annotation's optional attribute defines whether the entity field can be null or not; on the other hand,

@Column annotation's nullable attribute specifies whether the corresponding database column can be null

We can use @Basic to indicate that a field should be lazily loaded

The @Column annotation allows us to specify the name of the mapped database column

@Basic annotation marks the property as not optional on the Java object level. And (nullable = false) on the column mapping, is only responsible for the generation of a NOT NULL database constraint.