Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

auto_increment primary key

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a CMP entity bean with an auto_increment primary key in it's table with JBoss 3.2.3 and mySQL. Now I can insert new row into table with the auto_increment key successfully! But I want to know the auto_increment key value after bean creating. So I called EntityContext.getPrimaryKey() in ejbPostCreate() and printed it. But I found the key always is zero.(I set it zero when calling ejbCreate())

I want to know how to get the mySQL generate key in my code.

Thanks in advance!

Cliff Liang
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have JBoss have MySQL autogenerate the key (when the insert happens), you shouldn't set the primary key value at all. Just return null and that's it. Don't do anything else. JBoss will retrieve the value from MySQL and set the primary key value for you.

As for how to do this, search this forum. I remember answering this question a while ago.
 
Qi Liang
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is my bean UserBean:
Long id
String username
String name

public Long ejbCreate(String username, String name)
{
this.setId(new Long(0));
this.setUsername(username);
this.setName(name);
return null;
}

At first, I didn't write "this.setId(new Long(0));" as above, JBoss always print error in its console, saying "Primary key can not be null". But new row is inserted into database. After adding this statement, no error appears, and everything is ok.

So I think this statement take some role while creating.

My question is whether I can get the generated key after creating. As so far, I think it's impossible to get it, right?
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you search the forum? Or the Internet? I'm not going to give you all the answers, but if you spend about 10 more minutes of your time you can make everything crysal-clear for yourself.
 
Qi Liang
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I search it in Internet, and I also see the source of mysql-generated-key. But from the interface of EntityContext, I think if the primary key is not assigned automatically, I will never get it.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a try this:

https://coderanch.com/t/88598/JBoss/JBoss-CMP-auto-increment-not

I got the same error as you. (using Lomboz)

Remember change jboss version to 3.2 in "xdoclet.xml" to 3.2

-------------------
Victor J. Lu
BTech IT
UoA
[ August 23, 2004: Message edited by: Victor J. Lu ]
 
    Bookmark Topic Watch Topic
  • New Topic