-->
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.  [ 8 posts ] 
Author Message
 Post subject: Specifying Restrictions for primitive collection
PostPosted: Tue Jan 19, 2016 5:12 am 
Newbie

Joined: Tue Jan 12, 2016 3:59 am
Posts: 10
This is slightly different from my previous question (for the same class) so I'll start a new topic. I have a class defined like this:
Code:
@Entity
@Table(name = "ncc_failure")
public class NccFailure
{
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_STORE")
    private long id;


    @CollectionOfElements( fetch = FetchType.EAGER)
    @JoinTable(
            name = "ncc_failure_subnet_link",
            joinColumns = @JoinColumn(name = "ncc_failure__id")
    )
    @Column( name="ip_subnet_id", nullable = false)
    private final Set<String> subnetIds = new HashSet<>();

    @AccessType("property")
    @Lob
    @Column(name = "summary")
    private String summary;


    @AccessType("property")
    @Lob
    @Column(name = "file_lines_data")
    private String  fileLinesData;
}


Now I have an existing framework that makes use of Criteria and Restrictions to specify conditions for SELECT (most it tries to convert English-language conditions into Criteria/Restrictions in order to fetch the right record). The question I have now is how to write Criteria/Restrictions against the subnetIds variable, because it's a collection.
For example I want to get all the NccFailure whose subnetIds may contain an ID "12345". I would like to write a condition as:

criteria.add(org.hibernate.criterion.Restrictions.contains("subnetIds", "12345"));

Unfortunately this doesn't work, because there's no "contains" factory method for Restrictions. Is there a way I can achieve what I need?


Top
 Profile  
 
 Post subject: Re: Specifying Restrictions for primitive collection
PostPosted: Tue Jan 19, 2016 5:42 am 
Newbie

Joined: Tue Jan 12, 2016 3:59 am
Posts: 10
Similarly, what if I want to specify a Restrictions for a "any subnetId like '%abcd%'" condition?


Top
 Profile  
 
 Post subject: Re: Specifying Restrictions for primitive collection
PostPosted: Tue Jan 19, 2016 9:36 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Checkout this section in the Hibernate documentation:

Code:
List cats = session.createCriteria(NccFailure.class)
    .createCriteria("subnetIds")
        .add(Restrictions.eq("elements", "12345"))
    .list();


The same could be done for like:

Code:
List failures = session.createCriteria(NccFailure.class)
    .createCriteria("subnetIds")
        .add(Restrictions.eq("elements", "%abcd%"))
    .list();


Top
 Profile  
 
 Post subject: Re: Specifying Restrictions for primitive collection
PostPosted: Tue Jan 19, 2016 7:19 pm 
Newbie

Joined: Tue Jan 12, 2016 3:59 am
Posts: 10
I did like you said but got this exception:

org.springframework.orm.hibernate3.HibernateSystemException: collection was not an association: com.redsealsys.srm.common.ncc.NccFailureImpl.subnetIds; nested exception is org.hibernate.MappingException: collection was not an association: com.redsealsys.srm.common.ncc.NccFailureImpl.subnetIds

It looks like Hibernate is understanding the Criteria to an object association instead of collection of primitive types.

Does it have anything to do with Hibernate version? I notice the doc you were referring to is 4.3, while what we have is 3.3.1. Does it matter?


Top
 Profile  
 
 Post subject: Re: Specifying Restrictions for primitive collection
PostPosted: Tue Jan 19, 2016 11:09 pm 
Newbie

Joined: Tue Jan 12, 2016 3:59 am
Posts: 10
I cannot find an equivalent section or something similar to it (createCriteria for a collection of scalar/primitive values) in the 3.3 doc. So my assumption is this was only allowed starting in 4.3...


Top
 Profile  
 
 Post subject: Re: Specifying Restrictions for primitive collection
PostPosted: Wed Jan 20, 2016 2:26 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Nowadays, people use JPA Criteria API and so the legacy criteria queries are deprecated and now further development will be done.
Hibernate 3.5 supports JPA 2.0, so give it a try with JPA Criteria and see if it works better.


Top
 Profile  
 
 Post subject: Re: Specifying Restrictions for primitive collection
PostPosted: Wed Jan 20, 2016 4:00 am 
Newbie

Joined: Tue Jan 12, 2016 3:59 am
Posts: 10
By the way the quality of that 4.3 doc is, how do I put it, rather unreliable. In section 18.1 it still lists this code as valid:
Code:
sess.createSQLQuery("SELECT * FROM CATS")
.addScalar("ID", Hibernate.LONG)
.addScalar("NAME", Hibernate.STRING)
.addScalar("BIRTHDATE", Hibernate.DATE)


Well the thing is Hibernate.STRING/LONG/DATE have all been removed, so that code will not be compilable in 4.3


Top
 Profile  
 
 Post subject: Re: Specifying Restrictions for primitive collection
PostPosted: Wed Jan 20, 2016 4:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
The Hibernate 4.x documentation is out-dated, and the 4.x branch is not under development anymore.
As of writing, I'm working on a new User Guide for the 5.1 release which is mostly written from scratch.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.