Special select query is not working in my spring boot application - java

Repository Class:
#Repository // This my architecture repository class.
public class UserRepositoryImp implements UserRepository {
private EntityManager entityManager;
#Autowired
public UserRepositoryImp(EntityManager entityManager) {
this.entityManager = entityManager;
}
private static final String MY_SQL_COUNT_EMAIL =
"SELECT count(e) FROM User e WHERE e.email=:email "; // This is hibernate query.
#Override
public Integer getCountByEmail(String email) {
Session session = entityManager.unwrap(Session.class);
TypedQuery<Integer> query = session.createQuery(MY_SQL_COUNT_EMAIL, null);
query.setParameter("email", email); // This is return count in database check database
// .. if you email this database passing already exist this email but your count zero created new email.
return query.executeUpdate();
}
}
Service Class:
#Service
#Transactional
public class UserServiceImp implements UserService {
#Autowired
UserRepository userRepository;
#Override
public User validateUser(String email, String password) throws EtAuthException {
return null;
}
#Override
public User registerUser(User user) throws EtAuthException {
Pattern pattern = Pattern.compile("^[A-Z0-9._%+-]+#[A-Z0-9.-]+\\.[A-Z]{2, 6} $ "); // This regex.
if (pattern.matcher(user.getEmail()).matches()) {
throw new EtAuthException("Invalid email format"); // This is check email regex.
}
Integer count = userRepository.getCountByEmail(user.getEmail()); // This is my method
// .. count email in database and repository methods
if (count > 0) {
throw new EtAuthException("Email already in use");
}
Integer userId = userRepository.create(user);
return userRepository.findById(userId);
}
}
Postman Error:
Cannot invoke "java.lang.Class.isAssignableFrom(java.lang.Class)"
because "resultClass" is null"
Problem:
My problem is actually. I want to write a query to hibernated but for some reason it doesn't work. For such operations in spring boot, ie typing a query, for example polling email, put delete add and updated, how do I do it? I need to query me. I can't solve this problem.

You are calling executeUpdate() for a select query. Try using getSingleResult() instead.
#Override
public long getCountByEmail(String email) {
Session session = entityManager.unwrap(Session.class);
String query = "SELECT count(e) FROM User e WHERE e.email = :email";
TypedQuery<Long> typedQuery = session.createQuery(query, Long.class);
typedQuery.setParameter("email", email);
return typedQuery.getSingleResult();
}
OR
#Override
public long getCountByEmail(String email) {
String query = "SELECT count(e) FROM User e WHERE e.email = :email";
TypedQuery<Long> typedQuery = entityManager.createQuery(query, Long.class);
typedQuery.setParameter("email", email);
return typedQuery.getSingleResult();
}

Related

How to show information about an entity?

