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

Moderator: admin

Post Reply
ericrlessa
Posts: 5
Joined: Thu Oct 17, 2013 9:26 pm
Contact:

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

Post 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."

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Yes, that is correct but Wrapper classes i.e. Integer, Character, etc. are explicitly permitted by the specification.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

kemosabe
Posts: 9
Joined: Fri Oct 09, 2015 3:52 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

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

Post 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.

}


Post Reply

Who is online

Users browsing this forum: No registered users and 30 guests