reset password
Author Message
aligh1979
Posts: 121
Posted 23:12 Nov 16, 2011 |

I am looking at some csns2 project files , and I see that only in "pageDaoImpl"  we have something like

.createNamedQuery( "wiki.page.search", WikiPageSearchResult.class )........


while in other implemantaion files we have something like this

    return entityManager
            .createNamedQuery( "mailinglist.message.search" )

why is it that we create an extra class for WikiPageSearchResult and we do not do such a thing for others?

aligh1979
Posts: 121
Posted 02:37 Nov 17, 2011 |

Do we have any example on Csns2  site , to see how the search is applied ? I could not find one since all the wiki, mailing list ... etc pages do not exist in the csns2 project .

aligh1979
Posts: 121
Posted 02:40 Nov 17, 2011 |

there is this "coalesce"  keyword in createsql  file for the following function that I am not what it does exactly?

 

create function wiki_revisions_ts_trigger_function() returns trigger as $$
begin
    update wiki_pages set tsv =
        setweight( to_tsvector('pg_catalog.english', coalesce(new.subject,'')), 'A') ||..........................

cysun
Posts: 2935
Posted 09:13 Nov 17, 2011 |

1. EntityManager has two types of create query methods: createQuery/NativeQuery/NamedQuery(String) and createQuery/NativeQuery/NamedQuery(String, Class). The first type returns a Query and the second type returns a TypedQuery. Functionally they are pretty much the same, but syntactically Query returns Object or List<Object>, which you then have to cast to the result class type, but you don't need to do the casting for TypedQuery because the class type is already specified as an argument to the createQuery method.

2. The controller and view code for forums and wiki haven't been migrated to CSNS2 yet, but you can test the FTS queries with some unit tests. Currently there's a MessageDaoTests which tests searching mailing list messages.

3. If you concatenate null with something, the result would be null. coalesce() in the trigger code basically converts null to an empty string.

BTW, develop a good habit of reading documentation. For example, the difference between createNamedQuery(String) and createNamedQuery(String,Class) would be quite clear if you read the EntityManager API, and PostgreSQL functions are explained in details in the PostgreSQL documentation.

aligh1979
Posts: 121
Posted 16:06 Nov 17, 2011 |

what does this 16|32 does? specially the " | " ?

 order by ts_rank(tsv, query, 16 | 32)..................

cysun
Posts: 2935
Posted 16:18 Nov 17, 2011 |
aligh1979 wrote:

what does this 16|32 does? specially the " | " ?

 order by ts_rank(tsv, query, 16 | 32)..................

http://www.postgresql.org/docs/8.4/interactive/textsearch-controls.html#TEXTSEARCH-RANKING

aligh1979
Posts: 121
Posted 16:37 Nov 17, 2011 |

there are three things I do not understand about "MailingList" and "Message" in csns2 .

1. the MailingList class does not have any annotation as Entity , so the EntityManager should not automatically create a Table for it , however we have a table for it! and it exist in "ddl" file  . so how entityManger knows to create it?

2.the other thing is that the Message class has the Entity annotation , which links to "MailingList"  but it has extra columns that are not it's properties . why we do not have those columns as its properties , while entity manager magically creates these columns for this table ?

3. the last thing is about the trigger side , the trigger "trigger mailinglist_messages_ts_trigger" is set to be activated before insert or update . shouldn't be after insert or update instead?  since we need that vector to be created after the values are entered.?

Last edited by aligh1979 at 17:16 Nov 17, 2011.
aligh1979
Posts: 121
Posted 20:11 Nov 17, 2011 |
aligh1979 wrote:

there are three things I do not understand about "MailingList" and "Message" in csns2 .

1. the MailingList class does not have any annotation as Entity , so the EntityManager should not automatically create a Table for it , however we have a table for it! and it exist in "ddl" file  . so how entityManger knows to create it?

2.the other thing is that the Message class has the Entity annotation , which links to "MailingList"  but it has extra columns that are not it's properties . why we do not have those columns as its properties , while entity manager magically creates these columns for this table ?

 

for 1.  I found something on the top of the class that says : "/**
 * Currently JPA does not support interface as part of an association, so the
 * mappings of Subscribable and Subscription are done in Hibernate mapping files
 * under src/main/resources/hbm.
 * <p>
 * Mailinglist is mapped as a <union-subclass> of Subscribable in
 * Subscribable.hbm.xml.
 */"

I dont know why exactly , but thats the reason . and the properties are defined in that file.

 

for 2. it seems that some classes including Message are extending another class of inheritance type , called AbstractMessage , that has some extra fiels like subject , content .......


Last edited by aligh1979 at 20:12 Nov 17, 2011.
cysun
Posts: 2935
Posted 22:11 Nov 17, 2011 |

1 & 2. JPA 2.0 cannot map subclasses where the "superclass" is an interface, but HIbernate can. Hibernate also allows mixing mappings in annotation and mappings in XML, and that's why most of the CSNS2 classes are mapped with JPA annotations, but a few of them are mapped in XML.

3. "Before" means the tsv field of a new record should be populated "before" the record is inserted into the table.

aligh1979
Posts: 121
Posted 13:31 Nov 18, 2011 |

I am having problem , getting the search result back into my controller and not quite sure about the appraoch that I have taken for rubric search.

