EJB-QL问题

03-09-02 trainking
我做了一个CMP,程序如下:
Customer.java
package MyCustomerEJB;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import java.util.*;
public interface Customer extends EJBObject {
public void setSsn(java.lang.String ssn) throws RemoteException;
public java.lang.String getSsn()throws RemoteException;
public void setName(java.lang.String name)throws RemoteException;
public java.lang.String getName()throws RemoteException;
public void setPassword(java.lang.String password)throws RemoteException;
public java.lang.String getPassword()throws RemoteException;
}

CustomerHome.java
package MyCustomerEJB;
import java.util.Collection;
import java.rmi.RemoteException;
import javax.ejb.*;
public interface CustomerHome extends EJBHome {
public Customer create(java.lang.String ssn,java.lang.String name,java.lang.String password)
throws CreateException,RemoteException;
public java.util.Collection findByName(java.lang.String name)
throws FinderException,RemoteException;
public Customer findByPrimaryKey(java.lang.String ssn)
throws FinderException,RemoteException;
}

CustomerBean.java
package MyCustomerEJB;
import javax.ejb.*;
import javax.naming.*;
public abstract class CustomerBean implements EntityBean {
EntityContext entityContext;
public java.lang.String ejbCreate(java.lang.String ssn, java.lang.String name, java.lang.String password) throws CreateException {
setSsn(ssn);
setName(name);
setPassword(password);
return null;
}

public void ejbPostCreate(java.lang.String ssn, java.lang.String name,java.lang.String password) throws CreateException { }

public void ejbRemove() throws RemoveException { }

public abstract void setSsn(java.lang.String ssn);
public abstract void setName(java.lang.String name);
public abstract void setPassword(java.lang.String password);
public abstract java.lang.String getPassword();
public abstract java.lang.String getName();
public abstract java.lang.String getSsn();

public void ejbLoad() { }
public void ejbStore() { }
public void ejbActivate() { }
public void ejbPassivate() { }
public void unsetEntityContext() {
this.entityContext = null;
}
public void setEntityContext(EntityContext entityContext) {
this.entityContext = entityContext;
}
配置文件如下:
ejb-jar.xml
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN'
'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>CustomerCMPBean</ejb-name>
<home>MyCustomerEJB.CustomerHome</home>
<remote>MyCustomerEJB.Customer</remote>
<ejb-class>MyCustomerEJB.CustomerBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.String</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>CustomerBean</abstract-schema-name>
<cmp-field>
<field-name>ssn</field-name>
</cmp-field>
<cmp-field>
<field-name>name</field-name>
</cmp-field>
<cmp-field>
<field-name>password</field-name>
</cmp-field>
<primkey-field>ssn</primkey-field>
<query>
<description>finds customers in a given name</description>
<query-method>
<method-name>findByName</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql>
<![CDATA[FROM CustomerBean s WHERE s.name=?1]]>
</ejb-ql>
</query>
</entity>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>CustomerCMPBean</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>

weblogic-ejb-jar.xml
<?xml version="1.0"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 6.0.0 EJB//EN"

"http://www.bea.com/servers/wls600/dtd/weblogic-ejb-jar.dtd" >
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>CustomerCMPBean</ejb-name>
<entity-descriptor>
<persistence>
<persistence-type>
<type-identifier>WebLogic_CMP_RDBMS</type-identifier>
<type-version>6.0</type-version>
<type-storage>META-INF/weblogic-cmp-rdbms.xml</type-storage>
</persistence-type>
<persistence-use>
<type-identifier>WebLogic_CMP_RDBMS</type-identifier>
<type-version>6.0</type-version>
</persistence-use>
</persistence>
</entity-descriptor>
<jndi-name>CMPCustomerEJB</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>

weblogic-cmp-rdbms.xml
<?xml version="1.0"?>
<!DOCTYPE weblogic-rdbms-jar PUBLIC
'-//BEA Systems, Inc.//DTD WebLogic 6.0.0 EJB RDBMS Persistence//EN'
'http://www.bea.com/servers/wls600/dtd/weblogic-rdbms20-persistence-600.dtd'>
<weblogic-rdbms-jar>
<weblogic-rdbms-bean>
<ejb-name>CustomerCMPBean</ejb-name>
<data-source-name>CustomerDB</data-source-name>
<table-name>customer</table-name>
<field-map>
<cmp-field>ssn</cmp-field>
<dbms-column>SSN</dbms-column>
</field-map>
<field-map>
<cmp-field>name</cmp-field>
<dbms-column>NAME</dbms-column>
</field-map>
<field-map>
<cmp-field>password</cmp-field>
<dbms-column>PASSWORD</dbms-column>
</field-map>
</weblogic-rdbms-bean>
</weblogic-rdbms-jar>

当我做到java weblogic.ejbc pre_CMPCustomerEJB.jar CMPCustomerEJB.jar时,出现错误如下:
ERROR: Error from ejbc: Error while reading 'META-INF/weblogic-cmp-rdbms.xml'. T
he error was:
weblogic.ejb20.cmp.rdbms.finders.IllegalExpressionException:
While trying to process Finder
Method Name: findByName
Parameter Types: (java.lang.String)
EJB Query: FROM CustomerBean s WHERE s.name=?1
)
Could not parse EJB QL expression: FROM CustomerBean s WHERE s.name=?1
ERROR: ejbc found errors
可是我不觉得EJB QL有什么错误,恳请大家帮我看看,到底哪里错了,谢谢!

