Search code examples
javahibernatejpaentitymanager

All JPA queries fail to find any result(even tho data is present in the MySQL DB)


I'm trying to set up javax.Persistence in my project, and I've started to use the basic approach:

EntityManagerGenerator

 package olsa.amex.dao;

 import javax.persistence.*; 

 public class EntityManagerGenerator {

private EntityManager currentSession;

private EntityTransaction currentTransaction;

public EntityManager openCurrentSession() {
    if ((currentSession == null)||(currentSession != null && !currentSession.isOpen()))
        currentSession = getSessionFactory().createEntityManager();
    return currentSession;
}

public EntityManager openCurrentSessionwithTransaction() {
    if ((currentSession == null)||(currentSession != null && !currentSession.isOpen()))
        currentSession = getSessionFactory().createEntityManager();
    currentTransaction = currentSession.getTransaction();
    currentTransaction.begin();
    return currentSession;
}

public void closeCurrentSession() {
    if (currentSession != null && currentSession.isOpen())
        currentSession.close();
}

public void closeCurrentSessionwithTransaction() {
    if (currentSession != null && currentSession.isOpen()) {
        currentTransaction.commit();
        currentSession.close();
    }
}

private static EntityManagerFactory getSessionFactory() {
    EntityManagerFactory entityManager = Persistence.createEntityManagerFactory("JPAAmex");
    return entityManager;
}

public EntityManager getCurrentSession() {
    return currentSession;
}

public void setCurrentSession(EntityManager currentSession) {
    this.currentSession = currentSession;
}

public EntityTransaction getCurrentTransaction() {
    return currentTransaction;
}

public void setCurrentTransaction(EntityTransaction currentTransaction) {
    this.currentTransaction = currentTransaction;
}

}

ViewAgentSsbDAO(for now I have only implemented findAll and findById)

  package olsa.amex.dao;
  import java.util.List;

  import javax.persistence.TypedQuery;

  import com.olsa.amex.entities.ViewAgentssb;

  public class ViewAgentSsbDAO implements IGenericDAO<ViewAgentssb,Integer> {

private EntityManagerGenerator hibernateSessionGenerator;

public ViewAgentSsbDAO() {
    this.hibernateSessionGenerator = new EntityManagerGenerator();
}

public EntityManagerGenerator getHibernateSessionGenerator() {
    return hibernateSessionGenerator;
}

@Override
public void persist(ViewAgentssb entity) throws Exception{
    throw new UnsupportedOperationException();
}

@Override
public void update(ViewAgentssb entity) throws Exception {
    throw new UnsupportedOperationException();
}

@Override
public ViewAgentssb findById(Integer id) throws Exception{
    TypedQuery<ViewAgentssb> query = hibernateSessionGenerator.getCurrentSession().createQuery("from com.olsa.amex.entities.ViewAgentssb as v where v.IDAgent = :id", ViewAgentssb.class);
    query.setParameter("id", id);
    ViewAgentssb view = query.getSingleResult();
    return view; 
}

@Override
public void delete(ViewAgentssb entity) throws Exception {
    throw new UnsupportedOperationException();
}

@Override
public List<ViewAgentssb> findAll() throws Exception{
    List<ViewAgentssb> views = hibernateSessionGenerator.getCurrentSession().createQuery("from com.olsa.amex.entities.ViewAgentssb", ViewAgentssb.class).getResultList();
    return views;
}

@Override
public void deleteAll() throws Exception{
    throw new UnsupportedOperationException();
}

}

