Java Persistence / JPA: @Column vs @Basic

JavaJpaPersistence

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

  • @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.

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHosam AlyView Question on Stackoverflow
Solution 1 - JavadjnaView Answer on Stackoverflow
Solution 2 - JavaHosam AlyView Answer on Stackoverflow
Solution 3 - JavaGabView Answer on Stackoverflow
Solution 4 - JavaSonu patelView Answer on Stackoverflow