Page 1 of 1

About Question enthuware.oce-jpad.v6.2.517 :

Posted: Sun Oct 27, 2013 10:29 am
by ericrlessa
Option 1 don't is right too?

The Integer class don't have public no-arg constructor.

Specification:

"The primary key class must be public and must have a public no-arg constructor."

Re: About Question enthuware.oce-jpad.v6.2.517 :

Posted: Sat Nov 02, 2013 7:45 am
by admin
Yes, that is correct but Wrapper classes i.e. Integer, Character, etc. are explicitly permitted by the specification.

HTH,
Paul.

Re: About Question enthuware.oce-jpad.v6.2.517 :

Posted: Sun Feb 26, 2017 10:14 am
by kemosabe
What about for property access? In this case, setter methods (including for primary keys) must be provided, and are used by the JPA implementation.

Or is that what you meant by "in general" on option 5? Should property access therefore be avoided?

Re: About Question enthuware.oce-jpad.v6.2.517 :

Posted: Sun Feb 26, 2017 11:00 am
by admin
No, there is no need to avoid accessor methods.
The goal is to prevent other classes from changing the primary key. If you are using accessor methods instead of fields, a good way is to avoid making them public or even protected. This will prevent unintentional access from other classes and will also allow the container to manage those fields through the accessors.

HTH,
Paul.

Re: About Question enthuware.oce-jpad.v6.2.517 :

Posted: Sat Jul 08, 2017 7:38 pm
by himaiMinh
So, to set a composite primary key, it should be like this example from JPA Pro 2?

Code: Select all

@Entity
@IdClass(EmployeeId.class)
public class Employee{
@Id private int id;
@Id private String country;

 public void setId(int id) {....}
 public void setCountry(String country){...}
      
}
}

Code: Select all

public class EmployeeId implements Serializable{
private int id;
private String country;
  ...
  
//.... we can provide get methods for these two attributes, but we should not provide set methods for them.

}