The service class:

  package olsa.amex.services;

  import java.util.List;

  import org.apache.log4j.LogManager;
  import org.apache.log4j.Logger;

  import com.olsa.amex.entities.ViewAgentssb;

  import olsa.amex.dao.ViewAgentSsbDAO;

  public class ViewAgentSsbService implements IGenericService<ViewAgentssb,Integer> {

private ViewAgentSsbDAO viewAgentSsbDAO;

private Logger logger = LogManager.getLogger(ViewAgentSsbService.class);

public ViewAgentSsbService() {
    this.viewAgentSsbDAO = new ViewAgentSsbDAO();
}
@Override
public void persist(ViewAgentssb entity) throws Exception{
    throw new UnsupportedOperationException();
}

@Override
public void update(ViewAgentssb entity) throws Exception{
    throw new UnsupportedOperationException();
}

@Override
public ViewAgentssb findById(Integer id) throws Exception {
    viewAgentSsbDAO.getHibernateSessionGenerator().openCurrentSession();
    ViewAgentssb object = viewAgentSsbDAO.findById(id);
    viewAgentSsbDAO.getHibernateSessionGenerator().closeCurrentSession();
    return object;
}

@Override
public void delete(ViewAgentssb entity) throws Exception{
    throw new UnsupportedOperationException();
}

@Override
public List<ViewAgentssb> findAll() throws Exception{
    logger.debug("Sto iniziando la query findAll() per l'entity + " + ViewAgentssb.class);
    viewAgentSsbDAO.getHibernateSessionGenerator().openCurrentSession();
    List<ViewAgentssb> views = viewAgentSsbDAO.findAll();
    logger.debug("La query ha ritornato i seguenti risultati: + " + views.toString());
    viewAgentSsbDAO.getHibernateSessionGenerator().closeCurrentSession();
    return views;
}

@Override
public void deleteAll() throws Exception{
    throw new UnsupportedOperationException();

}

}

