嗯,你这个多个记录指什么?是多个重复记录?还是说有可能重复,有可能不重复的记录?findByPrimaryKey你可以重写一个自己的方法
解决方案 »
- Mac上跑tomcat跑不起来
- 为什么拦截器 actionInvocation.invoke()之后 不能通过return跳转
- urlrewrite匹配问题.
- 使用struts2在载入页面时,如何分页查出数据
- 急用!望给予指导,本人不胜感激……
- 关于<%%>用struts2.0的标签实现其功能!
- Maven & Subversion
- 怎样能方便地实现JavaBeans到XML的相互转换?用digester好像只能实现xml到javabean的转化啊
- java调用SQL2000存储过程的问题!(急)
- 上面有struts师兄没有,现在有一问题,需要帮助
- 关于cloudscape与数据库应用程序? 50分!
- 大家看看:这种情况怎么来设计软件的结构?
不可能重复需要解决并发问题,我还不知道在Entity Bean怎么控制并发问题!现在能给一个不可
能重复的例子么?findByPrimaryKey方法和create方法怎么写?急!!!
介绍一下,最好给个例子,谢先了!搞定马上给分!!!
没有主键又怎么用 findByPrimaryKey 方法呢 ?
多个主键无所谓,jb会生成一个主键类作为主键...
********************************************************************************
// Primary key Class
import java.io.Serializable;public class ProductPK implements java.io.Serializable {
public String productID; public ProductPK() {}
public ProductPK(String productID) {
this.productID = productID;
}
public String toString() {
return productID.toString();
}
public int hashCode() {
return productID.hashCode();
}
public boolean equals(Object prod) {
return ((ProductPK)prod).productID.equals(productID);
}
}
********************************************************************************
// Remote Interface
import javax.ejb.*;
import java.rmi.RemoteException;public interface Product extends EJBObject
{ public String getName() throws RemoteException;
public void setName(String name) throws RemoteException;
public String getDescription() throws RemoteException;
public void setDescription(String description) throws RemoteException; public double getBasePrice() throws RemoteException;
public void setBasePrice(double price) throws RemoteException;
public String getProductID() throws RemoteException;
public void setProductID(String id) throws RemoteException;
}
********************************************************************************
// Home Interface
import javax.ejb.*;
import java.rmi.RemoteException;
import java.util.Collection;public interface ProductHome extends EJBHome
{
Product create(String productID, String name, String description, double basePrice) throws CreateException, RemoteException; public Product findByPrimaryKey(String key) throws FinderException, RemoteException; public Collection findByName(String name) throws FinderException, RemoteException;
public Collection findByDescription(String description) throws FinderException, RemoteException;
public Collection findByBasePrice(double basePrice) throws FinderException, RemoteException;
public Collection findExpensiveProducts(double minPrice) throws FinderException, RemoteException;
public Collection findCheapProducts(double maxPrice) throws FinderException, RemoteException;
public Collection findAllProducts() throws FinderException, RemoteException;
}
********************************************************************************
// EJB Class
import javax.ejb.*;public abstract class ProductBean implements EntityBean
{
protected EntityContext ctx;
public ProductBean() {} public abstract String getName();
public abstract void setName(String name);
public abstract String getDescription();
public abstract void setDescription(String description);
public abstract double getBasePrice();
public abstract void setBasePrice(double price);
public abstract String getProductID();
public abstract void setProductID(String productID); public void ejbActivate() {}
public void ejbRemove() {}
public void ejbPassivate() {}
public void ejbLoad() {}
public void ejbStore() {} public void setEntityContext(EntityContext ctx) {
this.ctx = ctx;
}
public void unsetEntityContext() {
this.ctx = null;
} public void ejbPostCreate(String productID, String name, String description, double basePrice) {}
public String ejbCreate(String productID, String name, String description, double basePrice) {
setProductID(productID);
setName(name);
setDescription(description);
setBasePrice(basePrice);
return productID;
}}********************************************************************************
***************************** XML File *******************************************
<?xml version="1.0"?><!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_2_0.dtd"><ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>Product</ejb-name>
<home>examples.ProductHome</home>
<remote>examples.Product</remote>
<local-home>examples.ProductLocalHome</local-home>
<local>examples.ProductLocal</local>
<ejb-class>examples.ProductBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>examples.ProductPK</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>ProductBean</abstract-schema-name>
<cmp-field>
<field-name>productID</field-name>
</cmp-field>
<cmp-field>
<field-name>name</field-name>
</cmp-field>
<cmp-field>
<field-name>description</field-name>
</cmp-field>
<cmp-field>
<field-name>basePrice</field-name>
</cmp-field>
<query>
<query-method> <method-name>findByName</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql>
<![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE name = ?1]]>
</ejb-ql>
</query> <query>
<query-method>
<method-name>findByDescription</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql>
<![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE description = ?1]]>
</ejb-ql>
</query> <query>
<query-method>
<method-name>findByBasePrice</method-name>
<method-params>
<method-param>double</method-param>
</method-params>
</query-method>
<ejb-ql>
<![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE basePrice = ?1]]>
</ejb-ql>
</query> <query>
<query-method>
<method-name>findExpensiveProducts</method-name>
<method-params>
<method-param>double</method-param>
</method-params>
</query-method>
<ejb-ql>
<![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE basePrice > ?1]]>
</ejb-ql>
</query> <query>
<query-method>
<method-name>findCheapProducts</method-name>
<method-params>
<method-param>double</method-param>
</method-params>
</query-method>
<ejb-ql>
<![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE basePrice < ?1]]>
</ejb-ql>
</query> <query>
<query-method>
<method-name>findAllProducts</method-name>
<method-params>
</method-params>
</query-method>
<ejb-ql>
<![CDATA[SELECT OBJECT(a) FROM ProductBean AS a WHERE productID IS NOT NULL]]>
</ejb-ql>
</query> </entity>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Product</ejb-name>
<method-intf>Remote</method-intf>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor></ejb-jar>
********************************************************************************