• Post Reply Bookmark Topic Watch Topic
  • New Topic

Primitive primary key  RSS feed

 
Edmund Yong
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For an entity bean, can the primary key be a primitive, say an integer?
If yes, then if the ejbFind... method in the bean class needs to return a Collection, what should it do with the integer values? Wrap each integer in an Integer class?
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you need to use the wrapper classes, i.e.
<prim-key-class>java.lang.Integer</prim-key-class>
instead of
<prim-key-class>int</prim-key-class>
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edmund Yong:
[QB]For an entity bean, can the primary key be a primitive, say an integer?[/i]

No, the primary key must be a first-class object (Integer instead of int, Long instead of long). This is a requirement because the container must be able to call hashCode() and equals(Object) on the PKs. Also, int doesn't have a null value, which is necessary for foreign keys that allow null (0..1).
 
Edmund Yong
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to the both of you!
By the way, what is first-class object?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edmund Yong:
By the way, what is first-class object?

A first-class object just means that in Java it extends java.lang.Object (I'm pretty sure I'm using the right phrase for this, but it's been a while since I've "studied" OO in an academic setting). In Smalltalk, for example, everything is a first-class object -- there are no primitives ints or doubles.
 
Edmund Yong
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A first-class object just means that in Java it extends java.lang.Object (I'm pretty sure I'm using the right phrase for this, but it's been a while since I've "studied" OO in an academic setting). In Smalltalk, for example, everything is a first-class object -- there are no primitives ints or doubles.

Is it that only a direct subclass of java.lang.Object can be a primary key class?
 
shyam jakki
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Young
For an entity bean, can the primary key be a primitive, say an integer?
It should be an Integer not java primitive int type.An Integer should be returned for two reasons.First CMP 2.0 intoduces Container Mangement Relationships.This is a way letting an EJB container manage relationships between ENtity beans.When these relationships are used,the container is responsible for generating additional classes to handle them,similar to a container generating implementation classes for your CMP beans.When these classes are generated ,the first is the primary key value on an entiy bean is stored as an JAVA Object (i.e such as java.lang.Integer) ,and not the primitive type int.
If u are using primary key as composite key,a seperate class for the primary key must be specified.The class must be a simple Java calss that imlements the Serializable interface with the attributes that define the composite key for the enity bean.The attribute names and types int he primary key class must match those int the Entity bean,and also must be declared as public in both the bean implementation calss and the primary key class.
The Primary key class must implement the optional Java.lan.Object methods ,such as equals() and hashCode().

So better to use Integer instead of int when dealing with primary keys.
bye
shyam
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edmund Yong:
Is it that only a direct subclass of java.lang.Object can be a primary key class?

Sorry, I didn't explain that very clearly. A first-class object -- in Java -- is anything that has java.lang.Object as its root superclass. Really, anything that is instantiable. Thus int and long are not yet Integer and Long and HashMap and File are. If you can assign an instance of it to a variable declared as Object, it's a first-class object.
You cannot do "int i = new Integer();" nor "Long l = 5L;", but you can do "Object o = new File("foo.txt");". As ejbCreate returns the primary key of the newly created entity bean, and it is declared to return java.lang.Object, you cannot use int nor long as a primary key. You can, however, use Integer and Long or your own class.
 
Edmund Yong
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David and Shyam, I got it now.
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't Can someone show sample of composite key. In example my key would be name + lastname:
public class MyKey implements Serializable {
String name;
String lastname;
}
is that enough? what requirements are not met yet? I didn't catch that "attributes must correspond those from entity bean".
Best regards!
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vladas Razas:
I didn't Can someone show sample of composite key. In example my key would be name + lastname:
public class MyKey implements Serializable {
String name;
String lastname;
}
is that enough?

I haven't used a primary key class yet, but if I remember correctly, it has the same requires as a Java Bean: a public empty constructor and public accessors for each field (or maybe you can get by with public fields?). Before you go that route, however, I strongly recommend against multi-column primary keys and even more against using an attribute that may change like a name as part of one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!