嗯,你这个多个记录指什么?是多个重复记录?还是说有可能重复,有可能不重复的记录?findByPrimaryKey你可以重写一个自己的方法

解决方案 »

  1.   

    能给个简单例子么?我的多个记录指又可能重复,也有可能不重复。现在考虑不可能重复的!因为
    不可能重复需要解决并发问题,我还不知道在Entity Bean怎么控制并发问题!现在能给一个不可
    能重复的例子么?findByPrimaryKey方法和create方法怎么写?急!!!
      

  2.   

    你把他下成bmp的entitybean就没问题吧
      

  3.   

    一个实体bean对象的一个实例对应一条记录,是通过findByPrimaryKey()方法定位的!如果数据库表又一个主建可以获得。但是如果表没有主建或者有多个主建又该如何?那位有做过方面的,能详细
    介绍一下,最好给个例子,谢先了!搞定马上给分!!!
      

  4.   

    不明白你的数据库里的表为什么不定义主键 ?
    没有主键又怎么用 findByPrimaryKey 方法呢 ?
    多个主键无所谓,jb会生成一个主键类作为主键...
      

  5.   

    没有主见肯定不行了,我以前碰到过这样的问题(建立表时忘了设置KEY),结果不能操作数据库(新增、修改、删除),后来查了半天的错误都没有找到原因,还是无意中发现了这个弱治的失误。所以没有主见恐怕不行吧!
      

  6.   

    我是在生成时,控制的!也就是有一个字段:id,类型int,我每次都是取最大值+1插入!所以没有定义为主键,这样可以么?另外如果是视图呢?也是如此么?
      

  7.   

    是不是库的每一个表都要对应一个Entity Bean?
      

  8.   

    同意zez。没有主键不能查询多条记录,指针无法定位
      

  9.   

    这是一个例子。一共对应4个文件(3个java文件,1个XML)
    ********************************************************************************
    // 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>
    ********************************************************************************