所有JPA查询都无法找到任何结果(即使MySQL数据库中存在数据)

时间:2019-04-05 14:15:18

标签: java hibernate jpa entitymanager

我正在尝试在我的项目中设置javax.Persistence,并且我已经开始使用基本方法:

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(目前我只实现了findAll和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();
}

}

服务类别:

  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();

}

}

实体:

 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>

这是我用来测试所有内容的代码:

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);
         }

查询成功启动,但始终返回一个空列表,即使MySQL数据库中存在数据也是如此。

编辑:我知道我可以使用存储库,但是现在我只想检查是否一切正常。

1 个答案:

答案 0 :(得分:4)

我发现了问题,显然这是由错误的jdbc连接字符串引起的。 为了生成实体,我使用了以下字符串:

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

我在persistence.xml中使用的是:

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

为了使一切正常工作,我必须在persistence.xml中设置正确的字符串:

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