entity bean 用Collection selectByLastname(String name)取回的记录中关键字段是重复的

05-04-28 huhongbo

entity bean 用Collection selectByLastname(String name)取回的记录中关键字段是重复的,而其他字段是正常的。运行测试程序后,第一行中除关键字外,其余字段变成了符合查询条件的最后的一条记录。数据库为ms sql ,id为自动增长的,ejbCreate()方法为insert into userDesc(name,address) values(?,?),关键字id作为UserDescPK类,在传回的vector中,放入UserDescPK.

比如关键字id值为 数据库中记录是

id name address

0 0value oaddress

1 1value 1address

5 5value 5address

打印出来后,如下

id name address

0 0value oaddress

0 1value 1address <-----id值由1变为0

0 5value 5address <-----id值由1变为0

再到数据库中查询,如下

id name address

0 5value 5address <-----值由0value,0address变化成5value,5address.而我没有操作变化。

1 1value 1address

5 5value 5address

原码如下:

---------------------------------

---------------------------------

package com.hhb;

import javax.ejb.EntityBean;

import javax.ejb.EntityContext;

import javax.ejb.CreateException;

import javax.ejb.RemoveException;

import java.sql.Connection;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.sql.DataSource;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import javax.ejb.FinderException;

import java.rmi.RemoteException;

import java.util.Collection;

import java.util.Vector;

public class UserDescBean

implements EntityBean {

EntityContext entityContext;

private int id;

private String name;

private String email;

private String address;

private UserDescDetail userDescDetail = new UserDescDetail();

public int getId() {

return id;

}

public String getName() {

return name;

}

public String getEmail() {

return email;

}

public String getAddress() {

return address;

}

public EntityContext getEntityContext() {

return entityContext;

}

public UserDescDetail getUserDescDetail() {

userDescDetail.setId(id);

userDescDetail.setAddress(address);

userDescDetail.setMail(email);

userDescDetail.setName(name);

return userDescDetail;

}

/**

* ejbCreate

* @param id int

* @param name String

* @param email String

* @param address String

* @throws CreateException

* @return UserDescPK

*/

public UserDescPK ejbCreate(int id, String name, String email, String address) throws

CreateException {

setId(id);

setName(name);

setEmail(email);

setAddress(address);

PreparedStatement psmt = null;

Connection conn = null;

String sql = "insert into userDesc(name,mail,address) values(?,?,?)";

String idSql = "select max(id) from userDesc";

try {

conn = getConnection();

psmt = conn.prepareStatement(sql);

//psmt.setInt(1, id);

psmt.setString(1, name);

psmt.setString(2, email);

psmt.setString(3, address);

psmt.execute();

//获取最大

psmt = conn.prepareStatement(sql);

ResultSet rset = psmt.executeQuery();

id = rset.getInt(1);

return new UserDescPK(id);

}

catch (Exception ex) {

ex.printStackTrace();

throw new CreateException(ex.getMessage());

}

finally {

try {

if (psmt != null) {

psmt.close();

}

if (conn != null) {

conn.close();

}

}

catch (Exception ex) {

ex.printStackTrace();

}

}

} //end ejbCreate

public void ejbPostCreate(int id, String name, String email, String address) throws

CreateException {

}

public void ejbRemove() throws RemoveException {

UserDescPK key = (UserDescPK) entityContext.getPrimaryKey();

int intKey = key.id;

PreparedStatement pstmt = null;

Connection conn = null;

try {

conn = getConnection();

pstmt = conn.prepareStatement("delete from userDesc where id = ?");

pstmt.setInt(1, intKey);

pstmt.execute();

}

catch (Exception ex) {

ex.printStackTrace();

}

finally {

try {

if (pstmt != null) {

pstmt.close();

}

}

catch (Exception e) {}

try {

if (conn != null) {

conn.close();

}

}

catch (Exception e) {}

}

}

public void ejbLoad() {

UserDescPK key = (UserDescPK) entityContext.getPrimaryKey();

int intKey = key.id;

PreparedStatement pstmt = null;

Connection conn = null;

try {

conn = getConnection();

pstmt = conn.prepareStatement("select * from userDesc where id = ?");

pstmt.setInt(1, intKey);

ResultSet rs = pstmt.executeQuery();

rs.next();

name = rs.getString(2);

email = rs.getString(3);

address = rs.getString(4);

}

catch (Exception ex) {

ex.printStackTrace();

}

finally {

try {

if (pstmt != null) {

pstmt.close();

}

}

catch (Exception e) {}

try {

if (conn != null) {

conn.close();

}

}

catch (Exception e) {}

}

}

/**

* ejbStore

*/

public void ejbStore() {

PreparedStatement psmt = null;

Connection conn = null;

String sql = "update userDesc set name=?,mail=?,address=? where id=?";

try {

conn = getConnection();

psmt = conn.prepareStatement(sql);

//psmt.setInt(1, id);

psmt.setString(1, name);

psmt.setString(2, email);

psmt.setString(3, address);

psmt.setInt(4, id);

psmt.executeUpdate();

}

catch (Exception ex) {

ex.printStackTrace();

}

finally {

try {

if (psmt != null) {

psmt.close();

}

if (conn != null) {

conn.close();

}

}

catch (Exception ex) {

ex.printStackTrace();

}

}

}

public void ejbActivate() {

}

public void ejbPassivate() {

}

public void setEntityContext(EntityContext entityContext) {

this.entityContext = entityContext;

}

public void setId(int id) {

this.id = id;

}

public void setName(String name) {

this.name = name;

}

public void setEmail(String email) {

this.email = email;

}

public void setAddress(String address) {

this.address = address;

}

public void setUserDescDetail(UserDescDetail userDescDetail) {

this.userDescDetail = userDescDetail;

id = userDescDetail.getId();

name = userDescDetail.getName();

address = userDescDetail.getAddress();

email = userDescDetail.getMail();

}

public void unsetEntityContext() {

this.entityContext = null;

}

public UserDescPK ejbFindByPrimaryKey(UserDescPK key) throws FinderException {

String sql = "select * from userDesc where id = ?";

Connection conn = null;

PreparedStatement psmt = null;

try {

conn = getConnection();

psmt = conn.prepareStatement(sql);

psmt.setInt(1, key.id);

ResultSet reset = psmt.executeQuery();

while(reset.next()){

id = reset.getInt(1);

name = reset.getString(2);

email = reset.getString(3);

address = reset.getString(4);

}

return key;

}

catch (Exception ex) {

ex.printStackTrace();

throw new FinderException(ex.toString());

}

finally {

try {

if (psmt != null) {

psmt.close();

}

if (conn != null) {

conn.close();

}

}

catch (Exception ex) {

ex.printStackTrace();

}

}

}

public Collection ejbFindByName(String name) throws FinderException {

String sql = "select * from userDesc where name = ?";

Connection conn = null;

PreparedStatement psmt = null;

Vector v = new Vector();

try {

conn = getConnection();

psmt = conn.prepareStatement(sql);

psmt.setString(1, name);

ResultSet reset = psmt.executeQuery();

while (reset.next()) {

System.out.println("primary key = " + reset.getInt("id"));

v.addElement(new UserDescPK(reset.getInt("id")));

}

for (int i = 0; i < v.size(); i++) {

System.out.println("v key= " + v.get(i).toString());

}

return v;

}

catch (Exception ex) {

ex.printStackTrace();

throw new FinderException(ex.toString());

}

finally {

try {

if (psmt != null) {

psmt.close();

}

if (conn != null) {

conn.close();

}

}

catch (Exception ex) {

ex.printStackTrace();

}

}

}

/**

* getConnect

*/

public Connection getConnection() throws Exception {

try {

Context ctx = new InitialContext();

DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/ejbPool");

return ds.getConnection();

}

catch (Exception ex) {

ex.printStackTrace();

throw ex;

}

}

}

