初学者的问题(急):ejb3通过jndi连接oracle10g数据库出现的问题。

CXYZJ 2010-02-03 09:53:00
刚学ejb3,照着教程连接Mysql数据库顺利完成,但自己试着连接oracle10g快捷版时出现了一些问题,自己搞了好久都没有搞定,只能上来这里求助,请大虾们帮忙。我用的是jboss,驱动和数据源配置文件都没问题的,下面先贴出我写的东西:
1,persistence.xml
<?xml version= "1.0 " encoding= "UTF-8 "?>
<persistence xmlns= "http://java.sun.com/xml/ns/persistence "
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation= "http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd " version= "1.0 ">
<persistence-unit name= "EJBUserPU " transaction-type= "JTA ">
<jta-data-source> java:/DefaultOracleDS </jta-data-source>
<properties>
<!-- 显示最终执行的SQL -->
<property name= "hibernate.show_sql " value= "true "/>
<!-- 格式化显示的SQL -->
<property name= "hibernate.format_sql " value= "true "/>
</properties>
</persistence-unit>
</persistence>
2,单表映射实体(表建好的了)
package huizhi.sessionbean;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name= "user_zxl ")
public class User implements Serializable{

private int id;
private String name;
private int age;
private String phone;
private String address;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}

@Column(nullable=true)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}

@Column(nullable=true)
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}

@Column(nullable=true)
public String getPhone() {
return this.phone;
}
public void setPhone(String phone) {
this.phone = phone;
}

@Column(nullable=true)
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
}
3,实体操作实例
package huizhi.sessionbean;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
@Remote(UserDao.class)
public class UserDaobean implements UserDao {
@PersistenceContext(unitName= "EJBUserPU ") protected EntityManager em;
public void deleteUser(int userid) {
User user=em.find(User.class, userid);
if(user!=null) em.remove(user);
}

public User getUserByID(int userid) {
return em.find(User.class, userid);
}

@SuppressWarnings( "unchecked ")
public List <User> getUserList() {
Query query=em.createQuery( "select o from User o order by o.id asc ");
return (List <User> )query.getResultList();
}

public void insertUser(User user) {
em.persist(user);

}

public void mergeUser(User user) {
em.merge(user);

}

public void updateName(String newname, int userid) {
User user=em.find(User.class, userid);
if(user!=null) user.setName(newname);
}

}
4,jsp测试页面
<%@ page language= "java " import= "java.util.* " pageEncoding= "ISO-8859-1 "%>
<%@ page import= "huizhi.sessionbean.*,javax.naming.*,java.util.List " %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ ":// "+request.getServerName()+ ": "+request.getServerPort()+path+ "/ ";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN ">
<html>
<head>
<base href= " <%=basePath%> ">

<title> My JSP 'index.jsp ' starting page </title>
<meta http-equiv= "pragma " content= "no-cache ">
<meta http-equiv= "cache-control " content= "no-cache ">
<meta http-equiv= "expires " content= "0 ">
<meta http-equiv= "keywords " content= "keyword1,keyword2,keyword3 ">
<meta http-equiv= "description " content= "This is my page ">
<!--
<link rel= "stylesheet " type= "text/css " href= "styles.css ">
-->
</head>

<body>
<table width= "80% " border= "1 ">
<tr bgcolor= "#DFDFDF ">
<td> id </td>
<td> name </td>
<td> age </td>
<td> phone </td>
<td> address </td>
</tr>
<%
try{
InitialContext ctx = new InitialContext();
UserDao userdao=(UserDao)ctx.lookup( "UserDaobean/remote ");
userdao.updateName( "lili ",1);
List <User> users = userdao.getUserList();
for(User user:users){
out.println( " <tr> <td> "+user.getId()+ " </td> <td> "+user.getName()+ " </td> <td> "+user.getAge()+ " </td> <td> "+user.getPhone()+ " </td> <td> "+user.getAddress()+ " </td> </tr> ");
}
}catch(Exception e){
out.println(e);
}
%>
</table>
</body>
</html>
最后是报错信息:
16:39:45,515 INFO [STDOUT] Hibernate:
select
user0_.id as id4_0_,
user0_.address as address4_0_,
user0_.age as age4_0_,
user0_.name as name4_0_,
user0_.phone as phone4_0_
from
user_zxl user0_
where
user0_.id=?
16:39:45,515 WARN [JDBCExceptionReporter] SQL Error: 942, SQLState: 42000
16:39:45,515 ERROR [JDBCExceptionReporter] ORA-00942: 表或视图不存在

16:31:37,546 INFO [STDOUT] Hibernate:
select
user0_.id as id2_0_,
user0_.address as address2_0_,
user0_.age as age2_0_,
user0_.name as name2_0_,
user0_.phone as phone2_0_
from
"user_zxl " user0_
where
user0_.id=?
16:31:37,562 WARN [JDBCExceptionReporter] SQL Error: 904, SQLState: 42000
16:31:37,562 ERROR [JDBCExceptionReporter] ORA-00904: "USER0_ ". "ID ": 标识符无效

据我自己所知,oracle数据库的查询语名中的字段名和表名是要加双引号的,例如,select "name " from "user_zxl ";可是用ejb连接的时候,那些语句是自动生成的,请问大虾们要怎么解决啊,这个问题困扰了我好久。
...全文
79 6 打赏 收藏 举报
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
shiyiwan 2010-02-03
  • 打赏
  • 举报
回复
1.oracle查询不需要双引号;
2.表建在哪个schema下面了?该schema是否和DefaultOracleDS中设置的登陆用户名相同?
CXYZJ 2010-02-03
  • 打赏
  • 举报
回复
自己先顶!~高手赶紧来吧~~~~~~~~~~~
crazylaa 2010-02-03
  • 打赏
  • 举报
回复
简单,把你数据库的建表语句导出来,然后把里面的表名、字段名前面的双引号去掉,然后重新create table即可。估计你是用某种工具(比如PD)create table的导致加了双引号。
roylogic 2010-02-03
  • 打赏
  • 举报
回复
谁说oracle查询要加双引号的,不用的。
CXYZJ 2010-02-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 shiyiwan 的回复:]
1.oracle查询不需要双引号;
2.表建在哪个schema下面了?该schema是否和DefaultOracleDS中设置的登陆用户名相同?
[/Quote]

就是在system下面,用户名密码都相同的
相关推荐
发帖
Oracle 高级技术

3472

社区成员

Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
帖子事件
创建了帖子
2010-02-03 09:53
社区公告
暂无公告