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

  • SEARCH
  • Home
  • 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 945211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:43:03+00:00 2026-05-15T22:43:03+00:00

Where do i put my hibernate annotations? Is it the line above my instance

  • 0

Where do i put my hibernate annotations?

Is it the line above my instance variable? Or before the getter? Or before the setter? Or doesn’t it really matter?

Thanks a lot

  • 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-15T22:43:04+00:00Added an answer on May 15, 2026 at 10:43 pm

    You place them either on the field or on the getter. From the Hibernate Annotations Reference Guide:

    2.2.1. Marking a POJO as persistent entity

    (…)

    Depending on whether you annotate
    fields or methods, the access type
    used by Hibernate will be field or
    property. The EJB3 spec requires that
    you declare annotations on the element
    type that will be accessed, i.e. the
    getter method if you use property
    access, the field if you use field
    access. Mixing annotations in both
    fields and methods should be avoided.
    Hibernate will guess the access type
    from the position of @Id or
    @EmbeddedId.

    You might also want to read about the @Access annotation that allows to force/override the access type (prior to Hibernate Annotations 3.5 and JPA 2.0, it was part of Hibernate Annotation Extensions):

    2.2.2.2. Access type

    By default the access type of a class
    hierarchy is defined by the position
    of the @Id or @EmbeddedId annotations.
    If these annotations are on a field,
    then only fields are considered for
    persistence and the state is accessed
    via the field. If there annotations
    are on a getter, then only the getters
    are considered for persistence and the
    state is accessed via the
    getter/setter. That works well in
    practice and is the recommended
    approach.

    Note

    The placement of annotations within a class hierarchy has to be consistent
    (either field or on property) to be
    able to determine the default access
    type. It is recommended to stick to
    one single annotation placement
    strategy throughout your whole
    application.

    However in some situations, you need
    to:

    • force the access type of the entity hierarchy
    • override the access type of a specific entity in the class hierarchy
    • override the access type of an embeddable type

    The best use case is an embeddable
    class used by several entities that
    might not use the same access type. In
    this case it is better to force the
    access type at the embeddable class
    level.

    (…)

    Regarding the pros and cons of both styles, I suggest to read the following questions:

    • Hibernate/JPA – annotating bean methods vs fields
    • Hibernate Annotations – Which is better, field or property access?
    • Performance difference between annotating fields or getter methods in Hibernate / JPA
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.