总结1:
以下面这行代码为例:
PersonDaoBeanRemote pdb = (PersonDaoBeanRemote)ctx.lookup("PersonDaoBean/remote");
说明:
PersonDaoBeanRemote:为远程接口
PersonDaoBean:为会话bean
报错:
1、如果写成:
PersonDaoBean pdb = (PersonDaoBean)ctx.lookup("PersonDaoBean/remote");
报错:
java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to cn.com.zy.ebj.dao.bean.PersonDaoBean
at com.sise.lab1.test.SumBeanTest.main(SumBeanTest.java:23)
2、如果写成:
PersonDaoBean pdb = (PersonDaoBean)ctx.lookup("PersonDaoBeanRemote/remote");
或:
PersonDaoBeanRemote pdb = (PersonDaoBeanRemote)ctx.lookup("PersonDaoBeanRemote/remote");
报错:
javax.naming.NameNotFoundException: PersonDaoBeanRemote not bound
总结:
JNDI查找用法:
远程接口 远程接口对象 = (远程接口)ctx.lookup(“会话bean/remote”);
总结2:
以下为例:
Query query = em.createQuery("select p from Person p where p.sex='男'");
说明:
person:表名
Person:实体名
(注意:大小写)
报错:
1、如果写成(查询用表名,没用实体名):
Query query = em.createQuery("select p from person p where p.sex='男'");
报错:
javax.ejb.EJBException: java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: person is not mapped [select p from person p where p.sex='男']; nested exception is: java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: person is not mapped [select p from person p where p.sex='男']
2、如果写成(用sql语句查询):
Query query = em.createQuery("select * from person where sex='男'");
或:
Query query = em.createQuery("select * from Person where sex='男'");
报错:
javax.ejb.EJBException: java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [select * from person where sex='男']; nested exception is: java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [select * from person where sex='男']
总结:
查询语句不要用表名,要用实体名查询
总结3:
mysql数据源的配置:
步骤:
1、修改“JBOSS安装目录\docs\examples\jca”目录下的mysql-ds.xml:
说明:
<user-name>******</user-name>:设置安装的mysql的用户名
<password>********</password>:设置安装的mysql的密码
<jndi-name>MySqlDS</jndi-name>:为数据源的名,引用为:java:/MySqlDS,(可自行修改数据源名,但引用要与之对应上)
<connection-url>jdbc:mysql://数据源地址:3306/数据库名</connection-url>:数据源地址为IP地址,本地可用localhost,数据库名为要访问的数据库名
2、将修改后的mysql-ds.xml拷贝到“JBOSS安装目录\server\default\deploy”目录下
3、将mysql驱动(如mysql-connectot-java-5.1.9-bin,jar)拷贝到“JBOSS安装目录\server\default\lib”目录下
注:
在要用mysql数据库时一定要引入mysql驱动包
总结4:
EJB项目一定要引入“JBOSS安装目录\client”目录下的包