Как внедрить этот вид функциональности класса. Spring Open JPA

Как я могу внедрить класс 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();


    }
}

Мой весенний впрыск, который не работает. Я ввел сущность 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;
    }



}

1 ответ

Решение

Наконец я нашел основную причину и решение проблемы.

Первопричина:

UserProfile userProfile; -> это внедрит сущность UserProfile.

entityStart.em.find (UserProfile.class, userId); -> Это не будет работать, потому что он не был передан переменной класса userProfile..

Решение:

UserProfile userProfile; -> внедрить сущность UserProfile.

userProfile = entityStart.em.find (UserProfile.class, userId); ---> find () теперь работает find.

Другие вопросы по тегам