星期六, 三月 06, 2004

MySQL Auto increment 字段,如何在 JBoss CMP ejb 2.0中定义和使用.

环境:Jboss 3.2.2 with tomcat 4.1.27.Mysql 4.0.18
首先有两个Mysql 4.0.18 的表
# Host: williamwang
# Database: test
# Table: 'account'
#
CREATE TABLE `account` (
`accountid` int(11) NOT NULL auto_increment,
`acount` varchar(100) default '',
`salaryid` int(11) default '0',
`text` varchar(100) default '',
PRIMARY KEY (`accountid`)
) TYPE=MyISAM;

# Host: williamwang
# Database: test
# Table: 'salary'
#
CREATE TABLE `salary` (
`salaryid` int(11) NOT NULL auto_increment,
`userid` varchar(20) NOT NULL default '',
`workage` int(11) NOT NULL default '0',
`titlelevel` int(12) NOT NULL default '0',
PRIMARY KEY (`salaryid`)
) TYPE=MyISAM;

其次定义Jboss 的jbosscmp-jdbc.xml.红色为关键处

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jbosscmp-jdbc PUBLIC "-//JBoss//DTD JBOSSCMP-JDBC 3.2//EN" "http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_3_2.dtd">
<jbosscmp-jdbc>
<defaults>
<datasource>java:/MySqlDS</datasource>
<datasource-mapping>mySQL</datasource-mapping>
</defaults>
<enterprise-beans>
<entity>
<ejb-name>Account</ejb-name>
<table-name>account</table-name>
<cmp-field>
<field-name>accountid</field-name>
<column-name>accountid</column-name>
<auto-increment/>
</cmp-field>
<cmp-field>
<field-name>acount</field-name>
<column-name>acount</column-name>
</cmp-field>
<cmp-field>
<field-name>salaryid</field-name>
<column-name>salaryid</column-name>
</cmp-field>
<cmp-field>
<field-name>text</field-name>
<column-name>text</column-name>
</cmp-field>

<unknown-pk>
<unknown-pk-class>java.lang.Integer</unknown-pk-class>
<field-name>accountid</field-name>
<column-name>accountid</column-name>
<jdbc-type>INTEGER</jdbc-type>
<sql-type>INTEGER</sql-type>
<auto-increment/>
</unknown-pk>
<entity-command name="mysql-get-generated-keys"/>

</entity>

<entity>
<ejb-name>Salary</ejb-name>
<table-name>salary</table-name>
<cmp-field>
<field-name>salaryid</field-name>
<column-name>salaryid</column-name>
<auto-increment/>
</cmp-field>
<cmp-field>
<field-name>titlelevel</field-name>
<column-name>titlelevel</column-name>
</cmp-field>
<cmp-field>
<field-name>userid</field-name>
<column-name>userid</column-name>
</cmp-field>
<cmp-field>
<field-name>workage</field-name>
<column-name>workage</column-name>
</cmp-field>
</entity>
</enterprise-beans>
</jbosscmp-jdbc>

其三:定义一个TestClient来测试。