-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: what HQLcan I use to reach a string array
PostPosted: Fri Feb 08, 2008 10:03 am 
Beginner
Beginner

Joined: Thu Jun 07, 2007 2:38 am
Posts: 28
Location: Italy, Rome
My class looks like this

Code:
class Resource{

String[] keywords;


doing this HQL

Code:
FROM Resource resource WHERE  keywords[0] like '%searchstring%'


results in

Quote:
java.lang.ClassCastException: org.hibernate.type.SerializableType cannot be cast to org.hibernate.type.CollectionType


I replaced the keywords array with
Code:
@CollectionOfElements List<String> keywords

but that resulted in other errors.

What is the HQL to filter on a string array?


Top
 Profile  
 
 Post subject: Re: what HQLcan I use to reach a string array
PostPosted: Fri Feb 08, 2008 1:06 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I am wondering if you could use a join and mix it with the index function. Something like this:

Code:
from Resource resource inner join resource.keywords keyword where keyword like '%abc%' and index(keyword)=0



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 08, 2008 2:10 pm 
Beginner
Beginner

Joined: Thu Jun 07, 2007 2:38 am
Posts: 28
Location: Italy, Rome
I tried but the join gives a nullpointer exception and the index(keywords)=0 an InvalidDataAccessApiUsageException (I tried it seperately). But thanx anyway! Mmmm my problem is still open!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 08, 2008 2:16 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
How does the mapping look like now?


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 09, 2008 7:13 am 
Beginner
Beginner

Joined: Thu Jun 07, 2007 2:38 am
Posts: 28
Location: Italy, Rome
I didn't change the mapping(seen above). But maybe I did not understand your question. We use annotations to map the domain to the database.

Our domain is here:
https://giews.svn.sourceforge.net/svnroot/giews/gw_giews/trunk/fenix-domain/

Our dao's are here:
https://giews.svn.sourceforge.net/svnroot/giews/gw_giews/trunk/fenix-persistence/
In this project is the testclass which I use to test:
/fenix-persistence/src/test/java/org/fao/fenix/persistence/search/SearchDaoTest._testSearch2()

So, the question is still open: How do I filter with HQL on String[] keywords ?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 09, 2008 6:40 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I didn't look at your mapping but here is a simple test I did and it ran successfully.

Entity class:
Code:
@Entity
@Table(name = "Document")
public class Document implements Serializable
{
    @Id @GeneratedValue
    @Column(name = "Id")
    private Long id;

    @Column(name = "Name")
    private String name;

    @JoinTable(name = "Label", joinColumns = {@JoinColumn(name = "DocID")})
    @CollectionOfElements(targetElement = String.class)
    @Column(name = "Name")
    @IndexColumn(name = "Position")
    private String[] labels;

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String[] getLabels()
    {
        return labels;
    }

    public void setLabels(String[] labels)
    {
        this.labels = labels;
    }
}


and the test:
Code:
public class Driver22
{
    public static void main(String[] args)
    {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();

        Query query = em.createQuery("select doc from Document doc where doc.labels[0] like '%Test'");

        List<Document> docs = query.getResultList();

        for (Document doc : docs)
        {
            System.out.println(">>> doc = " + doc.getName());
        }

        em.getTransaction().commit();

        em.close();
        emf.close();
    }


compare this with what you have and you should be able to find the problem.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 11, 2008 6:23 am 
Beginner
Beginner

Joined: Thu Jun 07, 2007 2:38 am
Posts: 28
Location: Italy, Rome
Thanx man! I run into a subsequent problem but I will post that as another issue.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.