简体   繁体   中英

Hibernate JOIN query not working: Java/JSP with Hibernate, IDE Netbeans/GlassFish, Windows 10

Why doesn't this query work?

  query = "SELECT itm.itemId, itm.itemModel, itm.itemDescription, "
          + " itmImages.imageFileName, part.participant_id "
          + " FROM Users user "
          + " INNER JOIN user.participant part "
          + " INNER JOIN part.addresses addr "
          + " INNER JOIN part.item itm "    
          + " INNER JOIN itm.itemImages itmImages "
          + " WHERE user.userType LIKE '%borrow%') AND itm.itemDescription LIKE '%mower%') AND addr.addressType = 'primary'";

It always returns all items, disregards itemDescription LIKE... I checked the database and all of the join ids are fine

This works:

  query = "SELECT user"
          + " FROM Users user "
          + " INNER JOIN user.participant part "
          + " INNER JOIN part.addresses addr "
          + " WHERE user.userType LIKE '%borrow%') AND addr.addressType = 'primary'";

I have a table Users. It has a one-to-many association with table Participant.

In Users.java I have..

private Set<Participant> participant = new HashSet<Participant>();

with

  @OneToMany
  @JoinTable(name = "echomarket.hibernate.Participant")
  @JoinColumn(name = "user_id")
  public Set<Participant> getParticipant() {
    return participant;
  }

  public void setParticipant(Set<Participant> participant) {
    this.participant = participant;
  }

This join works fine.

In Participant.java I have

  private Set<Addresses> addresses = new HashSet<Addresses>();

with

  @OneToMany
  @JoinTable(name = "echomarket.hibernate.Addresses")
  @JoinColumn(name = "participant_id")
  public Set<Addresses> getAddresses() {
    return addresses;
   }

  public void setAddresses(Set<Addresses> addresses) {
    this.addresses = addresses;
  }

And private Set item = new HashSet();

With

  @OneToMany
  @JoinTable(name = "echomarket.hibernate.Items")
  @JoinColumn(name = "participant_id")
  public Set<Items> getItem() {
    return item;
 }

No association statement with regard to Users.

In Addresses.java I make no associations.

In Items.java I have

  private Set<ItemImages> itemImages = new HashSet<ItemImages>();

with

  @OneToMany
  @JoinTable(name = "echomarket.hibernate.ItemImages")
  @JoinColumn(name = "itemId")
  public Set<ItemImages> getItemImages() {
   return itemImages;
  }

 public void setItemImages(Set<ItemImages> itemImages) {
   this.itemImages = itemImages;
  }

In ItemImages.java I make no associations...

Very much thanks for your help. If you need more information, please just ask...

Liz

Couldn't get the above INNER JOIN query to work-- intended for a search that involves many tables. I tested each JOIN independently successfully, but together the JOIN does not work. Also had problems with query recognizing LIKE statement. I worked really hard to find solutions. So I rewrote with the following, data gathering is split in two query calls. The following works:

public String SearchResults() {

Session sb = null;
Transaction tx = null;
String queryString = "";
String forceString = this.found_zip_codes;
List results = null;
String fromStatement = "";
if (this.lenderOrBorrower == 2) {
  this.which = "borrow";
} else {
  this.which = "lend";
}
this.imageLibrary = this.which + "_images";

fromStatement = " SELECT  part "
        + "  FROM Participant part "
        + "  INNER JOIN part.addresses addr "
        + "  WHERE addr.addressType = 'primary' ";

if (ubean.getComDetailID() != null) {
  fromStatement = fromStatement + "  AND part.communityId = \'" + ubean.getComDetailID() + "\' ";
} else {
  fromStatement = fromStatement + "  AND part.communityId = ''";
}

if (this.postalCode.isEmpty() == false) {
  fromStatement = fromStatement + " OR addr.postalCode LIKE \'" + this.postalCode + "%\'";
}

try {
  sb = hib_session();
  tx = sb.beginTransaction();
  results = sb.createQuery(fromStatement).list();
  tx.commit();
} catch (Exception ex) {
  tx.rollback();
  Logger.getLogger(SearchesBean.class.getName()).log(Level.SEVERE, null, ex);
} finally {
  tx = null;
  sb = null;
}

String[] pids = new String[results.size()];
String hold_pid = "";
if (results != null) {
  if (results.size() > 0) {
    for (int i = 0; i < results.size(); i++) {
      Participant cArray = (Participant) results.get(i);
      if (cArray.getParticipant_id().isEmpty() == false) {
        hold_pid = "\'" + cArray.getParticipant_id() + "\'";
        pids[i] = hold_pid;
      }
    }
    hold_pid = String.join(",", pids);
  }
}

results = null;
fromStatement = " FROM Items itm WHERE itm.itemType = :which "
        + (hold_pid.isEmpty() ? "" : " AND itm.participant_id IN ( " + hold_pid + " )");


if ((this.startDate.isEmpty() == false) && (this.endDate.isEmpty() == false)) {
  try {
      queryString = queryString + " OR ";
      queryString = queryString + " ( itm.dateCreated >= \'" + this.startDate + "\' AND itm.dateCreated <= \'" + this.endDate + "\' ) ";

  } catch (Exception ex) {
    Logger.getLogger(SearchesBean.class.getName()).log(Level.INFO, null, ex);
  }
}
forceString = this.keyword;
if (forceString.isEmpty() == false) {
  queryString = queryString + " OR ";
  queryString = queryString + " (itm.itemDescription like \'%" + forceString + "%\' OR itm.itemModel like \'%" + forceString + "%\')";
}

if ((this.categoryId != -2)) {
  queryString = queryString + " OR";
  queryString = queryString + " itm.categoryId = " + this.categoryId;
}

fromStatement = fromStatement + queryString;

try {
  sb = hib_session();
  tx = sb.beginTransaction();
  results = sb.createQuery(fromStatement).setParameter("which", this.which).list();
  tx.commit();
} catch (Exception ex) {
  tx.rollback();
  Logger.getLogger(SearchesBean.class.getName()).log(Level.SEVERE, null, ex);
} finally {
  tx = null;
  sb = null;
}

this.itemDetail = results;

return "search";

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM