据我所知,对于主键自动增长的问题,j2ee规范并不明确,各家ejb容器均有自己的做法,对于使用weblogic+oracle,可以这样来做(weblogic文档中节选)Specifying Primary Key Support for OracleGenerated primary key support for Oracle databases uses Oracle's SEQUENCE feature. This feature works with a Sequence entity in the Oracle database to generate unique primary keys. The Oracle SEQUENCE is called when a new number is needed.Once the SEQUENCE already exists in the database, you specify automatic key generation in the XML deployment descriptors. In the weblogic-cmp-rdbms-jar.xml file, you specify automatic key generation as follows: Figure 5-8 Specifying automatic key generation for Oracle<automatic-key-generation> <generator-type>ORACLE</generator-type> <generator_name>test_sequence</generator-name> <key-cache-size>10</key-cache-size></automatic-key-generation>Specify the name of the ORACLE SEQUENCE to be used, using the generator-name element. If the ORACLE SEQUENCE was created with a SEQUENCE INCREMENT value, then you must specify a key-cache-size. This value must match the Oracle SEQUENCE INCREMENT value. If these two values are different, then you will most likely have duplicate key problems.Warning: Do not use the generator type USER_DESIGNATED_TABLE with Oracle, as doing so can cause the following exception:javax.ejb.EJBException: nested exception is: java.sql.SQLException: Automatic Key Generation Error: attempted to UPDATE or QUERY NAMED SEQUENCE TABLE NAMED_SEQUENCE_TABLE, but encountered SQLException java.sql.SQLException: ORA-08177: can't serialize access for this transaction. USER_DESIGNATED_TABLE mode sets the TX ISOLATION LEVEL to SERIALIZABLE which can cause problems with Oracle. Instead, use the AutoKey option ORACLE. 对于jboss3.2我已知道mysql数据库的做法,却不知道oracle数据库该怎么实现,jbosscmp-jdbc.xml的编写,希望高手能指教!
感谢楼上各位的回答,非常感谢! to bonhomme:你说任何表都有主键,那我举个例子(当然只是假设),有这样一个表,就三个字段,一个是报警时间,一个是报警地点,一个是报警内容,时间只定位到天,那么请问这个表除了增加一个id(自增)字段外 应该用什么做主键?(同一天同一个地点可以有多条报警)
不过方法有点土 ^_^
我们的id主键字段设置为自增的
然后写了一个方法用sql专门取得下一个主键值
然后在ejb的create方法中调用这个方法得到主键值
也是没办法啊
有sequence对象
在插入之前,先查询sequence
就知道下一个值了
cmp: 那就要看容器是否支持数据库的自增了. weblogic支持sqlserver和oracle..其他的就不清楚了...
敬仰之~~~~~~~~~~~~~~~~~~~~~~
“如果是ms sqlserver,有一个语句可以插入数据同时返回id”,请问这个语句怎么弄?怎么写呢?“那就要看容器是否支持数据库的自增了. weblogic支持sqlserver和oracle..”,如果支持,在create方法里容器能直接得到主键吗?
对于jboss3.2我已知道mysql数据库的做法,却不知道oracle数据库该怎么实现,jbosscmp-jdbc.xml的编写,希望高手能指教!
to bonhomme:你说任何表都有主键,那我举个例子(当然只是假设),有这样一个表,就三个字段,一个是报警时间,一个是报警地点,一个是报警内容,时间只定位到天,那么请问这个表除了增加一个id(自增)字段外 应该用什么做主键?(同一天同一个地点可以有多条报警)