0

我在用户登录程序中遇到了一个奇怪的场景。

  1. 插入记录..用户名密码等。
  2. 使用merge()插入记录;
  3. 然后关闭 IDE (Netbeans)
  4. 打开 IDE Netbeans 然后启动服务器,启动数据库连接。
  5. 打开登录浏览器。
  6. 使用插入的记录登录。
  7. 我的程序无法检测到桌子上的记录。

调试时,在 find() 之后它不会填充我的实体。也许还有另一个步骤来填充实体?

登录动作

package lotmovement.action;

import com.opensymphony.xwork2.ActionSupport;
import lotmovement.business.crud.RecordExistUserProfile;
import org.apache.commons.lang3.StringUtils;


public class LoginAction extends ActionSupport{

    private String userName;
    private RecordExistUserProfile recordExistUserProfile;


    private String password;

    @Override
    public void validate(){

        if(StringUtils.isEmpty(getUserName())){
            addFieldError("userName","Username must not be blanks.");

                }

        else{

            if(!recordExistUserProfile.checkrecordexist(getUserName())){
                addFieldError("userName","Username don't exist.");
            }



        }        



        if(StringUtils.isEmpty(getPassword())){
            addFieldError("password","Password must not be blanks.");
        }
        else{

         if(!recordExistUserProfile.CheckPasswordCorrect(getUserName(), getPassword())){
                addFieldError("userName","Password not correct");
            }


        }



    }


    public String execute(){

       return SUCCESS;
    }


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

      public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }   



     public RecordExistUserProfile getRecordExistUserProfile() {
        return recordExistUserProfile;
    }

    public void setRecordExistUserProfile(RecordExistUserProfile recordExistUserProfile) {
        this.recordExistUserProfile = recordExistUserProfile;
    }




}

验证程序

/* * 要更改此模板,请选择工具 | 模板 * 并在编辑器中打开模板。*/ 包 lotmovement.business.crud;

导入lotmovement.business.entity.UserProfile;

/** * * @author god-gavedmework */ public class RecordExistUserProfile {

private EntityStart entityStart;
private UserProfile userProfile;


public boolean checkrecordexist(String userId) {
    entityStart.StartDbaseConnection();
    entityStart.em.find(UserProfile.class, userId);

    if (userId.equals(userProfile.getUserId())) {
        return true;
    } else {
        return false;
    }
}

public boolean CheckPasswordCorrect(String userId, String password) {
    entityStart.StartDbaseConnection();

    entityStart.em.find(UserProfile.class, userId);

    if (password.equals(userProfile.getPassword())) {
        return true;
    } else {
        return false; ---> It will step here.
    }

}



public UserProfile getUserProfile() {
    return userProfile;
}

public void setUserProfile(UserProfile userProfile) {
    this.userProfile = userProfile;
}

public EntityStart getEntityStart() {
    return entityStart;
}

public void setEntityStart(EntityStart entityStart) {
    this.entityStart = entityStart;
}

}

实体

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lotmovement.business.entity;
import java.io.Serializable;
import javax.persistence.*;


/**
 *
 * @author god-gavedmework
 */

@Entity(name = "USERPROFILE") //Name of the entity
public class UserProfile implements Serializable{

    @Id //signifies the primary key
    @Column(name = "USER_ID", nullable = false,length = 20)


    private String userId;


    @Column(name = "PASSWORD", nullable = false,length = 20)
    private String password;

    @Column(name = "FIRST_NAME", nullable = false,length = 20)

    private String firstName;
    @Column(name = "LAST_NAME", nullable = false,length = 50)

    private String lastName;

    @Column(name = "SECURITY_LEVEL", nullable = false,length = 4)
    private int securityLevel; 


    @Version
    @Column(name = "LAST_UPDATED_TIME")
    private java.sql.Timestamp updatedTime;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getSecurityLevel() {
        return securityLevel;
    }

    public void setSecurityLevel(int securityLevel) {
        this.securityLevel = securityLevel;
    }

    public java.sql.Timestamp getUpdatedTime() {
        return updatedTime;
    }

    public void setUpdatedTime(java.sql.Timestamp updatedTime) {
        this.updatedTime = updatedTime;
    }

}


*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lotmovement.business.crud;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import lotmovement.business.entity.UserProfile;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAPersistence;

public class EntityStart {
     EntityManagerFactory factory;
     EntityManager em;



    public void StartDbaseConnection()
    {

        factory = Persistence.createEntityManagerFactory("LotMovementPU");
        em = factory.createEntityManager();

    }

    public void StartPopulateTransaction(Object entity){

        EntityTransaction userTransaction = em.getTransaction();

        userTransaction.begin();

        em.merge(entity);
        userTransaction.commit();

        em.close();

    }

    public void CloseDbaseConnection(){
        factory.close();
    }

}

按照建议使用 Trace,这是 SQL 的日志

SELECT t0.LAST_UPDATED_TIME, t0.FIRST_NAME, t0.LAST_NAME, t0.PASSWORD, t0.SECURITY_LEVEL FROM USERPROFILE t0 WHERE t0.USER_ID = ? [params=(String) tok]

这是记录:

USER_ID FIRST_NAME  LAST_NAME   PASSWORD    SECURITY_LEVEL  LAST_UPDATED_TIME
tok           1      1          1           1                  2012-12-13 08:46:48.802                        

添加了 Persistence.XML

<persistence version="2.0" 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_2_0.xsd">
  <persistence-unit name="LotMovementPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <non-jta-data-source/>
    <class>lotmovement.business.entity.UserProfile</class>
    <properties>
      <property name="openjpa.ConnectionURL" value="jdbc:derby://localhost:1527/LotMovementDBase"/>
      <property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="openjpa.ConnectionUserName" value="toksis"/>
      <property name="openjpa.ConnectionPassword" value="bitoytoksis"/>
      <property name="openjpa.Log" value="SQL=TRACE"/>
      <property name="openjpa.ConnectionFactoryProperties" value="PrintParameters=true" />
    </properties>
  </persistence-unit>
</persistence>

我发现了问题的根本原因。这是关于我如何在 Spring Plugin 中实例化类。

当我将 find() 语句更改为下面时,它现在可以工作了。

  UserProfile up = entityStart.em.find(UserProfile.class, "tok"); 

但是我怎样才能使用 Spring 初始化这个呢?下面的代码不起作用?

private UserProfile userProfile;

...... some codes here.

entityStart.em.find(UserProfile.class, userId);


  ..... getter setter
4

1 回答 1

0

问题的根本原因。

entityStart.em.find(UserProfile.class, userId); --> 应该是

userProfile = entityStart.em.find(UserProfile.class, userId);

于 2012-12-19T23:33:08.227 回答