添加记录可以调用实体bean的create方法,同时向create方法传送主键参数,比如下面这个应该对你有所帮助:AUser auser = null;
/**@todo Complete this method*/
try
{
Context context= new InitialContext();
Object ref=context.lookup("AUser");
AUserHome auserHome=(AUserHome)PortableRemoteObject.narrow(ref,AUserHome.class);
System.out.println("Insert a user whose id is:"+id);
auser=auserHome.create(id);
auser.setPassword(password); }
catch(Exception ex){
System.out.println("Exception in adding!"+ex);
}
/**@todo Complete this method*/
try
{
Context context= new InitialContext();
Object ref=context.lookup("AUser");
AUserHome auserHome=(AUserHome)PortableRemoteObject.narrow(ref,AUserHome.class);
System.out.println("Insert a user whose id is:"+id);
auser=auserHome.create(id);
auser.setPassword(password); }
catch(Exception ex){
System.out.println("Exception in adding!"+ex);
}
另外,如果数据库表除主键外都允许为空的话可以生成只有主键不为空的记录,但如果存在不允许为空的字段的话,是添加不了的,即记录插不进去。
auser=auserHome.findByPrimaryKey(id);
然后再 auser.setPassword(password);从而更新password字段