×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT技术讨论 / EJB. What's the difference between compound primary key and single-field key? Why cannot the primary keys be primitive types in single-field key? I read some books but can't get the answer. thank you!
    • Here you go...
      Whether you use single-fielded or multi-fielded primary key id determined by your business data.

      Wrapper classes (Integer, Boolean...) cannot be used as primary key because their sole instance variable is PRIVATE. (Wrapper classes follow the immutable pattern.) The instance variables of a primary key must be PUBLIC so that the EJB container can access them directly.
      To say, you have a primary class as follows:

      public class PK implements java.io.Serializable{

      public PK(id) {
      this.id=id;
      }
      public int id;
      }

      You should be abale to write the following code:

      PK pk= new PK(4);
      pk.id=123;

      Note the syntax above that directly accesses id.