as I looked at the examples , it seems that search needs to be done against one table (tsv column) , so here for rubrics we have 3 tables involved , "rubric" itself , "criteria" and "criteria_description" . so other than a possible approach for creating a new entity class that contains the required field of all 3 tables (which makes things more complicated) , the easier approach would be picking one of those tables and add the required fields to it (on the sql side , not through hibernate) , I have chosen the table "criteria_description" to be the one that has those extra columns + tsv  column . -,although I am not sure if I had made the right choice .

so the next step would be the run the query and return the results . here comes the first problem . because the result that we are getting is mix of all 3 table we do not have any object (Entity Class) like that to cast it to , so I decided to do something like one of the examples in csns2 for wiki .  so basically I just copied the "WikiPageSerachResult.java" and after necessary modification , made it something suitable for search result of rubric.  the only annotation that I see on this page is @Embeddable . is this the only annnotation that I need ? 

after adding another method in my rubricDaoImp   , for search , this method will return a list of RubricSerachResult 
and I have something like :

.........

.createNamedQuery( "rubric.search", RubricSearchResult.class ) ...................

which is exactly the same approach as wiki . the only thing is left is doing the thing on the "NamedQueries.hbm.xml" . I am 99% sure about this part , since running this part directly in database returns the results (hard coding the query parameter that needed to be passed from the RubricDaoImp ... ) .

so at the end , as soon as I call that search method from my RubricDao , it will give me java.lang.ArrayIndexOutOfBoundsException:  exception .  so explaining all of this , the only part I am suspected to be the problem is the RubricSerachResult  file , that for some reason is not able to get the result and make an appropriate object . I am not sure if I need to have some extra annotation somewhere or doing something in my DaoImp file or controller , while I tried to approach this the same way as wiki..........

Last edited by aligh1979 at 13:31 Nov 18, 2011.
aligh1979
Posts: 121
Posted 05:35 Nov 19, 2011 |

Is there any way to get the value of database url , user pass , ....   from build.properties or one of xml files?

something similar exist for application email , through ApplicationProperties .

cysun
Posts: 2935
Posted 22:04 Nov 19, 2011 |
aligh1979 wrote:

Is there any way to get the value of database url , user pass , ....   from build.properties or one of xml files?

something similar exist for application email , through ApplicationProperties .

You can add them to ApplicationProperties like the other properties, but it's not a good practice so try not to do it if possible.

aligh1979
Posts: 121
Posted 22:33 Nov 19, 2011 |

Professor , if you could please take a look at my previous -Long -  question. regarding the approach that I had taken and not being successful at casting ......  (above , posted on NOV 18th)

the reason I need to access my database info is that I could not use having my query in NamedQuery.hbm.xml , I did some direct old fashion connection to the database with JDBC , and it my database info (user , pass ..) are hard coded , and it is fine in my system , but since you are going to test it yourself in your system , it is not going to work , unless you change the hard coded database info , or add it to application properties like the email ... . 

cysun
Posts: 2935
Posted 00:04 Nov 20, 2011 |
aligh1979 wrote:

Professor , if you could please take a look at my previous -Long -  question. regarding the approach that I had taken and not being successful at casting ......  (above , posted on NOV 18th)

...

It's an ArrayIndexOutOfBound error. You should be able to fix it yourself. It's a homework after all.

aligh1979
Posts: 121
Posted 00:38 Nov 20, 2011 |

Professor ,  I was just trying to say that no result gets generated or casted properly  in order to be added into Array , so obviously the Array will be empty and I get this exception .(so the Array thing is not the real problem) . one more thing to ask is that I really need to get the value of the property from  the Xml file , I search a lot , found many thing , the first thing that I need to fix is to have the right relative path for the Xml file in my controller , and whatever combination I try , the file can not be located - here i am trying to address spring data.xml    file.....

Last edited by aligh1979 at 00:48 Nov 20, 2011.
cysun
Posts: 2935
Posted 09:35 Nov 20, 2011 |
aligh1979 wrote:

Professor ,  I was just trying to say that no result gets generated or casted properly  in order to be added into Array , so obviously the Array will be empty and I get this exception .(so the Array thing is not the real problem) . one more thing to ask is that I really need to get the value of the property from  the Xml file , I search a lot , found many thing , the first thing that I need to fix is to have the right relative path for the Xml file in my controller , and whatever combination I try , the file can not be located - here i am trying to address spring data.xml    file.....

Your general approach seems to be OK, except that

a) you don't have to search just one tsv column. It's SQL after all, so you could easily do something like "where tsquery @@ rubrics.tsv or tsquery @@ criteria.tsv or tsquery @@ criteria_descriptoins".

b) If I had to put all the fields in one tsv column (which wouldn't be a bad idea), I'd probably use the rubrics table instead of criteria_desriptions table.

As for injecting some additional properties into your beans, you can modify spring.xml and add them to the properties of ApplicationProperties, and then inject ApplicationProperties to your beans (@Autowired would do). Note that if you are adding some properties that are environment-dependent, e.g. db url etc., you need to notify me so I can change them when I grade your homework.

aligh1979
Posts: 121
Posted 09:54 Nov 20, 2011 |

Thanks for guidance , is there any thing else I should do to make that casting work with my newly made class for rubric search ?,- imitating the same way as "wikiPageSearchResult" is made (in Util folder).

cysun
Posts: 2935
Posted 10:05 Nov 20, 2011 |
aligh1979 wrote:

Thanks for guidance , is there any thing else I should do to make that casting work with my newly made class for rubric search ?,- imitating the same way as "wikiPageSearchResult" is made (in Util folder).

It should work, and if not, debug.