Note that there are some explanatory texts on larger screens.

plurals
  1. POHow To inject this kind of class functionality. Spring Open JPA
    text
    copied!<p>How can I inject the class UserProfile in this kind of statement? This is using Struts2 - Spring plugin.</p> <p>( UserProfile userProfile = em.find(UserProfile.class,"1");) ---> I could don't have any idea how to change this for injection style in spring?</p> <pre><code>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(); } } </code></pre> <p>My Spring injection that does not work. I had injected UserProfile entity using setters but i think there is a problem because it could not retrieve any data.</p> <pre><code>private UserProfile userProfile; --&gt; it does inject. entityStart.em.find(UserProfile.class, userId)--&gt; will not work in the sense that no values are retrieved. </code></pre> <p>Complete Class</p> <pre><code>/* * 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); ---&gt; 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); --&gt; 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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload