openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael Dick" <michael.d.d...@gmail.com>
Subject Re: JPAAnnotations on the instance variable or on the getter
Date Tue, 15 Apr 2008 02:45:23 GMT
Hi Lars,

Where you put the annotations indicates whether you want to use property or
field based access. If you put the annotations on a get or set method then
the JPA provider will expect property access to be used (ie
PersonNew.setFirstName("John")). If the annotations are on the field then a
JPA provider can expect the fields to be accessed directly (ie
Person.firstName="John"))

In the example below the fields are private which is probably the root
problem when you use field access.

Have you tried making the fields public in the Person class you provided?

hth,
Mike



On Mon, Apr 14, 2008 at 2:04 AM, Lars Vogel <lars.vogel@googlemail.com>
wrote:

> Hi,
>
> my understanding so far was that it doesn't matter if I have the
> annotation on the instance variable or on the the getter method of the
> class.
>
> It that true? Because I have an example where I receive a Null pointer
> exception if I put the annotation on the instance variable and not on the
> getter. Example below. The error occurs during the access of the JobList
> variable. The whole project is attached in case someone would like to have a
> look. For testing just exchange PersonNew.java with Person.java.
>
> Best regards, Lars
>
> ----------
>
> This works:
> ------------------------------
>
> package datamodel.impl;
>
> import java.util.ArrayList;
> import java.util.List;
>
> import javax.persistence.Column;
> import javax.persistence.Entity;
> import javax.persistence.GeneratedValue;
> import javax.persistence.GenerationType;
> import javax.persistence.Id;
> import javax.persistence.OneToMany;
> import javax.persistence.OrderBy;
> import javax.persistence.Transient;
>
> import org.apache.openjpa.persistence.PersistentCollection;
>
> import datamodel.IPerson;
>
> @Entity
> public class PersonNew implements IPerson {
>     private String id;
>     private String firstName;
>     private String lastName;
>
>     private String nonsenseField = "";
>
>     private List<Job> jobList = new ArrayList<Job>();
>
>     private List<String> nickNameList = new ArrayList<String>();
>
>     @Id
>     @GeneratedValue(strategy=GenerationType.SEQUENCE)
>     public String getId() {
>         return id;
>     }
>
>     public void setId(String Id) {
>         this.id = Id;
>     }
>
>
>     public String getFirstName() {
>         return firstName;
>     }
>
>     public void setFirstName(String firstName) {
>         this.firstName = firstName;
>     }
>
>     // Leave the standard column name of the table
>     public String getLastName() {
>         return lastName;
>     }
>
>     public void setLastName(String lastName) {
>         this.lastName = lastName;
>     }
>
>     @Transient
>     public String getNonsenseField() {
>         return nonsenseField;
>     }
>
>     public void setNonsenseField(String nonsenseField) {
>         this.nonsenseField = nonsenseField;
>     }
>
>     @OneToMany
>     public List<Job> getJobsList() {
>         return this.jobList;
>     }
>
>     public void setJobsList(List<Job> nickName) {
>         this.jobList = nickName;
>     }
>
>     @PersistentCollection
>     @OrderBy
>     public List<String> getNickNameList() {
>         return nickNameList;
>     }
>
>     public void setNickNameList(List<String> nickNameString) {
>         this.nickNameList = nickNameString;
>     }
> }
>
>
> This results in an error:
>
> package datamodel.impl;
>
> import java.util.ArrayList;
> import java.util.List;
>
> import javax.persistence.Column;
> import javax.persistence.Entity;
> import javax.persistence.GeneratedValue;
> import javax.persistence.GenerationType;
> import javax.persistence.Id;
> import javax.persistence.OneToMany;
> import javax.persistence.OrderBy;
> import javax.persistence.Transient;
>
> import org.apache.openjpa.persistence.PersistentCollection;
>
> import datamodel.IPerson;
>
> @Entity
> public class Person implements IPerson {
>     @Id
>     @GeneratedValue(strategy=GenerationType.SEQUENCE)
>     private String id;
>     private String firstName;
>     private String lastName;
>
>     @Transient
>     private String nonsenseField = "";
>     @OneToMany
>     private List<Job> jobList = new ArrayList<Job>();
>     @PersistentCollection
>     @OrderBy
>     private List<String> nickNameList = new ArrayList<String>();
>
>
>     public String getId() {
>         return id;
>     }
>
>     public void setId(String Id) {
>         this.id = Id;
>     }
>
>
>     public String getFirstName() {
>         return firstName;
>     }
>
>     public void setFirstName(String firstName) {
>         this.firstName = firstName;
>     }
>
>     // Leave the standard column name of the table
>     public String getLastName() {
>         return lastName;
>     }
>
>     public void setLastName(String lastName) {
>         this.lastName = lastName;
>     }
>
>     public String getNonsenseField() {
>         return nonsenseField;
>     }
>
>     public void setNonsenseField(String nonsenseField) {
>         this.nonsenseField = nonsenseField;
>     }
>
>
>     public List<Job> getJobsList() {
>         return this.jobList;
>     }
>
>     public void setJobsList(List<Job> nickName) {
>         this.jobList = nickName;
>     }
>
>
>     public List<String> getNickNameList() {
>         return nickNameList;
>     }
>
>     public void setNickNameList(List<String> nickNameString) {
>         this.nickNameList = nickNameString;
>     }
> }
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message