请问直接利用CMP和SessionBean能否实现删除数据库记录的操作?

04-11-18 ronin
我不想再去写个BMP 不知道CMP能否实现呢? 反正我已经用BMP+SessionBean实现了添加,更新等操作,就是在删除时遇到了麻烦,请大侠指教!

SportsBaby1980
2004-11-19 10:50
当然可以了

不然 还有那么多人用这个吗?

有一个remove方法

应该是在Home interface中

调用该方法即可

ronin
2004-11-19 13:45
其实我也是这么操作的 但总是删不掉 不知道为什么 我是在会话bean里通过cmp的本地引用来访问cmp的 我的两个bean代码如下:

package booksys;

import javax.ejb.*;

abstract public class BookinfoBean implements EntityBean {

EntityContext entityContext;

public java.lang.String ejbCreate(java.lang.String bookid) throws CreateException {

setBookid(bookid);

return null;

}

public void ejbPostCreate(java.lang.String bookid) throws CreateException {

/**@todo Complete this method*/

}

public void ejbRemove() throws RemoveException {

/**@todo Complete this method*/

}

public abstract void setBookid(java.lang.String bookid);

public abstract void setBooktype(java.lang.String booktype);

public abstract void setBookname(java.lang.String bookname);

public abstract void setAuthor(java.lang.String author);

public abstract void setPublisher(java.lang.String publisher);

public abstract void setPubtime(java.lang.String pubtime);

public abstract java.lang.String getBookid();

public abstract java.lang.String getBooktype();

public abstract java.lang.String getBookname();

public abstract java.lang.String getAuthor();

public abstract java.lang.String getPublisher();

public abstract java.lang.String getPubtime();

public void ejbLoad() {

/**@todo Complete this method*/

}

public void ejbStore() {

/**@todo Complete this method*/

}

public void ejbActivate() {

/**@todo Complete this method*/

}

public void ejbPassivate() {

/**@todo Complete this method*/

}

public void unsetEntityContext() {

this.entityContext = null;

}

public void setEntityContext(EntityContext entityContext) {

this.entityContext = entityContext;

}

}

package booksys;

import javax.ejb.*;

import javax.naming.*;

import javax.rmi.PortableRemoteObject;

import java.util.*;

public class facadeBean implements SessionBean {

SessionContext sessionContext;

public Bookinfo bookinfo=null;

private BookinfoHome bookinfoHome=null;

private Book book=null;

//通过实体bean:bookinfo的本地引用(bookref)获得该实体bean的Home接口对象

//且在每次创建和激活sessionBean的时候各获取一次

public void ejbCreate() throws CreateException {

try {

Context context=new InitialContext();

Object ref=context.lookup("java:/comp/env/bookref");

bookinfoHome=(BookinfoHome)ref;

}

catch (Exception ex) {

ex.printStackTrace();

}

}

public void ejbRemove() {

}

//通过实体bean:bookinfo的本地引用(bookref)获得该实体bean的Home接口对象

//且在每次创建和激活sessionBean的时候各获取一次

public void ejbActivate() {

try {

Context context=new InitialContext();

Object ref=context.lookup("java:/comp/env/bookref");

bookinfoHome=(BookinfoHome)ref;

}

catch (Exception ex) {

ex.printStackTrace();

}

}

public void ejbPassivate() {

}

public void setSessionContext(SessionContext sessionContext) {

this.sessionContext = sessionContext;

}

//通过指定bookid查询书籍信息,返回Book类的一个实例

public Book getSpecifiedBook(String bookid) {

try {

bookinfo=bookinfoHome.findByPrimaryKey(bookid);

book=new Book(bookinfo.getBookid(),bookinfo.getBooktype(),bookinfo.getBookname(),bookinfo.getAuthor(),bookinfo.getPublisher(),bookinfo.getPubtime());

}

catch (Exception ex) {

ex.printStackTrace();

}

return book;

}

//通过知道bookname查询书籍信息,返回Book类的一个实例

public Book getBookByName(String bookname) {

try {

bookinfo=bookinfoHome.findBookByName(bookname);

book=new Book(bookinfo.getBookid(),bookinfo.getBooktype(),bookinfo.getBookname(),bookinfo.getAuthor(),bookinfo.getPublisher(),bookinfo.getPubtime());

}

catch (Exception ex) {

ex.printStackTrace();

}

return book;

}

//返回所有书籍集合

public java.util.Collection getAllBook() {

Collection result=new Vector();

try {

Collection collection=bookinfoHome.findAllBook();

System.out.println(collection.size());

Object object[]=collection.toArray();

for (int i = 0; i < object.length; i++) {

bookinfo=(Bookinfo)object;

result.add(new Book(bookinfo.getBookid(),bookinfo.getBooktype(),bookinfo.getBookname(),bookinfo.getAuthor(),bookinfo.getPublisher(),bookinfo.getPubtime()));

}

}

catch (Exception ex) {

ex.printStackTrace();

}

return result;

}

//通过书名模糊查询书籍记录

public java.util.Collection getBookBySubName(String subname) {

Collection result=new Vector();

try {

Collection collection=bookinfoHome.findBookBySubName(subname);

System.out.println(collection.size());

Object object[]=collection.toArray();

for (int i = 0; i < object.length; i++) {

bookinfo=(Bookinfo)object;

result.add(new Book(bookinfo.getBookid(),bookinfo.getBooktype(),bookinfo.getBookname(),bookinfo.getAuthor(),bookinfo.getPublisher(),bookinfo.getPubtime()));

}

}

catch (Exception ex) {

ex.printStackTrace();

}

return result;

}

public void updateBook(Book book) {

try {

bookinfo=bookinfoHome.findByPrimaryKey(book.getBookid());

bookinfo.remove();

bookinfo=bookinfoHome.create(book.getBookid());

bookinfo.setBooktype(book.getBooktype());

bookinfo.setBookname(book.getBookname());

bookinfo.setAuthor(book.getAuthor());

bookinfo.setPublisher(book.getPublisher());

bookinfo.setPubtime(book.getPubtime());

}

catch (Exception ex) {

ex.printStackTrace();

}

}

public void addBook(Book book) {

try {

bookinfo=bookinfoHome.create(book.getBookid());

bookinfo.setBooktype(book.getBooktype());

bookinfo.setBookname(book.getBookname());

bookinfo.setAuthor(book.getAuthor());

bookinfo.setPublisher(book.getPublisher());

bookinfo.setPubtime(book.getPubtime());

}

catch (Exception ex) {

ex.printStackTrace();

}

}

public void removeBookByID(String bookid) {

try {

bookinfo=bookinfoHome.create(bookid);

bookinfo.remove();

}

catch (Exception ex) {

ex.printStackTrace();

}

}

}

其余的方法经过测试都能正常实现要求 唯独最下面的这个removeBookByID不行 希望大侠指教

SportsBaby1980
2004-11-19 17:19
public void removeBookByID(String bookid) {

try {

bookinfo=bookinfoHome.create(bookid);

bookinfo.remove();

}

catch (Exception ex) {

ex.printStackTrace();

}

bookinfo.remove();

这个删除是不行的,这个不是删除CMP EJB的

用bookinfoHome.remove

进行删除

ronin
2004-11-25 14:49
bookinfoHome对象没有remove()方法啊 只有一个remove(Object)方法

我通过这样调用还是不行:

bookinfo=bookinfoHome.create(bookid);

bookinfoHome.remove(bookinfo);

迷惑中

猜你喜欢
2Go 1 2 下一页