the Entity:

 package com.olsa.amex.entities;

 import java.io.Serializable;
 import javax.persistence.*;


 /**
  * The persistent class for the view_agentssbs database table.
  * 
  */
 @Entity
 @Table(name="view_agentssbs")
 @NamedQuery(name="ViewAgentssb.findAll", query="SELECT v FROM ViewAgentssb v")
 public class ViewAgentssb implements Serializable {
private static final long serialVersionUID = 1L;

private int active;

private String agencyName;

@Lob
private String agentCode;

@Lob
private String agentName;

@Lob
private String agentPassword;

@Lob
private String agentSurname;

@Lob
private String agentUsername;

private int bExist;

@Lob
private String buyed_visure;

@Lob
private String cervedCreation;

@Lob
private String cervedDisable;

@Lob
private String cervedPassword;

@Lob
private String cervedUsername;

@Lob
private String channel_Code;

private int checkBackOffice;

@Lob
private String codiceOAM;

@Lob
private String company_cancelled_counter;

@Lob
private String company_failed_counter;

@Lob
private String company_inactive_counter;

private String countryResidence;
@Id
private String deviceID;

private String documentExpiryDate;

private String documentIssueBy;

private String documentIssuedOn;

private String documentNumber;

private String documentType;

private String email;

private int enableCrif;

private int enableVisura;

private String expr1;

@Lob
private String failed_search;

@Lob
private String groupAvatar;

@Lob
private String groupName;

private int IDAgency;

private int IDAgent;

private int IDAreaRegion;

private int IDGroup;

private int IDSubRegion;

private byte isSimpleSignature;

@Lob
private String partnerID;

@Lob
private String photo;

private String pin;

@Lob
private String protestPrejudical;

private String regione;

private String role;

private String socialSecurityNumber;

private String subRegion;

private String telephoneCell;

@Lob
private String userActive;

@Lob
private String userBOAsAgent;

@Lob
private String userCreation;

@Lob
private String userDisable;

private int userHasSignature;

private String userID;

@Lob
private String userPasswordExpired;

private int userResetPassword;

@Lob
private String userToken;

@Lob
private String venueCode;

public ViewAgentssb() {
}

public int getActive() {
    return this.active;
}

public void setActive(int active) {
    this.active = active;
}

public String getAgencyName() {
    return this.agencyName;
}

public void setAgencyName(String agencyName) {
    this.agencyName = agencyName;
}

public String getAgentCode() {
    return this.agentCode;
}

public void setAgentCode(String agentCode) {
    this.agentCode = agentCode;
}

public String getAgentName() {
    return this.agentName;
}

public void setAgentName(String agentName) {
    this.agentName = agentName;
}

public String getAgentPassword() {
    return this.agentPassword;
}

public void setAgentPassword(String agentPassword) {
    this.agentPassword = agentPassword;
}

public String getAgentSurname() {
    return this.agentSurname;
}

public void setAgentSurname(String agentSurname) {
    this.agentSurname = agentSurname;
}

public String getAgentUsername() {
    return this.agentUsername;
}

public void setAgentUsername(String agentUsername) {
    this.agentUsername = agentUsername;
}

public int getBExist() {
    return this.bExist;
}

public void setBExist(int bExist) {
    this.bExist = bExist;
}

public String getBuyed_visure() {
    return this.buyed_visure;
}

public void setBuyed_visure(String buyed_visure) {
    this.buyed_visure = buyed_visure;
}

public String getCervedCreation() {
    return this.cervedCreation;
}

public void setCervedCreation(String cervedCreation) {
    this.cervedCreation = cervedCreation;
}

public String getCervedDisable() {
    return this.cervedDisable;
}

public void setCervedDisable(String cervedDisable) {
    this.cervedDisable = cervedDisable;
}

public String getCervedPassword() {
    return this.cervedPassword;
}

public void setCervedPassword(String cervedPassword) {
    this.cervedPassword = cervedPassword;
}

public String getCervedUsername() {
    return this.cervedUsername;
}

public void setCervedUsername(String cervedUsername) {
    this.cervedUsername = cervedUsername;
}

public String getChannel_Code() {
    return this.channel_Code;
}

public void setChannel_Code(String channel_Code) {
    this.channel_Code = channel_Code;
}

public int getCheckBackOffice() {
    return this.checkBackOffice;
}

public void setCheckBackOffice(int checkBackOffice) {
    this.checkBackOffice = checkBackOffice;
}

public String getCodiceOAM() {
    return this.codiceOAM;
}

public void setCodiceOAM(String codiceOAM) {
    this.codiceOAM = codiceOAM;
}

public String getCompany_cancelled_counter() {
    return this.company_cancelled_counter;
}

public void setCompany_cancelled_counter(String company_cancelled_counter) {
    this.company_cancelled_counter = company_cancelled_counter;
}

public String getCompany_failed_counter() {
    return this.company_failed_counter;
}

public void setCompany_failed_counter(String company_failed_counter) {
    this.company_failed_counter = company_failed_counter;
}

public String getCompany_inactive_counter() {
    return this.company_inactive_counter;
}

public void setCompany_inactive_counter(String company_inactive_counter) {
    this.company_inactive_counter = company_inactive_counter;
}

public String getCountryResidence() {
    return this.countryResidence;
}

public void setCountryResidence(String countryResidence) {
    this.countryResidence = countryResidence;
}

public String getDeviceID() {
    return this.deviceID;
}

public void setDeviceID(String deviceID) {
    this.deviceID = deviceID;
}

public String getDocumentExpiryDate() {
    return this.documentExpiryDate;
}

public void setDocumentExpiryDate(String documentExpiryDate) {
    this.documentExpiryDate = documentExpiryDate;
}

public String getDocumentIssueBy() {
    return this.documentIssueBy;
}

public void setDocumentIssueBy(String documentIssueBy) {
    this.documentIssueBy = documentIssueBy;
}

public String getDocumentIssuedOn() {
    return this.documentIssuedOn;
}

public void setDocumentIssuedOn(String documentIssuedOn) {
    this.documentIssuedOn = documentIssuedOn;
}

public String getDocumentNumber() {
    return this.documentNumber;
}

public void setDocumentNumber(String documentNumber) {
    this.documentNumber = documentNumber;
}

public String getDocumentType() {
    return this.documentType;
}

public void setDocumentType(String documentType) {
    this.documentType = documentType;
}

public String getEmail() {
    return this.email;
}

public void setEmail(String email) {
    this.email = email;
}

public int getEnableCrif() {
    return this.enableCrif;
}

public void setEnableCrif(int enableCrif) {
    this.enableCrif = enableCrif;
}

public int getEnableVisura() {
    return this.enableVisura;
}

public void setEnableVisura(int enableVisura) {
    this.enableVisura = enableVisura;
}

public String getExpr1() {
    return this.expr1;
}

public void setExpr1(String expr1) {
    this.expr1 = expr1;
}

public String getFailed_search() {
    return this.failed_search;
}

public void setFailed_search(String failed_search) {
    this.failed_search = failed_search;
}

public String getGroupAvatar() {
    return this.groupAvatar;
}

public void setGroupAvatar(String groupAvatar) {
    this.groupAvatar = groupAvatar;
}

public String getGroupName() {
    return this.groupName;
}

public void setGroupName(String groupName) {
    this.groupName = groupName;
}

public int getIDAgency() {
    return this.IDAgency;
}

public void setIDAgency(int IDAgency) {
    this.IDAgency = IDAgency;
}

public int getIDAgent() {
    return this.IDAgent;
}

public void setIDAgent(int IDAgent) {
    this.IDAgent = IDAgent;
}

public int getIDAreaRegion() {
    return this.IDAreaRegion;
}

public void setIDAreaRegion(int IDAreaRegion) {
    this.IDAreaRegion = IDAreaRegion;
}

public int getIDGroup() {
    return this.IDGroup;
}

public void setIDGroup(int IDGroup) {
    this.IDGroup = IDGroup;
}

public int getIDSubRegion() {
    return this.IDSubRegion;
}

public void setIDSubRegion(int IDSubRegion) {
    this.IDSubRegion = IDSubRegion;
}

public byte getIsSimpleSignature() {
    return this.isSimpleSignature;
}

public void setIsSimpleSignature(byte isSimpleSignature) {
    this.isSimpleSignature = isSimpleSignature;
}

public String getPartnerID() {
    return this.partnerID;
}

public void setPartnerID(String partnerID) {
    this.partnerID = partnerID;
}

public String getPhoto() {
    return this.photo;
}

public void setPhoto(String photo) {
    this.photo = photo;
}

public String getPin() {
    return this.pin;
}

public void setPin(String pin) {
    this.pin = pin;
}

public String getProtestPrejudical() {
    return this.protestPrejudical;
}

public void setProtestPrejudical(String protestPrejudical) {
    this.protestPrejudical = protestPrejudical;
}

public String getRegione() {
    return this.regione;
}

public void setRegione(String regione) {
    this.regione = regione;
}

public String getRole() {
    return this.role;
}

public void setRole(String role) {
    this.role = role;
}

public String getSocialSecurityNumber() {
    return this.socialSecurityNumber;
}

public void setSocialSecurityNumber(String socialSecurityNumber) {
    this.socialSecurityNumber = socialSecurityNumber;
}

public String getSubRegion() {
    return this.subRegion;
}

public void setSubRegion(String subRegion) {
    this.subRegion = subRegion;
}

public String getTelephoneCell() {
    return this.telephoneCell;
}

public void setTelephoneCell(String telephoneCell) {
    this.telephoneCell = telephoneCell;
}

public String getUserActive() {
    return this.userActive;
}

public void setUserActive(String userActive) {
    this.userActive = userActive;
}

public String getUserBOAsAgent() {
    return this.userBOAsAgent;
}

public void setUserBOAsAgent(String userBOAsAgent) {
    this.userBOAsAgent = userBOAsAgent;
}

public String getUserCreation() {
    return this.userCreation;
}

public void setUserCreation(String userCreation) {
    this.userCreation = userCreation;
}

public String getUserDisable() {
    return this.userDisable;
}

public void setUserDisable(String userDisable) {
    this.userDisable = userDisable;
}

public int getUserHasSignature() {
    return this.userHasSignature;
}

public void setUserHasSignature(int userHasSignature) {
    this.userHasSignature = userHasSignature;
}

public String getUserID() {
    return this.userID;
}

public void setUserID(String userID) {
    this.userID = userID;
}

public String getUserPasswordExpired() {
    return this.userPasswordExpired;
}

public void setUserPasswordExpired(String userPasswordExpired) {
    this.userPasswordExpired = userPasswordExpired;
}

public int getUserResetPassword() {
    return this.userResetPassword;
}

public void setUserResetPassword(int userResetPassword) {
    this.userResetPassword = userResetPassword;
}

public String getUserToken() {
    return this.userToken;
}

public void setUserToken(String userToken) {
    this.userToken = userToken;
}

public String getVenueCode() {
    return this.venueCode;
}

public void setVenueCode(String venueCode) {
    this.venueCode = venueCode;
}

}

persistence.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns     /persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence      http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
     <persistence-unit name="JPAAmex" transaction-     type="RESOURCE_LOCAL">
         <class>com.olsa.amex.entities.Agency</class>
         <class>com.olsa.amex.entities.AgencyRegionView</class>
         <class>com.olsa.amex.entities.Agent</class>
         <class>com.olsa.amex.entities.AgentAgencyRegionAndSubRegionRel</class>
         <class>com.olsa.amex.entities.AgentGroup</class>
         <class>com.olsa.amex.entities.AgentsInfo</class>
         <class>com.olsa.amex.entities.AmexStatoPriorita</class>
         <class>com.olsa.amex.entities.AmexXEROXField</class>
         <class>com.olsa.amex.entities.AmexXEROXSection</class>
         <class>com.olsa.amex.entities.Application</class>
         <class>com.olsa.amex.entities.ApplicationField</class>
    <class>com.olsa.amex.entities.ApplicationFieldStatus</class>
    <class>com.olsa.amex.entities.ApplicationFieldTemplate</class>
    <class>com.olsa.amex.entities.ApplicationFieldTemplateFixedValue</class>
    <class>com.olsa.amex.entities.ApplicationOfferAndRegionRel</class>
    <class>com.olsa.amex.entities.ApplicationOfferSuppBaseRel</class>
    <class>com.olsa.amex.entities.ApplicationPacakge</class>
    <class>com.olsa.amex.entities.ApplicationPackageArkDap</class>
    <class>com.olsa.amex.entities.ApplicationSection</class>
    <class>com.olsa.amex.entities.ApplicationSectionPdf</class>
    <class>com.olsa.amex.entities.ApplicationSectionTemplate</class>
    <class>com.olsa.amex.entities.ApplicationSignaturePDF</class>
    <class>com.olsa.amex.entities.ApplicationStatus</class>
    <class>com.olsa.amex.entities.ApplicationTemplate</class>
    <class>com.olsa.amex.entities.ApplicationTrace</class>
    <class>com.olsa.amex.entities.Applicationproductoffer</class>
    <class>com.olsa.amex.entities.AreaRegion</class>
    <class>com.olsa.amex.entities.AreaSubRegion</class>
    <class>com.olsa.amex.entities.AreamanagerAgent</class>
    <class>com.olsa.amex.entities.BankList</class>
    <class>com.olsa.amex.entities.Cab</class>
    <class>com.olsa.amex.entities.CabList</class>
    <class>com.olsa.amex.entities.CervedCredenzial</class>
    <class>com.olsa.amex.entities.Comuni</class>
    <class>com.olsa.amex.entities.ConfigurazioneSegnalazione</class>
    <class>com.olsa.amex.entities.Cordinate_Firma_Agente</class>
    <class>com.olsa.amex.entities.Crif</class>
    <class>com.olsa.amex.entities.ErrorSqlTrace</class>
    <class>com.olsa.amex.entities.InternationalTelPrefix</class>
    <class>com.olsa.amex.entities.Level_Rule</class>
    <class>com.olsa.amex.entities.Nazionalita</class>
    <class>com.olsa.amex.entities.NazioniIso</class>
    <class>com.olsa.amex.entities.New</class>
    <class>com.olsa.amex.entities.SBS_OfferList</class>
    <class>com.olsa.amex.entities.SbsOfferlist</class>
    <class>com.olsa.amex.entities.SectionStatus</class>
    <class>com.olsa.amex.entities.SegnalazioneApplicationSection</class>
    <class>com.olsa.amex.entities.Segnalazioni</class>
    <class>com.olsa.amex.entities.StorageCervedVisure</class>
    <class>com.olsa.amex.entities.Sysdiagram</class>
    <class>com.olsa.amex.entities.Teamleader_AM</class>
    <class>com.olsa.amex.entities.TempSignersInfo</class>
    <class>com.olsa.amex.entities.TemplateAppkeyStore</class>
    <class>com.olsa.amex.entities.TipologiaSegnalazione</class>
    <class>com.olsa.amex.entities.TokenPushService</class>
    <class>com.olsa.amex.entities.UsedPassword</class>
    <class>com.olsa.amex.entities.VersionMobile</class>
    <class>com.olsa.amex.entities.ViewAgencyareaandsubareaofferrelation</class>
    <class>com.olsa.amex.entities.ViewAgentagencyregionandsubregionrel</class>
    <class>com.olsa.amex.entities.ViewAgentsOfAreamanager</class>
    <class>com.olsa.amex.entities.ViewAgentssb</class>
    <class>com.olsa.amex.entities.ViewApplicationagent</class>
    <class>com.olsa.amex.entities.ViewApplicationsegnalazionibackoffice</class>
    <class>com.olsa.amex.entities.ViewAreamanagersOfTeamleader</class>
    <class>com.olsa.amex.entities.ViewArearegionsubregionrel</class>
    <class>com.olsa.amex.entities.ViewControlloallegati</class>
    <class>com.olsa.amex.entities.ViewControlloesistenzaagente</class>
    <class>com.olsa.amex.entities.ViewGetagentscompleteWoutfk</class>
    <class>com.olsa.amex.entities.ViewGetnew</class>
    <class>com.olsa.amex.entities.ViewGetsectionandstatus</class>
    <class>com.olsa.amex.entities.ViewSection_field</class>
    <class>com.olsa.amex.entities.ViewSectioncbsu</class>
    <class>com.olsa.amex.entities.ViewXroxsectionandfield</class>
    <class>com.olsa.amex.entities.VisureType</class>

    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://rmarjboss-001c.customer.olsa:3306/amex_digital_test_dev"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="*******"/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
        <property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>

And here's the code I used to test out everything:

try {
        logger.info("Hibernate test");
        ViewAgentSsbService service = new ViewAgentSsbService();
             List<ViewAgentssb> entities = service.findAll();
             logger.info(entities.toString());
         } catch (Exception e1) {
             logger.info("Failed... " + e1.getMessage());
             ErrorUtility.getError(e1);
         }

And the query successfully starts, but it always returns an empty list, even tho there is data present in the MySQL DB.

EDIT: I know I can use the Repositories, but for now I just want to check if everything works as it should.


Solution

  • I found the problem, apparently it was all caused by the wrong jdbc connection string. In order to generate the entities I used the following string:

    jdbc:mysql://rmarjboss-001c.customer.olsa:3306/amex_digital_sbs_dev?useUnicode=yes&characterEncoding=UTF-8
    

    What I used in my persistence.xml is this:

    jdbc:mysql://rmarjboss-001c.customer.olsa:3306/amex_digital_sbs_dev
    

    In order for everything to work correctly, I had to set the correct string in my persistence.xml:

    jdbc:mysql://rmarjboss-001c.customer.olsa:3306/amex_digital_sbs_dev?useUnicode=yes&amp;characterEncoding=UTF-8