trainking
2003-09-02 11:04
我的ejb-jar.xml中
ejb-name:CustomerCMPBean
abstract-schema-name:CustomerBean
method-name:findByName
method-param:java.lang.String
ejb-ql:FROM CustomerBean s WHERE s.name=?1
容器管理的字段是:ssn,name,password

weblogic-cmp-rdbms.xml中
字段映射关系为:ssn――SSN,name――NAME,password――PASSWORD

banq
2003-09-02 11:50
ejb-ql写法有误吧,使用code按钮可以原封不动发表code代码。



SELECT OBJECT(s) FROM Customer AS s WHERE s.lastName=?1
<p class="indent">


其中Customer是你的abstract-schema-name,一般abstract-schema-name设定和ejb-name是一致的。只要更改这个ejb-ql中的Customer和lastName就可以实现你的数据库查询。

有关EJB-QL参考:http://www.ejb-ql.com/

trainking
2003-09-02 12:31
banq,我按照你的做法再试了一次,还是老问题。

ejb-jar.xml

<?xml version="1.0"?>

<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 
 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>

<ejb-jar>
  <enterprise-beans>
     <entity>
        <ejb-name>CustomerCMPBean</ejb-name>
        <home>MyCustomerEJB.CustomerHome</home>
        <remote>MyCustomerEJB.Customer</remote>
        <ejb-class>MyCustomerEJB.CustomerBean</ejb-class>
        <persistence-type>Container</persistence-type>
        <prim-key-class>java.lang.String</prim-key-class>
        <reentrant>False</reentrant>
        <cmp-version>2.x</cmp-version>
        <abstract-schema-name>CustomerCMPBean</abstract-schema-name>

        <cmp-field>
           <field-name>ssn</field-name>
        </cmp-field>
        <cmp-field>
           <field-name>name</field-name>
        </cmp-field>
        <cmp-field>
           <field-name>password</field-name>
        </cmp-field>

        <primkey-field>ssn</primkey-field>

        <query>
           <description>finds customers in a given name</description>
           <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(s) FROM CustomerCMPBean AS s WHERE s.name=?1]]>
           </ejb-ql>
        </query>
     </entity>
  </enterprise-beans>

<assembly-descriptor>
   <container-transaction>
      <method>
          <ejb-name>CustomerCMPBean</ejb-name>
          <method-name>*</method-name>
      </method>
      <trans-attribute>Required</trans-attribute>
   </container-transaction>
</assembly-descriptor>

</ejb-jar>
<p class="indent">


weblogic-cmp-rdbms.xml

<?xml version="1.0"?>

<!DOCTYPE weblogic-rdbms-jar PUBLIC 
 '-//BEA Systems, Inc.//DTD WebLogic 6.0.0 EJB RDBMS Persistence//EN'
 'http://www.bea.com/servers/wls600/dtd/weblogic-rdbms20-persistence-600.dtd'>

<weblogic-rdbms-jar>
  <weblogic-rdbms-bean>
    <ejb-name>CustomerCMPBean</ejb-name>
    <data-source-name>CustomerDB</data-source-name>
    <table-name>customer</table-name>
    <field-map>
      <cmp-field>ssn</cmp-field>
      <dbms-column>SSN</dbms-column>
    </field-map>
    <field-map>
      <cmp-field>name</cmp-field>
      <dbms-column>NAME</dbms-column>
    </field-map>
    <field-map>
      <cmp-field>password</cmp-field>
      <dbms-column>PASSWORD</dbms-column>
    </field-map>
</weblogic-rdbms-bean>
</weblogic-rdbms-jar>
<p class="indent">

请再帮我看看,好吗,谢谢!

jd2bs
2003-09-02 16:02
SELECT OBJECT(s) FROM Customer AS s WHERE s.lastName = ?1

trainking
2003-09-02 16:58
还是不行。
一定是这句EJB-QL错了吗?会不会是其他什么地方有问题,从而使得不能解析这句ejb-ql?

mk_008
2003-09-02 18:12
是空格问题,注意等号两边的空格
WHERE s.name = ?1

trainking
2003-09-03 12:17
问题解决了,的确是空格的问题,原来等号两边都要有空格。
谢谢大家!

jbwang
2003-09-03 12:52
在ql语句,使用ejb的abstract-schema-name,不是ejb-name