So I got query that I made list from and then pass that list to another method that should print it.
Query query = session.createQuery("from Osoba as o FETCH ALL PROPERTIES WHERE ((o.id) = (id_osoby)) and (LOWER(Zainteresowania.zainteresowanie) LIKE ?) and (LOWER(Zainteresowania.zainteresowanie) LIKE ?) and (LOWER(Zainteresowania.zainteresowanie) LIKE ?) and (LOWER(Zainteresowania.zainteresowanie) LIKE ?) and (LOWER(Zainteresowania.zainteresowanie) LIKE ?)");
// Query query = session.createQuery("from Osoba WHERE (LOWER(zainteresowania) LIKE ?)"); // Stara, zachowana tymczasowo
query.setString(0,"%"+input1+"%");
query.setString(1,"%"+input2+"%");
query.setString(2,"%"+input3+"%");
query.setString(3,"%"+input4+"%");
query.setString(4,"%"+input5+"%");
List<Osoba> osoby = query.list(); // robimy sobie liste na podst zapytania
wyswietlWybrane(osoby);
session.getTransaction().commit();
session.close();
}
for eg. when I want to print all I just use:
List<Osoba> osoby = session.createQuery("from Osoba").list(); // tworzymy sobie liste z zapytania do bazy ktora wyswietli nam wszystkich
wyswietlWybrane(osoby); // przekazujym ta liste do innej i wyswietlamy
method that prints what I pass:
private void wyswietlWybrane(List<Osoba> osoby) {
for (Osoba a : osoby) {
List zainteresowania1 = a.getZainteresowania();
System.out.println(a.getId() +". " + a.getImie() + " " + a.getNazwisko() + "\nTelefon: " + a.getTelefon() + "\nEmail: " + a.getEmail() + "\nUczelnia: " + a.getUczelnia() + "\t\n" + "Skad slyszal: " + a.getSkadSlyszal());
System.out.print("Obszary zainteresowan: ");
for (Iterator iterator2 = zainteresowania1.iterator(); iterator2.hasNext();){
Zainteresowania nazwa = (Zainteresowania) iterator2.next();
System.out.print(nazwa.getZainteresowanie() + ". ");
}
}
}
map files:
Osoba
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.mycompany.kwestionariusz">
<class name="Osoba" table="DANEOSOBOWE">
<id name="id" column="ID" type="integer">
<generator class="native"/>
</id>
<property name="imie" column="Imie"/>
<property name="nazwisko" column="Nazwisko"/>
<property name="telefon" column="Telefon"/>
<property name="email" column="Email" />
<property name="uczelnia" column="Uczelnia" />
<property name="doswiadczenie" column="Doswiadczenie" />
<list name="zainteresowania" cascade="all">
<key column="id_osoby"/>
<list-index column="idx"/>
<one-to-many class="Zainteresowania"/>
</list>
<property name="skadSlyszal" column="SkadSlyszal" />
</class>
</hibernate-mapping>
Zainteresowania:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.mycompany.kwestionariusz">
<class name="Zainteresowania" table="ZAINTERESOWANIA">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="zainteresowanie" column="zainteresowanie" type="string"/>
</class>
</hibernate-mapping>
Osoba class:
private int id;
private String imie;
private String nazwisko;
private String email;
private String telefon;
private String uczelnia;
private String doswiadczenie;
private String skadSlyszal;
private List zainteresowania;
public Osoba(){ // domyslny
}
// konstruktor zeby mozna bylo sobie w jednej linijce dodawac osobe
public Osoba(String imie1, String nazwisko1, String telefon1, String email1, String uczelnia1, String doswiadczenie1, String skadSlyszal1 ){
this.imie = imie1;
this.nazwisko = nazwisko1;
this.email = email1;
this.telefon = telefon1;
this.uczelnia = uczelnia1;
this.doswiadczenie = doswiadczenie1;
this.skadSlyszal = skadSlyszal1;
}
// kontr przyj caly obiekt
public Osoba(Osoba tymczasowa){
this.imie = tymczasowa.imie;
this.nazwisko = tymczasowa.nazwisko;
this.email = tymczasowa.email;
this.telefon = tymczasowa.telefon;
this.uczelnia = tymczasowa.uczelnia;
this.doswiadczenie = tymczasowa.doswiadczenie;
this.skadSlyszal = tymczasowa.skadSlyszal;
}
// gettery, settery
public int getId() {
return id;
}
public String getImie() {
return imie;
}
public String getNazwisko() {
return nazwisko;
}
public String getEmail() {
return email;
}
public String getTelefon() {
return telefon;
}
public String getUczelnia() {
return uczelnia;
}
public String getDoswiadczenie() {
return doswiadczenie;
}
public List getZainteresowania() {
return zainteresowania;
}
public String getSkadSlyszal() {
return skadSlyszal;
}
public void setImie(String string) {
this.imie = string;
}
public void setId(int in) {
this.id = in;
}
public void setNazwisko(String string) {
this.nazwisko = string;
}
public void setEmail(String string) {
this.email = string;
}
public void setTelefon(String string) {
this.telefon = string;
}
public void setUczelnia(String string) {
this.uczelnia = string;
}
public void setDoswiadczenie(String string) {
this.doswiadczenie = string;
}
public void setZainteresowania(List string) {
this.zainteresowania = string;
}
public void setSkadSlyszal(String string) {
this.skadSlyszal = string;
}
Zainteresowania class:
private int id;
private String zainteresowanie;
public Zainteresowania(){ // domyslny
}
public Zainteresowania(String zainteresowanie){
this.zainteresowanie = zainteresowanie;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getZainteresowanie() {
return zainteresowanie;
}
public void setZainteresowanie(String zainteresowanie) {
this.zainteresowanie = zainteresowanie;
}
Ok well when I am trying to do this seach query and I pass argument I am getting an error:
ERROR: Invalid path: 'null.zainteresowanie'
kwi 22, 2015 3:05:00 PM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: Invalid path: 'null.zainteresowanie'
Invalid path: 'null.zainteresowanie'
Exception in thread "main" org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'null.zainteresowanie' [from com.mycompany.kwestionariusz.Osoba as o FETCH ALL PROPERTIES WHERE ((o.id) = (id_osoby)) and (LOWER(Zainteresowania.zainteresowanie) LIKE ?) and (LOWER(Zainteresowania.zainteresowanie) LIKE ?) and (LOWER(Zainteresowania.zainteresowanie) LIKE ?) and (LOWER(Zainteresowania.zainteresowanie) LIKE ?) and (LOWER(Zainteresowania.zainteresowanie) LIKE ?)]
when I change query to this:
Query query = session.createQuery("from Osoba as o, Zainteresowania as z FETCH ALL PROPERTIES WHERE ((o.id) = (id_osoby)) and (LOWER(z.zainteresowanie) LIKE ?) and (LOWER(z.zainteresowanie) LIKE ?) and (LOWER(z.zainteresowanie) LIKE ?) and (LOWER(z.zainteresowanie) LIKE ?) and (LOWER(z.zainteresowanie) LIKE ?)");
I am getting error:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.mycompany.kwestionariusz.Osoba
at com.mycompany.kwestionariusz.Osoba.wyswietlWybrane(Osoba.java:226)
at com.mycompany.kwestionariusz.Osoba.wyszukajOsoby(Osoba.java:218)
at com.mycompany.kwestionariusz.main.main(main.java:42)
Line 226 (its wyswietlWybrane method, posted up there)
for (Osoba a : osoby) {List zainteresowania1 = a.getZainteresowania();
line 218 (part of searching method):
wyswietlWybrane(osoby);
I really do need help, tried it in many ways, nothing seem to works.
I believe that the reason is that search query is creating other List than Osoba list - but I need to compare Osoba ID to Zainteresowania ID_Osoby, so how to do this and still be able to pass my result list to my wyswietlWybrane method?
The reason you are running into this problem is that you are specifying two Hibernate entities in the FROM section of your query and you are by default selecting every column of both tables since you dont specify a SELECT.
This is returning an Object[] of entity properties which is why you are getting the cast exception.
To fix this try explicitly SELECT every named property of o entity that you want and it should be smart enough to properly map to o entities instead of an Object[].
Also I noticed that you are not actually joining the two tables together. This will give you a cartesian product I believe. To fix this add a join to your filter:
This will limit z entities to ones that exist as children in o.
that's the solution, well there are some other problems but at least the list of people is being made