Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4252376
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:47:13+00:00 2026-05-21T04:47:13+00:00

For simple string field, @Entity class Foo { //1. @Basic(optional = false) //2. @Column(length

  • 0

For simple string field,

@Entity
class Foo {

    //1. @Basic(optional = false)
    //2. @Column(length = 100, nullable = false)
    String name;
}

I need to restrict name’s length using @Column annotation, but I’m confused with the nullable attribute. While I’m using other annotations like @ManyToOne and @OneToMany those use optional attributes, I feel like to use @Basic(optional) to keep most annotations uniform. But I can’t restrict the name’s length with @Basic.

So, where should I annotate the nullable attribute, by @Basic or @Column?

EDIT

Simply say, in which form would you prefer:

Form 1:

@Entity
class Foo {
    @Basic(optional = false)
    @Column(length = 100)
    String name;
}

Form 2:

@Entity
class Foo {
    @Column(length = 100, nullable = false)
    String name;
}

Well personally I like Form 1, because optional attribute is also used by @ManyToOne etc. annotations, but Form 2 is also good because it’s done in single annotation.

EDIT

After read http://markmail.org/message/osod6rsauwbnkvya, I’ve got the difference between @Basic.optional and @Column.nullable. But I still don’t know which one I should use. It seems like good to include both annotations, so make the underlying table well defined, and check null in JPA before the actual update maybe slightly faster.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:47:14+00:00Added an answer on May 21, 2026 at 4:47 am

    From API documentation:

    @Basic:

    @Basic annotation is the simplest type
    of mapping to a database column. The
    Basic annotation can be applied to a
    persistent property or instance
    variable of any of the following
    types: Java primitive types, wrappers
    of the primitive types, String,
    java.math.BigInteger,
    java.math.BigDecimal, java.util.Date,
    java.util.Calendar, java.sql.Date,
    java.sql.Time, java.sql.Timestamp,
    byte[], Byte[], char[], Character[],
    enums, and any other type that
    implements Serializable.

    @Column

    @Column Is used to specify a mapped
    column for a persistent property or
    field. If no Column annotation is
    specified, the default values are
    applied.

    So, if you don’t specify @Column it derives column value from getter/setter.
    If you need to specify column name you have to @Column annotation.

    @Basic allows you to specify Fetch Type. If you want to change default fetching type you have to use this annotation, otherwise you can omit it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple class Role: @Entity @Table (name = ROLE) public class Role
Given a very simple object: class User { private Integer id; private String name;
Using simple field width specifier, the columns are 'thinner' when the string includes 'European'
I defined a simple Document, like this: class Company(Document): screen_name = StringField(max_length=100, required=True, unique=True)
I need to convert a string vector in a simple string. I do not
I have a simple entity, Code, that I need to persist to a MySQL
I have these two class(table) @Entity @Table(name = course) public class Course { @Id
I have the following simple entity: public class Something{ [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public int ID {
I have two entities Room.php /** * @ORM\Table(name=room) * @ORM\Entity(repositoryClass=Ahsio\StackBundle\Repository\RoomRepository) */ class Room {
The model class: @Entity @Table(name = CSU_AUDIT) @NamedQuery(name = findAllEventsForObjectId, query = from AuditEvent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.