【问题标题】:How To inject this kind of class functionality. Spring Open JPA如何注入这种类功能。春季开放 JPA
【发布时间】:2012-12-13 08:47:13
【问题描述】:

如何在这种语句中注入类 UserProfile?这是使用 Struts2 - Spring 插件。

(UserProfile userProfile = em.find(UserProfile.class,"1");) ---> 我不知道如何在春季更改此注入样式?

package tester;

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


import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import lotmovement.business.entity.UserProfile;



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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        EntityManagerFactory entityManagerFactory = 
        Persistence.createEntityManagerFactory("LotMovementPU");

        EntityManager em = entityManagerFactory.createEntityManager();

        EntityTransaction userTransaction = em.getTransaction();
        userTransaction.begin();
        UserProfile userProfile = em.find(UserProfile.class,"1");

        if(userProfile.getUserId().equals("tok"))
        {
            System.out.println("find");
            System.out.println("write successful");
        }


        entityManagerFactory.close();


    }
}

我的 Spring 注入不起作用。 我使用 setter 注入了 UserProfile 实体,但我认为存在问题,因为它无法检索任何数据。

private UserProfile userProfile; --> it does inject.

entityStart.em.find(UserProfile.class, userId)--> will not work in the sense that no values are retrieved.

完整课程

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

import 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); ---> this one will not work.

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

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

        UserProfile up = entityStart.em.find(UserProfile.class, userId); --> this one will work but it is not injected...


        if (password.equals(up.getPassword())) {
            return true;
        } else {
            return false;
        }

    }



    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;
    }



}

【问题讨论】:

  • 你到底想注入什么以及在哪里注入?
  • 而不是这样做: UserProfile up = entityStart.em.find(UserProfile.class, userId); srping应该做什么? @aleksandr M
  • 你想要它是什么?你到底想注射什么!?
  • 为了解耦..这就是我在这个程序上使用弹簧的原因....或者我没有得到它?
  • 我试图通过这个注入实体用户配置文件。 UserProfile userprofile 但是此代码不起作用。 entityStart.em.find(UserProfile.class, userId);原代码为:UserProfile userProfile = em.find(UserProfile.class,userId); @Aleksandr M 你现在明白了吗?

标签: spring jakarta-ee struts2 dependency-injection openjpa-maven-plugin


【解决方案1】:

终于找到了问题的根源和解决办法。

根本原因:

用户配置文件用户配置文件; --> 它将注入 UserProfile 实体。

entityStart.em.find(UserProfile.class, userId); --> 这不起作用,因为它没有传递给类变量 userProfile..

解决方案:

用户配置文件用户配置文件; --> 注入 UserProfile 实体。

userProfile = entityStart.em.find(UserProfile.class, userId); ---> find() 现在可以使用 find。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多