-----------------------------------------

-----------------------------------------

package com.hhb;

import java.io.Serializable;

/**

* 设置信息,利于分析

* <p>Title: </p>

* <p>Description: </p>

* <p>Copyright: Copyright (c) 2005</p>

* <p>Company: </p>

* @author not attributable

* @version 1.0

*/

public class UserDescDetail implements Serializable{

private int id;

private String name;

private String mail;

private String address;

public int getId() {

return id;

}

public String getName() {

return name;

}

public String getMail() {

return mail;

}

public void setAddress(String address) {

this.address = address;

}

public void setId(int id) {

this.id = id;

}

public void setName(String name) {

this.name = name;

}

public void setMail(String mail) {

this.mail = mail;

}

public String getAddress() {

return address;

}

public UserDescDetail() {

}

}

=====================================

=====================================

package com.hhb;

import javax.ejb.EJBObject;

import java.rmi.RemoteException;

import java.sql.ResultSet;

public interface UserDesc

extends EJBObject {

public int getId() throws RemoteException;

public void setId(int id) throws RemoteException;

public void setName(String name) throws RemoteException;

public String getName() throws RemoteException;

public void setEmail(String email) throws RemoteException;

public String getEmail() throws RemoteException;

public void setAddress(String address) throws RemoteException;

public String getAddress() throws RemoteException;

public UserDescDetail getUserDescDetail() throws RemoteException;

public void setUserDescDetail(UserDescDetail userDescDetail) throws

RemoteException;

}

==========================================

==========================================

package com.hhb;

import javax.ejb.EJBHome;

import javax.ejb.CreateException;

import java.rmi.RemoteException;

import javax.ejb.FinderException;

import java.util.Collection;

public interface UserDescHome

extends EJBHome {

public UserDesc create(int id, String name, String email, String address) throws

CreateException, RemoteException;

public UserDesc findByPrimaryKey(UserDescPK pk) throws

FinderException, RemoteException;

public Collection findByName(String name) throws FinderException,

RemoteException;

}

========================================

=========================================

package com.hhb;

import java.io.Serializable;

public class UserDescPK

implements Serializable {

public int id;

public UserDescPK() {

}

public UserDescPK(int id) {

this.id = id;

}

public boolean equals(Object obj) {

if (this == obj) {

return true;

}

if (! (obj instanceof UserDescPK)) {

return false;

}

UserDescPK that = (UserDescPK) obj;

if (that.id != this.id) {

return false;

}

return true;

}

public int hashCode() {

int result = 17;

result = 37 * result + (int)this.id;

return result;

}

public String toString(){

return Integer.toString(id);

}

测试的方法为

public void printCol(){

try {

Collection col = userDescHome.findByName("ch");

Iterator itor = col.iterator();

int ii = 0 ;

while (itor.hasNext()) {

UserDesc acc = (UserDesc)itor.next();

System.out.println("---------------------------");

ii++;

System.out.println("acc"+ ii +"=" + acc.getId());

System.out.println("acc"+ ii +"=" +acc.getName());

System.out.println("acc"+ ii +"=" + acc.getAddress());

}

}

catch (Exception ex) {

ex.printStackTrace();

}

}