entity bean 用Collection selectByLastname(String name)取回的記錄中關鍵欄位是重複的
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();
}
}
比如關鍵字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();
}
}
相關文章
- mysql去除某些欄位重複的紀錄MySql
- sql根據多個欄位查詢重複記錄SQL
- 能給我講講 collection ,entity_bean的關係嗎? 一個實體bean對應一張表,那麼collection是怎麼回事?Bean
- awk 中的欄位、記錄和變數變數
- 查mysql欄位中的數字記錄MySql
- 關於SQL的重複記錄問題SQL
- 用SQL語句去掉重複的記錄SQL
- 關於oracle中blob欄位的錄入問題Oracle
- 資料庫中怎麼取回剛剛插入的記錄?資料庫
- MYSQL中刪除重複記錄的方法薦MySql
- Oracle 刪除表中重複記錄的DELETE SQLOracledeleteSQL
- 刪除重複id的記錄
- SpingMVC框架中去掉List中重複bean的方法MVC框架Bean
- db2中刪除重複記錄的問題DB2
- 查詢/刪除重複的資料(單個欄位和多個欄位條件)
- Oracle如何刪除表中重複記錄Oracle
- 查詢刪除表中重複記錄
- session bean 對 entity bean的訪問策略?SessionBean
- Oracle刪重複記錄Oracle
- SQL Server複製的表中如何修改欄位SQLServer
- 用SQL語句刪除重複記錄的四種方法SQL
- Oracle 查詢重複記錄,以及簡單的sql應用。OracleSQL
- MyBatis 多表聯合查詢,欄位重複的解決方法MyBatis
- 欄位修改記錄操作日誌的實現
- db2刪除重複的記錄DB2
- oracle-快速刪除重複的記錄Oracle
- 去除站點的資料庫欄位中包含的關鍵詞,並恢復資料庫
- Spring中給靜態欄位(field)注入beanSpringBean
- 高效快速刪除Oracle表中重複記錄Oracle
- Spring竟然可以建立“重複”名稱的bean?—一次專案中存在多個bean名稱重複問題的排查SpringBean
- 更新一張與另一張表關聯的連線欄位記錄
- 刪除Oracle重複記錄Oracle
- SQL重複記錄查詢SQL
- oracle刪除重複記錄Oracle
- 對錶中的欄位設定了預設值,新增記錄後卻發現該欄位為nullNull
- more effective entity bean(新的改進entity bean的效能的七條(EJB2.0版)) (轉)Bean
- MySql避免重複插入記錄的幾種方法MySql
- 在Oracle中找出重複的紀錄的方法(轉)Oracle