File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Counting Matches Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Counting Matches" Watch "Counting Matches" New topic
Author

Counting Matches

Patrick McDonogh
Ranch Hand

Joined: Oct 13, 2005
Posts: 89
Hi there all,

I am trying to count the number of times a word is found in a database field using Hibernate.
For instance say i have a table called Person and a Person java class.

class Person {

String name;
String decsription;

etc,

}

The description of a person can be up to 500 characters.

I then run a query like "from Person p where p.description like '%chocolate%'".

Say i have 4 people in the database whose descriptions are as follows:

Person 1 description - I really love chocolate.

Person 2 description - chocolate is my favorite thing, especially dark chocolate.

Person 3 description - I like long walks.

Person 4 description - chocolate, chocolate, i absolutelly love chocolate


This query will then return people 1, 2 and 4 but not 3 as 3's description does not contain the word chocolate.

So what my question is, is that i want to create a counter for each row showing how many times chocolate occured in that persons description.
I then also want to order by this counter. For instance the counters for the 4 people would be the number of times chocolate appeared in their decsription.

So:

Person 1 - 1
Person 2 - 2
Person 3 - 0
Person 4 - 3

Does anyone know how to do this and how especially to do it with criteria and HQL.
If someone knows where an example or documentation, or what the technical term for this dynamic counting please could you let me know. Is this even possible using a relational database.

Cheers,

Patrick
 
 
subject: Counting Matches
 
Threads others viewed
Getting selected contents from database table
How to build an array of objects of a class...
Series of stars
Struts2 and XML subnodes to show database relationships?
how to compare if object exists in arraylist and add if doesn't
IntelliJ Java IDE