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 86557
In Process

The Archive Base Latest Questions

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

I’ve got what I think is a simple question. I’ve seen examples both ways.

  • 0

I’ve got what I think is a simple question. I’ve seen examples both ways. The question is – ‘why can’t I place my annotations on the field?’. Let me give you an example….

@Entity @Table(name='widget') public class Widget {  private Integer id;   @Id  @GeneratedValue(strategy=GenerationType.AUTO)  public Integer getId() { return this.id; }  public Integer setId(Integer Id) { this.id = id;} } 

The above code works fine (assuming there’s not a typo in there). When the annotation is placed on the getter of the property everything is perfect.

However, that seems awkward to me. In my mind it’s cleaner to place the annotation on the field, like so —

@Entity @Table(name='widget') public class Widget {  @Id  @GeneratedValue(strategy=GenerationType.AUTO)  private Integer id;   public Integer getId() { return this.id; }  public Integer setId(Integer Id) { this.id = id;} } 

I’ve seen examples of both ways. However, when I run this second example I get the following…

 java.lang.NullPointerException     at com.widget.util.hibernate.HibernateSessionFactory$ThreadLocalSession.initialValue(HibernateSessionFactory.java:25)     at com.widget.util.hibernate.HibernateSessionFactory$ThreadLocalSession.initialValue(HibernateSessionFactory.java:1)     at java.lang.ThreadLocal$ThreadLocalMap.getAfterMiss(Unknown Source)     at java.lang.ThreadLocal$ThreadLocalMap.get(Unknown Source)     at java.lang.ThreadLocal$ThreadLocalMap.access$000(Unknown Source)     at java.lang.ThreadLocal.get(Unknown Source)     at com.widget.util.hibernate.HibernateSessionFactory.get(HibernateSessionFactory.java:33)     at com.widget.db.dao.AbstractDao.(AbstractDao.java:12)     at com.widget.db.dao.WidgetDao.(WidgetDao.java:9)     at com.widget.db.dao.test.WidgetDaoTest.findById(WidgetDaoTest.java:17)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)     at java.lang.reflect.Method.invoke(Unknown Source)     ... 

Here’s the skeleton of HibernateSessionFactory (line 25 is marked) ….

protected Session initialValue() {     SessionFactory sessionFactory = null;     try {         Configuration cfg = new AnnotationConfiguration().configure();         String url = System.getProperty('jdbc.url');         if (url != null) {             cfg.setProperty('hibernate.connection.url', url);         }         sessionFactory = cfg.buildSessionFactory();     }     catch (Exception e) {     }      Session session = sessionFactory.openSession();  // LINE 25     return session; } 

Anyone have an idea what’s going on here?

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

    From a performance and design perspective, using annotations on getters is a better idea than member variables, because the getter setters are called using reflection if placed on the field, than a method. Also if you plan to use validation and other features of hibernate, you’ll have all the annotations at one place, rather than scattering them all over the place.

    My recommendation go with methods not member variables.

    From the documentation

    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 EJB3 annotations in both fields and methods should be avoided. Hibernate will guess the access type from the position of @Id or @EmbeddedId.

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

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Best is to designate one specific process as owning that… May 12, 2026 at 1:03 am
  • Editorial Team
    Editorial Team added an answer There are GNTP (Growl Network Transport Protocol) bindings for various… May 12, 2026 at 1:03 am
  • Editorial Team
    Editorial Team added an answer VerifyGet is enough, assert seems to add no value so… May 12, 2026 at 1:03 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.