Jason, I tried to find the page for the "EJB Design Patterns" book draft chapters, which I think could help you a lot, but it looks like theserverside.com has taken down the preview chapters. Now you'll have to wait for the final PDF, which will be available shortly:
http://www.theserverside.com/books/EJBDesignPatterns/index.jsp In that book you could find information for assigning ids to your objects, using a design pattern. Greetings, Ricardo Arguello ----- Original Message ----- From: "Robertson, Jason" <[EMAIL PROTECTED]> To: "'Dain Sundstrom'" <[EMAIL PROTECTED]> Cc: "JBoss User (E-mail)" <[EMAIL PROTECTED]> Sent: Thursday, February 28, 2002 3:04 PM Subject: RE: [JBoss-user] CMP2.0 Create Problem > Ok, I didn't realize I had to do that myself. Was hoping to use the DB. > > Thanks for the quick responses. > > Jason > > -----Original Message----- > From: Dain Sundstrom [mailto:[EMAIL PROTECTED]] > Sent: Thursday, February 28, 2002 3:08 PM > To: Robertson, Jason > Cc: JBoss User (E-mail) > Subject: Re: [JBoss-user] CMP2.0 Create Problem > > > You need to assign a value to each primary key field in ejbCreate. > > -dain > > Robertson, Jason wrote: > > > Ok, I took out all AUTO INCREMENT and NOT NULL stuff, and now I have > another > > problem in that it's trying to create the table with NULL as the value for > > the primary key, which isn't allowed in MySQL. If I remove the PRIMARY KEY > > attribute from the column, I get the exception from the first email. > Here's > > the log leading up to the exception: > > > > DEBUG reateEntityCommand.UserRoleEJB UserRoleEJB - Create: pk=null > > > > DEBUG reateEntityCommand.UserRoleEJB UserRoleEJB - Executing SQL: SELECT > > COUNT(*) FROM userroles WHERE id=? > > DEBUG UserRoleEJB.id UserRoleEJB - Set parameter: index=1, > > jdbcType=INTEGER, value=NULL > > DEBUG reateEntityCommand.UserRoleEJB UserRoleEJB - Executing SQL: INSERT > > INTO userroles (id, roleName, username, groupId) VALUES (?, ?, ?, ?) > > DEBUG UserRoleEJB.id UserRoleEJB - Set parameter: index=1, > > jdbcType=INTEGER, value=NULL > > DEBUG UserRoleEJB.name UserRoleEJB - Set parameter: index=2, > > jdbcType=VARCHAR, value=NULL > > DEBUG UserRoleEJB.username UserRoleEJB - Set parameter: index=3, > > jdbcType=VARCHAR, value=NULL > > DEBUG UserRoleEJB.id UserRoleEJB - Set parameter: index=4, > > jdbcType=INTEGER, value=NULL > > DEBUG reateEntityCommand.UserRoleEJB UserRoleEJB - Create: Rows affected = > 1 > > > > ERROR plugins.LogInterceptor UserRoleEJB - > > TransactionRolledbackException, causedBy: > > > > > > Completely guessing on my part, is the SELECT COUNT(*) statement trying to > > verify that a record with the given choice for primary key doesn't exist? > If > > so, it's strange that it's picking NULL as its primary key, and would be > why > > the exception is getting thrown. > > > > Any other ideas? > > > > Jason > > > > > > p.s. I found a small error in my code below in that I was returning a > String > > from ejbCreate when it should be an Integer, it's been fixed. > > > > -----Original Message----- > > From: Dain Sundstrom [mailto:[EMAIL PROTECTED]] > > Sent: Thursday, February 28, 2002 2:18 PM > > To: Robertson, Jason > > Cc: JBoss User (E-mail) > > Subject: Re: [JBoss-user] CMP2.0 Create Problem > > > > > > You can't use database autonumber columns, and you can't have not-null > > foreign keys. Both of these will eventually be supported. > > > > -dain > > > > Robertson, Jason wrote: > > > > > >>jboss3.0.0beta, jdk1.3.1_02, MySQL > >> > >>Two questions: > >> > >>1. I understand that for CMR fields you set them in ejbPostCreate, but > >> > > what > > > >>if your table (that already exists) has these fields marked as "NOT NULL"? > >>The initial INSERT INTO fails since it has that field as NULL and it never > >>gets to ejbPostCreate. > >> > >>2. In my case I have control of the DB layout, so I removed the NOT NULL > >>from the initial creation of the table to see it work (and added in checks > >>for null objects? is this the best way to do this?). I then get the > >>following error: > >> > >>java.lang.Error: id may not be null > >> at org.jboss.ejb.CacheKey.<init>(CacheKey.java:80) > >> at > >> > >> > > > org.jboss.ejb.plugins.EntityInstanceCache.createCacheKey(EntityInstanceCache > > > >>.java:63) > >> at > >> > >> > > > org.jboss.ejb.plugins.CMPPersistenceManager.createEntity(CMPPersistenceManag > > > >>er.java:239) > >> at > >>org.jboss.ejb.EntityContainer.createLocalHome(EntityContainer.java:561) > >> at java.lang.reflect.Method.invoke(Native Method) > >> at > >> > >> > > > org.jboss.ejb.EntityContainer$ContainerInterceptor.invokeHome(EntityContaine > > > >>r.java:1051) > >> at > >> > >> > > > org.jboss.ejb.plugins.AbstractInterceptor.invokeHome(AbstractInterceptor.jav > > > >>a:73) > >> at > >> > >> > > > org.jboss.ejb.plugins.EntitySynchronizationInterceptor.invokeHome(EntitySync > > > >>hronizationInterceptor.java:222) > >> at > >> > >> > > > org.jboss.ejb.plugins.EntityInstanceInterceptor.invokeHome(EntityInstanceInt > > > >>erceptor.java:136) > >> at > >> > >> > > > org.jboss.ejb.plugins.EntityLockInterceptor.invokeHome(EntityLockInterceptor > > > >>.java:80) > >> at > >> > >> > > > org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor > > > >>.java:98) > >> at > >> > >> > > > org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT. > > > >>java:167) > >> at > >> > >> > > > org.jboss.ejb.plugins.TxInterceptorCMT.invokeHome(TxInterceptorCMT.java:52) > > > >> at > >> > >> > > > org.jboss.ejb.plugins.SecurityInterceptor.invokeHome(SecurityInterceptor.jav > > > >>a:102) > >> at > >>org.jboss.ejb.plugins.LogInterceptor.invokeHome(LogInterceptor.java:109) > >> at > >>org.jboss.ejb.EntityContainer.invokeHome(EntityContainer.java:469) > >> at > >> > >> > > > org.jboss.ejb.plugins.local.BaseLocalContainerInvoker.invokeHome(BaseLocalCo > > > >>ntainerInvoker.java:244) > >> at > >> > >> > > > org.jboss.ejb.plugins.local.BaseLocalContainerInvoker$HomeProxy.invoke(BaseL > > > >>ocalContainerInvoker.java:369) > >> at $Proxy13.create(Unknown Source) > >> at > >> > >> > > > test.com.acs.j2ee.framework.user.entity.UserRoleEntityTest.testCreate(Unknow > > > >>n Source) > >> > >>My code looks like this: > >> > >> public String ejbCreate(UserEntity user, RoleEntity role, GroupEntity > >>group) > >> { > >> setId(new Integer(0)); // Auto increment > >> return null; > >> } > >> > >> public void ejbPostCreate(UserEntity user, RoleEntity role, GroupEntity > >>group) > >> { > >> setUser(user); > >> setRole(role); > >> setGroup(group); > >> } > >> > >>where UserEntity, RoleEntity, and GroupEntity are all foriegn-key > >>relationships. > >> > >>In the database, the initial INSERT INTO worked: > >> > >>mysql> select * from userroles; > >>+----+----------+----------+---------+ > >>| id | username | roleName | groupId | > >>+----+----------+----------+---------+ > >>| 7 | NULL | NULL | NULL | > >>+----+----------+----------+---------+ > >> > >>But that "7" isn't making it back to the EJB. Should I not be using the > >>"AUTO INCREMENT" feature of MySQL to make sure I get unique primary keys? > >> > > My > > > >>DB create statment is: > >> > >>CREATE TABLE IF NOT EXISTS UserRoles( > >> id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, > >> username VARCHAR(32), > >> roleName VARCHAR(64), > >> groupId INTEGER > >>) > >> > >>Jason > >> > >>p.s. In the JBoss Forums, do a 'search all forums' for "relationship > >>ejbPostCreate". For me, at least, I get 272 results, but only number one > >> > > is > > > >>shown then there is a "2" and that's the end of the page. > >> > >> > >>_______________________________________________ > >>JBoss-user mailing list > >>[EMAIL PROTECTED] > >>https://lists.sourceforge.net/lists/listinfo/jboss-user > >> > >> > > > > > > _______________________________________________ > JBoss-user mailing list > [EMAIL PROTECTED] > https://lists.sourceforge.net/lists/listinfo/jboss-user > _______________________________________________ JBoss-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jboss-user
