Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

primary key on jboss

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since EJB 2.0 does not support ejb-ql with MAX function, I was wondering how the primary keys are being created. The only way I could think of is to have a seperate table which could keep track of the next increment id for a table.
any suggestions.
- Walk rustin
 
Walk Rustin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using jboss with mysql. Mysql supports auto generation of the primary key. Does JBoss supports this too? It will really take a lot of burden off the developer's shoulder.
- Walk rustin
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JBoss does support it. There's some element in the deployment descriptors that sets it. Check out the "entity-command" element in the DTD for the jbosscmp-jdbc deployment descriptors.
 
Walk Rustin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks.
Jboss documentation is so poor that it's hard to find instructions on getting anything done. Same is the case with 'enity commands'. I paid $10.00 and bought the online documentation on CMP. It doesn't even mention 'entity commands'.
Does anyone have any working example of how to use 'entity commands'?
thanks.
- walker rustin
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Walker,
Have you used Google to search for "entity-command"? You may want to try that.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the idea of using 'entity-command'. I suppose it is used so that the database doesn't have to generate the primary key for a record. By using this feature, the developer is asking jboss to generate a primary key and pass that key to the database. It means the primary key on the database should NOT be marked at auto-increment.
Is that assumption correct? Or Do I need to mark both at the jboss level and at the database level as auto-increment. Do they work in conjunction?
thanks.
- Walker
 
Walker Rustin
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IT WORKS!
Here is the complete code:
ejb-jar.xml
<entity>
<display-name>EmployeeEJB</display-name>
<ejb-name>EmployeeEJB</ejb-name>
<local-home>employee.ejb.com.sbm.cts.EmployeeHome</local-home>
<local>employee.ejb.com.sbm.cts.Employee</local>
<ejb-class>employee.ejb.com.sbm.cts.EmployeeBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>EmployeeSchema</abstract-schema-name>
<cmp-field><field-name>firstname</field-name></cmp-field>
<cmp-field><field-name>lastname</field-name></cmp-field>
<cmp-field><field-name>telephone</field-name></cmp-field>
<cmp-field><field-name>employeeid</field-name></cmp-field>

<primkey-field>employeeid</primkey-field>
<resource-ref>
<res-ref-name>jdbc/ctsDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</entity>

jbosscmp-jdbc.xml
<entity>
<ejb-name>EmployeeEJB</ejb-name>
<table-name>employee</table-name>

<cmp-field>
<field-name>firstname</field-name>
<column-name>firstname</column-name>
</cmp-field>
<cmp-field>
<field-name>lastname</field-name>
<column-name>lastname</column-name>
</cmp-field>
<cmp-field>
<field-name>telephone</field-name>
<column-name>telephone</column-name>
</cmp-field>
<cmp-field>
<field-name>employeeid</field-name>
<column-name>employeeid</column-name>
<auto-increment/>
<not-null/>
</cmp-field>
<entity-command name="mysql-get-generated-keys"
class="org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCMySQLCreateCommand"/>
</entity>

code:
// HOME methods
public Integer ejbCreate(EmployeeDetails employeeDetails) throws CreateException {
logger.debug("ejbCreate");
try {
BeanUtils.copyProperties(this, employeeDetails);
} catch (java.lang.reflect.InvocationTargetException e) {
//nothing to log. It's expected because the primary key can't be
// changed.
logger.error("error in invocation" + e.getMessage());
} catch (Exception e) {
logger.error(e.getMessage());
}
return null;
}
public void ejbPostCreate(EmployeeDetails employeeDetails) {
logger.debug("ejbPostCreate");
}
// CMP field accessors
public abstract String getFirstname();
public abstract void setFirstname(String firstname);
public abstract String getLastname();
public abstract void setLastname(String lastname);
public abstract String getTelephone();
public abstract void setTelephone(String telephone);

public abstract Integer getEmployeeid();
public abstract void setEmployeeid(Integer employeeid);
sql code:
CREATE TABLE employee
(
firstname VARCHAR(30),
lastname VARCHAR(30),
telephone VARCHAR(30),
employeeid int(4) auto_increment,
CONSTRAINT pk_user PRIMARY KEY (employeeid)
);
Thanks everyone.
- walker
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Walker,
did u generate you ejb with xdoclet?
if yes, could we please have that?
Thank you
Rudi
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't convertend my code to xdoclete yet. I was just playing with the features of jboss. The xdoclet code is discussed in this forum of jboss.
http://jboss.org/index.html?module=bb&op=viewtopic&t=49321.
Hope it helps.
- Roger
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If/when you convert to use XDoclet, you might consider following the best practices and use the GUID Generator that it will generate for you.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Now i have a problem in Multi- Threaded Environment. I have multiple threads running at the same time to collect the data from different source and store it the table using ejb create method. When i use Primary key generation through entity commands , i am getting duplicate keys generated. How to synchronize the way the keys are generated?

Thanks
Siva
reply
    Bookmark Topic Watch Topic
  • New Topic