I have a problem. After clicking on the "create order" button, the user is redirected to the URL: "localhost:8080/currentorder/{id}" After visiting this URL, the user should see order.text.
Attempts to solve: In the DAO, I create a method that, by the ID passed from the controller, looks for an order in HQL:
public List show(Long id) {
Transaction tx = null;
try (Session session = BogPomogi.getSessionFactory().openSession()) {
session.beginTransaction();
Query query = session.createQuery("from Order where id = :id");
query.setParameter("id", id);
List result = query.getResultList();
session.getTransaction().commit();
return result;
}
}
But as you understand, after that, the timelif could not display anything (I mean order.getStatus()) Now I still think that I need to search the database and return an object, but how? help me please
My code:
Controller
#PostMapping("/")
public String createOrder (#ModelAttribute("order") Orderdao orderdao, String text, Model model, RedirectAttributes redirectAttributes){
orderdao.createOrder(text);
redirectAttributes.addAttribute("id", orderdao.checkLastOrder());
return "redirect:/currentorders/{id}";
}
#GetMapping("/currentorders/{id}")
public String showOrder (#PathVariable("id") Long id, Orderdao orderdao, Model model, Order order){
model.addAttribute("currentOrder", orderdao.show(id));
return "order";
}
Entity
#Entity
#Table(name = "orders")
public class Order implements Serializable{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String text;
private String customer;
private int status;
public Order(String text, String customer, int status) {
this.text = text;
this.customer = customer;
this.status = status;
}
public Order(String customer) {
this.customer = customer;
}
public Order(){
}
//Getters and setters
Method:
public Order show(Long id) {
Transaction tx = null;
try (Session session = BogPomogi.getSessionFactory().openSession()) {
session.beginTransaction();
Query query = session.createQuery("from Order where id = :id");
query.setParameter("id", id);
List result = (List) query.getSingleResult();
session.getTransaction().commit();
return (Order) session.save(result);
}
}
I am missing your code for orderDao.create, but usually you would have a service class (annotated with Springs #Service annotation), which is injected to the controller and which is called to create the entity. You can make this service method return the ID of the just created entity. It could hence be something like public Long createOrder(OrderDao orderDao). Inside there, after calling repository.save(entity), the entity will already have the ID set (try to verify yourself with debugger: Set a breakpoint to the line before you save the entity and check the ID is null, then go one step forward and see that after save, the ID is set).
My answer:
public Order show(Long id) {
Transaction tx = null;
try (Session session = BogPomogi.getSessionFactory().openSession()) {
session.beginTransaction();
Query query = session.createQuery("select text from Order where id = :id");
query.setParameter("id", id);
String result = (String) query.getSingleResult();
session.getTransaction().commit();
session.close();
return new Order(result, "adsfreger", 1);
}
}

spring boot CrudRepository query passing object mysql

I have the following code:
public interface UserRepository extends CrudRepository<User, Integer> {
#Modifying
#Transactional
#Query(value = "INSERT INTO users(email, password, name) VALUES (?,?,?)", nativeQuery = true)
void insertUserToUsers(String email, String password, String name);
}
I don't want to pass the values, i want to pass a user object like this:
void insertUserToUsers(User user);
Try this
public interface UserRepository extends CrudRepository<User, Integer> {
#Modifying
#Transactional
#Query(value = "INSERT INTO users(email, password, name) VALUES (:#{#user.email},:#{#user.firstname},:#{#user.name})", nativeQuery = true)
void insertUserToUsers(#Param("user") User user);
}
Try this code,
#Modifying
#Transactional
#Query(value = "INSERT INTO users(email, password, name) VALUES (?,?,?)", nativeQuery = true)
void insertUserToUsers(String email, String password, String name);
default void insertUserToUsers(User user) {
return insertUserToUsers(user.getEmail(), user.getPassword(), user.getName());
}
Where you use your UserRepository you can use the default method to save user:
ex:
#Service
public class MyService{
#Autowired
private UserRepository userRepository ;
public void example(User user){
userRepository.save(user)
}
}
Because CrudRepository give us some default methods , you can check here

Hibernate Session Closed Exception,When 10 or more user access the page

I have a spring MVC application with hibernate.I keep on getting the session closed error, when 10 or more users accessed the same page for reading the data or after fast subsequent requests.
Please help, I needed a crucial fix. It is affecting the customer.
I use the below code
try{
session = sessionFactory.openSession();
tx = session.getTransaction();
session.beginTransaction();
Map<Organization, List<Users>> comToUserLst
= new HashMap<Organization,List<Users>>();
String queryString = "FROM Users as usr Inner Join usr.organization
as org where org.id = :id";
Query query = session.createQuery(queryString);
query.setInteger("id", Integer.valueOf(id));
List<?> comLst = query.list();
Iterator<?> ite = comLst.iterator();
while (ite.hasNext()) {
Object[] objects = (Object[]) ite.next();
Users user = (Users) objects[0];
Organization Organization = (Organization) objects[1];
if (comToUserLst.containsKey(Organization)) {
List<Users> usrLst = new ArrayList<Users>();
usrLst.addAll(comToUserLst.get(Organization));
usrLst.add(user);
comToUserLst.put(Organization, usrLst);
} else {
List<Users> userLst = new ArrayList<Users>();
userLst.add(user);
comToUserLst.put(Organization, userLst);
}
}
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
tx.commit();
session.close();
}
return comToUserLst;
For update
#Transactional
public Account updateAccount(Account account, UserDetail userInfo) {
session = sessionFactory.getCurrentSession();
Account acct = null;
String queryString = "FROM Account where id = :acctId";
Query query = session.createQuery(queryString);
query.setLong("acctId", account.getId());
acct = (Account) query.uniqueResult();
acct.setName(account.getName());
acct.setPhone(account.getPhone());
acct.setRating(account.getRating());
acct.setFax(account.getFax());
acct.setAccountNumber(account.getAccountNumber());
if (!ValidateUtil.isNullOrEmptyCheckStr(account.getParentAccount()
.getName())) {
acct.setParentAccount(account.getParentAccount());
}
acct.setWebsite(account.getWebsite());
acct.setType(account.getType());
acct.setIndustry(account.getIndustry());
acct.setNumberOfEmployees(account.getNumberOfEmployees());
acct.setDescription(account.getDescription());
acct.setAnnualRevenue(account.getAnnualRevenue());
acct.setEmail(account.getEmail());
acct.setBillingAddress(account.getBillingAddress());
acct.setShippingAddress(account.getShippingAddress());
Users user = new Users();
user.setId(userInfo.getUserId());
// modified details
acct.setModifiedBy(user);
acct.setModifiedDate(new Date());
//update use merge
session.merge(acct);
session.flush();
System.out.println("update Account" + acct.getId());
return acct;
}
Exception
org.hibernate.SessionException: Session is closed!
at org.hibernate.internal.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:133)
at org.hibernate.internal.SessionImpl.getTransactionCoordinator(SessionImpl.java:2069)
at org.hibernate.loader.Loader.doQuery(Loader.java:923)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doList(Loader.java:2553)
at org.hibernate.loader.Loader.doList(Loader.java:2539)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
at org.hibernate.loader.Loader.list(Loader.java:2364)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
at com.oi.service.setup.OrganizationService.getOrgToUserLst(OrganizationService.java:311)
at com.oi.service.setup.OrganizationService$$FastClassBySpringCGLIB$$84e99831.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
In Spring it might need to look like this:
#Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
#Transactional
public Map getOrgToUserLst() {
Map<Organization, List<Users>> comToUserLst = new HashMap<Organization,List<Users>>();
String queryString = "FROM Users as usr Inner Join usr.organization as org where org.id = :id";
List<?> comLst = this.sessionFactory.getCurrentSession()
.createQuery(queryString)
.setParameter(0, Integer.valueOf(id))
.list();
Iterator<?> ite = comLst.iterator();
while (ite.hasNext()) {
Object[] objects = (Object[]) ite.next();
Users user = (Users) objects[0];
Organization Organization = (Organization) objects[1];
if (comToUserLst.containsKey(Organization)) {
List<Users> usrLst = new ArrayList<Users>();
usrLst.addAll(comToUserLst.get(Organization));
usrLst.add(user);
comToUserLst.put(Organization, usrLst);
} else {
List<Users> userLst = new ArrayList<Users>();
userLst.add(user);
comToUserLst.put(Organization, userLst);
}
}
return comToUserLst;
}
#Transactional
public void saveOrganization(Organization org) {
this.sessionFactory.getCurrentSession().persist(org);
}
#Transactional
public void updateOrganization(Organization org) {
this.sessionFactory.getCurrentSession().merge(org);
}
#Transactional
public void deleteOrganization(Organization org) {
getCurrentSession().delete(org);
}
#Transactional
public void deleteOrganizationById(long id) {
final Organization org = this.getCurrentSession().get(Organization, id);
this.getCurrentSession().delete(org);
}
FOR UPDATE
// Get an account object by ID
public Account getAccount(long id) {
session = sessionFactory.getCurrentSession();
String queryString = "FROM Account where id = :acctId";
Query query = session.createQuery(queryString);
query.setLong("acctId", id);
return (Account) query.uniqueResult();
}
// Set account object's attributes
public Account updateAccount(Account acct, UserDetail userInfo) {
acct.setName(account.getName());
acct.setPhone(account.getPhone());
acct.setRating(account.getRating());
acct.setFax(account.getFax());
acct.setAccountNumber(account.getAccountNumber());
if (!ValidateUtil.isNullOrEmptyCheckStr(account.getParentAccount()
.getName())) {
acct.setParentAccount(account.getParentAccount());
}
acct.setWebsite(account.getWebsite());
acct.setType(account.getType());
acct.setIndustry(account.getIndustry());
acct.setNumberOfEmployees(account.getNumberOfEmployees());
acct.setDescription(account.getDescription());
acct.setAnnualRevenue(account.getAnnualRevenue());
acct.setEmail(account.getEmail());
acct.setBillingAddress(account.getBillingAddress());
acct.setShippingAddress(account.getShippingAddress());
Users user = new Users();
user.setId(userInfo.getUserId());
// modified details
account.setModifiedBy(user);
account.setModifiedDate(new Date());
updateAccount(acct);
}
// Update the account object in the database. Here transaction is necessary
#Transactional
private Account updateAccount(Account acct) {
session = sessionFactory.getCurrentSession();
//update use merge
System.out.println("update Account" + acct.getId());
return session.merge(acct);
}
// This is for testing
public void testUpdate(long id, UserDetail userInfo) {
Account acc = getAccount(id);
updateAccount(acct, userInfo);
}
Reference
How are you fetching your sessions SessionFactory.openSession() or SessionFactory.getCurentSession()? Keep in mind that hibernate sessions are not thread safe, hence you require one session per request response cycle.
You should use sessionFactory.getCurrentSession() in place of openSession(). This will maintain the opening and closing sessions automatically.
Also you should use Spring's Transaction using #Transactional with you method. Spring's Transaction management is more efficient than the Hibernate's getCurrentTransaction()

Hibernate : How to get predefined object passing a String value through parameters

I used following method to get predefined object .
public Patient getUserNameAndPassword(String username, Session session) {
Patient patient=(Patient)session.get(Patient.class,username);
return patient;
}
After execute this method , following exception was generated .
org.hibernate.TypeMismatchException: Provided id of the wrong type for class beans.Patient. Expected: class java.lang.Integer, got class java.lang.String
PatientService.java
public class PatientService {
private static PatientDAOInterface patientDAOInterface;
public PatientService() {
patientDAOInterface = new PatientDAOImpl();
}
public Session getSession() {
Session session = patientDAOInterface.openCurrentSession();
return session;
}
public Transaction getTransaction(Session session) {
return patientDAOInterface.openTransaction(session);
}
public Patient getUserNameAndPassword(String username){
Session session = patientDAOInterface.openCurrentSession();
Transaction transaction = null;
Patient patient=new Patient();
try{
transaction = patientDAOInterface.openTransaction(session);
patient=patientDAOInterface.getUserNameAndPassword(username, session);
transaction.commit();
}catch(Exception ex){
ex.printStackTrace();
}finally{
session.close();
}
return patient;
}
}
PatientDAOInterface .java
public interface PatientDAOInterface
{
public Patient getUserNameAndPassword(String username,Session session);
public Session openCurrentSession();
public Transaction openTransaction(Session session);
}
PatientDAOImpl.java
public class PatientDAOImpl implements PatientDAOInterface {
#Override
public Patient getUserNameAndPassword(String username, Session session) {
Patient patient=(Patient)session.get(Patient.class,username);
return patient;
}
private static final SessionFactoryBuilder sessionFactoryBuilder = SessionFactoryBuilder.getInstance();
#Override
public Session openCurrentSession() {
Session currentSession = sessionFactoryBuilder.getSessionFactory().openSession();
return currentSession;
}
#Override
public Transaction openTransaction(Session session) {
Transaction beginTransaction = session.beginTransaction();
return beginTransaction;
}
}
I have mentioned my works above.
Actually , I want to pass a String through parameters and get a Patient object.
I am familiar with passing an Integer instead of a String. But I have no idea about this .
Have any ideas ?
You want to find a Patient by his username? Because here you are trying to find him by his ID and you are passing a String representing username. You should write your own query/criteria for getting a Patient by username. Something like this:
Criteria criteria = session.createCriteria(Patient.class);
criteria.add(Restrictions.eq("username", username);
List<Patient> patients = criteria.list();
Or query version:
String hql = "FROM Patient p WHERE p.username = "+username;
Query query = session.createQuery(hql);
List patients = query.list();
I found a answer to the problem.
public Patient getUserNameAndPassword(String username, Session session) {
Query query=session.createQuery("from Patient where user_name= :username");
query.setParameter("username", username);
List list = query.list();
Patient patient=(Patient) list.get(0);
return patient;
}
This is worked for me .
Thanks.

How to pass userid from one table to another table in spring mvc using hibernate

I'm using Spring security for the login. I have the User.java which contains user-details.
#Entity(name = "user_table")
//#Table(name = "user_table")
public class User {
#Id
#Column(name = "id")
private String userId;
#Column(name = "email" ,unique = true)
private String userEmail;
#Column(name = "password")
private String userPassword;
//getter and setters
}
I'm getting the whole data of the current user from the table by using spring security. This is the code:
public User findUserByEmail(String email) {
List<User> users = new ArrayList<User>();
try{
users = sessionFactory.getCurrentSession().createQuery("from user_table where email= ?").setParameter(0, email).list();
System.out.println("user is " +users);
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
if (users.size() > 0) {
return users.get(0);
} else {
return null;
}
}
#Override
public User getCurrentUser() {
Authentication auth = SecurityContextHolder.getContext()
.getAuthentication();
User currentUser = new User();
if (!(auth instanceof AnonymousAuthenticationToken)) {
UserDetails userDetails = (UserDetails) auth.getPrincipal();
System.out.println("User has authorities: "
+ userDetails.getAuthorities());
System.out.println("USERNAME:: "+userDetails.getUsername());
currentUser = findUserByEmail(userDetails
.getUsername());
System.out.println("currentUser "+currentUser);
System.out.println("currentUser "+currentUser.getUserId());
return currentUser;
}
return null;
}
What I want is to send the user id which I'm getting from currentUser.getUserId() to some other method. In that method I'm mapping to some other table like user_detail table where id is primary key. By sending id, I will get the other user_details which are not present in the user_table.
This is my UserDetail:
#Entity(name = "user_detail")
#Table(name = "user_detail")
public class UserDetail {
#Id
#GeneratedValue
#Column(name = "id")
private String userId;
//some other details like Address .
//getter and setter.
}
From controller I'm calling the above method like this:
UserService userService = new UserService();
User user=userDao.getCurrentUser();
String userId = user.getUserId();
System.out.println(userId);
UserDetail u=userDao.findUserById(userId);
and this is the method where I pass the current user id :
public UserDetail findUserById(String id) {
// TODO Auto-generated method stub
List<String> users = new ArrayList<String>();
try{
users = sessionFactory.getCurrentSession().createQuery("from user_detail where id= ?").setParameter(0, id).list();
System.out.println("user is " +users);
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
if (users.size() > 0) {
return null;
} else {
return null;
}
}
Now the result I'm getting here is null . Like user is null. What I'm doing wrong here?
There are several problems in your code. Just to point out some of them:
UserService userService = new UserService(); - you're manually creating the service object and not letting Spring-MVC injecting it into your controller, i.e. :
#Autowired
private UserService userService ;
UserDAO should be injected in your service, and not called from your controller :
class UserServiceImpl implements UserService{
#Autowired
private UserDAO userDAO;
}
All operations from your controller should call services methods and not DAO's methods. The service should use the DAO for database access. i.e.
UserDetail u=userDao.findUserById(userId);
should become
UserDetail u = userService.findUserById(userId);
and in your service :
class UserServiceImpl implements UserService{
#Autowire
private UserDAO userDAO;
#Override
public UserDetail findUserById(Long userId){
return userDAO.findUserById(userId);
}
}
if (users.size() > 0) {
return null;
} else {
return null;
}
is always returning null. Should be :
if (`users.isEmpty()){
return users.get(0);
}else { return null;}
users = sessionFactory.getCurrentSession().createQuery("from user_detail where id= ?").setParameter(0, id).list();
Your query is wrong. You should use your current bean class name and not the table name in your query, i.e. createQuery("FROM UserDetail WHERE id = ?")

Categories