Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I see that other comments has misguided you so I feel myself obliged to elaborate on this issue a bit, even though I can't give you a scientific and complete answer. @vcetinick wrote the current accepted answer:</p> <blockquote> <p><em>You <strong>may</strong> find that you <strong>may</strong> be able to get away [..] from the persistence side of things.</em></p> </blockquote> <p>This quote in particular is wrong. All depends on where you put your <code>@Id</code> annotation. The <a href="http://download.oracle.com/otndocs/jcp/ejb-3_0-fr-eval-oth-JSpec/" rel="noreferrer">specification</a> says:</p> <blockquote> <p><em>If the entity has field-based access, the persistence provider runtime accesses instance variables directly.</em></p> </blockquote> <p>Thus you are not required in any way to provide a setter or getter. Because you annotated your <strong>field</strong> and not a getter method (annotating the setter method will be ignored and have no bearing).</p> <p>However, if you write a getter method, and annotated this method with your @Id annotation instead of your field, then we would tell our persistence provider to access our field through the accessor (<em>getter</em>) and mutator (<em>setter</em>) methods and not use reflection. In that case, <strong>both</strong> a getter and a setter should be present. The book <a href="http://rads.stackoverflow.com/amzn/click/1430219564" rel="noreferrer">Pro JPA 2: Mastering the Java™ Persistence API</a> writes on page 71 (<strong>bold</strong> markup by me!):</p> <blockquote> <p><em>When property access mode is used, the same contract as for JavaBeans applies, and there must be getter and setter methods for the persistent properties. The type of property is determined by the return type of the getter method and must be the same as the type of the single parameter passed into the setter method. <strong>Both methods must be either public or protected visibility</strong>.</em></p> </blockquote> <p>Therefore, I usually annotate my id field, and write both a setter and getter method, but the setter method I give protected access. I just don't want any other pieces of code to have easy write access to such an important field. I don't know if this would cause problems in other domains. I'm no expert. But I don't find any rationale either as to why <em>not</em> setup an id attribute in this way. See also the <a href="http://netbeans.org/bugzilla/show_bug.cgi?id=151387" rel="noreferrer">Netbeans forums</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload