کلاسی به شکل زیر دارم که نیاز دارم به جای مقادیری که در hashmapusers پوت شده اند مقادیر username و password از database گرفته شود.
import com.mytravel.arsam.dto.UserDetails;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class LoginService {
HashMap<String, String> hashmapusers = new HashMap<String, String>();
public LoginService() {
hashmapusers.put("me id", "im arsam");
hashmapusers.put("you id", "you saied");
// declareing getAllUsersFromDB method for add alluser objects to hashmapUsers
}
private void getAllUsersFromDB() { //connect to db and return users objects
//write this method with jpa and hibernate annotations
HashMap<String, String> listOfAllUsers = new HashMap<>();
SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory();
Session session = sessionfactory.openSession();
session.beginTransaction();
Query query = session.createQuery("select new map (userName, password) from SignIn");
listOfAllUsers.put(query.getQueryString ); // moshkel injast
query.setFirstResult(5);
query.setMaxResults(4);
session.getTransaction().commit();
session.close();
}
public boolean authenticate(String userId, String password) {
if (password != null || password.trim() == "") {
return false;
}
return true;
}
public UserDetails getUserDetails(String userId) {
UserDetails user = new UserDetails();
user.setUserName(hashmapusers.get(userId));
// user.setUserId(userId);
return user;
}
}