Try the attached sample. There are two session bean. One of them is a 'id
generator': it can be access through a singleton object that provides the
syncronization. The second one is a test bean: it will use the id generator.
One fault of this approach is you can access the DB through the the
appserver, because if someone insert data in the DB another way the PK may
be not correct.

Note: this approach was published in the JavaDev Journal 12/2000 :)))

-----Original Message-----
From: gizmo [mailto:[EMAIL PROTECTED]]
Sent: 2001. május 24. 19:08
To: [EMAIL PROTECTED]
Subject: [JBoss-user] automatic generated primarykey ???


i don't need the primarykey in my application, so i want jboss or the
database to generate my primarykey automaticly.
i read i can do this when i use a java.lang.Object as a primarykey, but it
didn't work - jboss throwed something like a NotSerializableException.
then i tried java.lang.Integer - then jboss said i would need the follow
cmp-fields in my bean : MIN_VALUE, MAX_VALUE, TYPE.
so i did, even when i don't know what's meant by TYPE.
after it i could deploy my bean without problems.
but when i executed the home.create - method, there occurred loads of
exceptions saying it's not possible to create a primarykey.

is there any way to generate the primarykeys automaticly, the same way when
the database does it for me with AUTOINCREMENT ???

Thanks in advantage


here the code when i used a java.lang.Object as the primarykey :

> I don't want to generate a primary key and i did read it's possible the
> container can do this for me.
> I just need to act with java.lang.Object as key the way I show in the code
> below.
> The bean is very simple and handle with a string. I can deploy the bean
> without problems.
> But when i try to access with a client I get an error.
> By the way has someone a good and safe algorithm to generate a unique key
?
>
> thanks in advantage
>
>
> ************************************************************
> package com.test.string;
>
> import java.rmi.RemoteException;
> import javax.ejb.CreateException;
> import javax.ejb.FinderException;
> import java.util.*;
>
> public interface MyStringHome extends javax.ejb.EJBHome {
>   public MyString create()
>     throws CreateException, RemoteException;
>
>   public MyString findByPrimaryKey(java.lang.Object id)
>     throws FinderException, RemoteException;
>
>   public Collection findAll()
>     throws FinderException, RemoteException;
> }
> ************************************************************
>
> package com.test.string;
>
> import java.rmi.RemoteException;
>
> public interface MyString extends javax.ejb.EJBObject {
>   public String getString() throws RemoteException;
>   public void setString(String string) throws RemoteException;
> }
> ************************************************************
>
> package com.test.string;
>
> import javax.ejb.EntityContext;
>
> public class MyStringBean implements javax.ejb.EntityBean {
>
>   transient private EntityContext ctx;
>   public String myString;
>
>   public java.lang.Object ejbCreate() {
>     return null;
>   }
>
>   public void ejbPostCreate() {}
>
>   public String getString(){
>     return myString;
>   }
>
>   public void setString(String string) {
>     myString = string;
>   }
>
>   public void setEntityContext(EntityContext ctx) { this.ctx = ctx; }
>   public void unsetEntityContext() { ctx = null; }
>   public void ejbActivate() {}
>   public void ejbPassivate() {}
>   public void ejbLoad() {}
>   public void ejbStore() {}
>   public void ejbRemove() {}
> }
> ************************************************************
> <?xml version="1.0"?>
>
> <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise
> JavaBeans 1.1//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd";>
>
> <ejb-jar>
>   <display-name>Strings</display-name>
>   <enterprise-beans>
>
>     <entity>
>       <description>Handle a String</description>
>       <ejb-name>MyStringBean</ejb-name>
>       <home>com.test.string.MyStringHome</home>
>       <remote>com.test.string.MyString</remote>
>       <ejb-class>com.test.string.MyStringBean</ejb-class>
>       <persistence-type>Container</persistence-type>
>       <prim-key-class>java.lang.Object</prim-key-class>
>       <reentrant>False</reentrant>
>       <cmp-field><field-name>myString</field-name></cmp-field>
>     </entity
>   </enterprise-beans>
>
>   <assembly-descriptor>
>     <container-transaction>
>       <method>
>         <ejb-name>MyStringBean</ejb-name>
>         <method-name>*</method-name>
>       </method>
>       <trans-attribute>Required</trans-attribute>
>     </container-transaction>
>   </assembly-descriptor>
> </ejb-jar>
> ************************************************************



_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-user

idgenerator.tar

Reply via email to