Reputation: 230
I have this ArrayList:
List<String> zainteresowanka = new ArrayList<String>();
zainteresowanka.add("Java");
zainteresowanka.add("Platforma .NET (C#)");
zainteresowanka.add("Android");
zainteresowanka.add("iOS");
zainteresowanka.add("Windows Phone");
zainteresowanka.add("Technologie WWW (HTML, CSS, JavaScript)");
zainteresowanka.add("Testowanie aplikacji");
zainteresowanka.add("Projektowanie aplikacji");
zainteresowanka.add("Analiza Biznesowa");
zainteresowanka.add("Inne");
And those are things that someone can choose what he likes. If he choose "Inne", then he must enter some other thing that he likes that this ArrayList doesn't contain.
It is written to database like that:
- Platforma .NET (C#) 2. iOS 3. Windows Phone
in one string.
The method that saves it to a string looks like this:
zainteresowaniaa = zainteresowaniaa + " " + licz + ". " + zainteresowanka.get(wyboor);
licz = licz + 1;
What I need to do is to search the list based on the ArrayList zainteresowanka
, so I can choose for example "Java, iOS" and I get everyone who had choosen those ones.
How can I do this?
Upvotes: 1
Views: 144
Reputation: 1965
From what i understand
You have an arraylist that contains items that can be chosen by users.You will save the chosen items to database . Then you need to get the users who selected some item like "windows phone" for example
First of all you need to assign an id for different users to distinguish them.
Store the details in database with userid
for example
==============================
userid | chosen items
==============================
1 | windows ios android
2 | android ios
===============================
Then you need to search in the database ( assuming mysql or sql like ) for the specific items (value1,value2) . You can use "LIKE" or "IN" operator
you can use SQL IN operator to search multiple absolute values:
SELECT userid FROM tablename WHERE name IN ( 'Value1', 'Value2', ... );
If you want to use LIKE you will need to use OR instead:
SELECT userid FROM tablename WHERE name LIKE '%Value1' OR name LIKE '%Value2'; Using AND (as you tried) requires ALL conditions to be true, using OR requires at least one to be true.
Here you will get the list of userid and hence users who have chooses specific items value1,value2
You can use Native sql queries or create query . But search feature is best implemented by Hibernate search query DSL
Query query = session.createQuery("SELECT u FROM UserData u WHERE u.userRole IN (:roles)");
query.setParameterList("roles", roles);
more info in the link below
http://w3facility.org/question/jpa-get-all-rows-that-matches-multiple-strings/
You can search multiple keywords on multiple fields with this
Query luceneQuery = queryBuilder
.keyword()
.wildcard()
.onField("foo")
.matching("bar*")
.createQuery();
http://hibernate.org/search/documentation/getting-started/
Upvotes: 2