当前位置: 技术问答>java相关
oracle的sequence怎么在EJB中实现???
来源: 互联网 发布时间:2017-03-10
本文导语: oracle作为开发EJB的数据库; 创建sequence: create sequence seqForTest increment by 1 start with 1; 创建表: CREATE TABLE test ( ID number(8) primary key NOT NULL, Name varchar(50) ); 请问在创建ENTITY BEAN 时,怎样才能使用seqForTest.nextva...
oracle作为开发EJB的数据库;
创建sequence:
create sequence seqForTest increment by 1 start with 1;
创建表:
CREATE TABLE test (
ID number(8) primary key NOT NULL,
Name varchar(50)
);
请问在创建ENTITY BEAN 时,怎样才能使用seqForTest.nextval使ID递增
tks!
创建sequence:
create sequence seqForTest increment by 1 start with 1;
创建表:
CREATE TABLE test (
ID number(8) primary key NOT NULL,
Name varchar(50)
);
请问在创建ENTITY BEAN 时,怎样才能使用seqForTest.nextval使ID递增
tks!
|
上个星期刚解决,共享啦。
写一个 stateless sessionbean 先,用于取得 sequence 值。
package com.yourcompany.util.ejb;
import javax.naming.InitialContext;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
public class SequenceBean implements SessionBean {
// ==== session bean related ====
public void ejbCreate() throws CreateException, EJBException { init(); }
public void ejbRemove() throws EJBException { }
public void ejbActivate() throws EJBException { }
public void ejbPassivate() throws EJBException { }
private SessionContext ctx;
public void setSessionContext(SessionContext context) throws RemoteException, EJBException { ctx = context; }
// ==== properties ====
static final private String classname = SequenceBean.class.getName();
static final private Loger log = Loger.getInstance(classname);
/**
* @RESOURCE-REF
* @ResourceRefAuth Application
* @ResourceRefType javax.sql.DataSource
* @ResourceRefRemoteType local
*/
static final private String dataSourceName = "java:comp/env/jdbc/yourDataSource";
private DataSource dataSource = null;
private void init() throws CreateException, EJBException {
try {
InitialContext ic = new InitialContext();
dataSource = (DataSource) ic.lookup(dataSourceName);
} catch (Exception e) {
log.debug("init",e);
}
}
// ==== bussiness methods ====
public Long getNext(String sequenceName) {
Long value = null;
Connection con = null;
try {
con = dataSource.getConnection();
String sql = "select " + sequenceName + ".nextval nextId from dual";
PreparedStatement prepStmt = con.prepareStatement(sql);
ResultSet rs = prepStmt.executeQuery();
if (rs.next()) {
value = new Long(rs.getLong(1));
}
rs.close();
prepStmt.close();
} catch (Exception e) {
log.debug("getNext",e);
} finally {
try { con.close(); } catch (Exception anye) { }
}
return value;
}
}
( local home 和 local 接口略 )
创建的时候:( stateful sessionbean 中引用 local 的 sessionbean (Sequence) 以及 local 的 entitybean (User) )
try {
Long userId = sequenceLocal.getNext(userSeqenceName);
user = new UserDto(userId, userName, userPass, userEmail);
userLocalHome.create(user);
} catch (Exception e) {
log.debug(e);
throw new EJBException( new YourSystemException("createUser failure", e) );
}
注意,本版另一个帖子中,有一位“牛人同志”严肃批评了程序中 sequence 使用的想法,请自行决定是否采用 sequence 的设计。
详情可以参考:“请问在CMP中如何处理自动增长的主键?”一贴。
写一个 stateless sessionbean 先,用于取得 sequence 值。
package com.yourcompany.util.ejb;
import javax.naming.InitialContext;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
public class SequenceBean implements SessionBean {
// ==== session bean related ====
public void ejbCreate() throws CreateException, EJBException { init(); }
public void ejbRemove() throws EJBException { }
public void ejbActivate() throws EJBException { }
public void ejbPassivate() throws EJBException { }
private SessionContext ctx;
public void setSessionContext(SessionContext context) throws RemoteException, EJBException { ctx = context; }
// ==== properties ====
static final private String classname = SequenceBean.class.getName();
static final private Loger log = Loger.getInstance(classname);
/**
* @RESOURCE-REF
* @ResourceRefAuth Application
* @ResourceRefType javax.sql.DataSource
* @ResourceRefRemoteType local
*/
static final private String dataSourceName = "java:comp/env/jdbc/yourDataSource";
private DataSource dataSource = null;
private void init() throws CreateException, EJBException {
try {
InitialContext ic = new InitialContext();
dataSource = (DataSource) ic.lookup(dataSourceName);
} catch (Exception e) {
log.debug("init",e);
}
}
// ==== bussiness methods ====
public Long getNext(String sequenceName) {
Long value = null;
Connection con = null;
try {
con = dataSource.getConnection();
String sql = "select " + sequenceName + ".nextval nextId from dual";
PreparedStatement prepStmt = con.prepareStatement(sql);
ResultSet rs = prepStmt.executeQuery();
if (rs.next()) {
value = new Long(rs.getLong(1));
}
rs.close();
prepStmt.close();
} catch (Exception e) {
log.debug("getNext",e);
} finally {
try { con.close(); } catch (Exception anye) { }
}
return value;
}
}
( local home 和 local 接口略 )
创建的时候:( stateful sessionbean 中引用 local 的 sessionbean (Sequence) 以及 local 的 entitybean (User) )
try {
Long userId = sequenceLocal.getNext(userSeqenceName);
user = new UserDto(userId, userName, userPass, userEmail);
userLocalHome.create(user);
} catch (Exception e) {
log.debug(e);
throw new EJBException( new YourSystemException("createUser failure", e) );
}
注意,本版另一个帖子中,有一位“牛人同志”严肃批评了程序中 sequence 使用的想法,请自行决定是否采用 sequence 的设计。
详情可以参考:“请问在CMP中如何处理自动增长的主键?”一贴。