Find with Criteria/Example: object containing a List
by Lev
hi,
i am trying to execute a find using Criteria/Example on an
Entity that contains a List of enum. consider a class such
as:
@Entity
public class MockEntity extends ModelBase implements Serializable {
@CollectionOfElements()
@Enumerated(EnumType.STRING)
private List<MockEnum> mockEnums;
...
}
here, the enum is defined as:
public enum MockEnum {
GOOD, BAD, OTHER;
}
i am trying to find MockEntity objects containing a prescribed MockEnum
contained in the MockEntity.List<MockEnum> list.
i have created a findByExample() method that utilizes Criteria/Example
to execute the query:
public List<T> findByExample(T exampleInstance, String[] excludeProperty) {
Criteria criteria = getSession().createCriteria(entityClass);
Example example = Example.create(exampleInstance);
if (excludeProperty != null) {
for (int i = 0; i < excludeProperty.length; i++)
example.excludeProperty(excludeProperty[i]);
}
criteria.add(example);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
return criteria.list();
}
i tried calling the findByExample() method as follows:
MockEntity example = new MockEntity();
List<MockEntity.MockEnum> enums = new ArrayList<MockEntity.MockEnum>();
enums.add(MockEntity.MockEnum.OTHER);
example.setMockEnum(enums);
dao = new MockEntityDao();
List<MockEntity> found = dao.findByExample(example, null);
but, the List called found contains all of the MockEntity residing in the
DB -- not merely the MockEntity containing MockEnum.OTHER in
the associated MockEntity.List<MockEnum>.
does anybody have suggestions on how i can achieve this: find
MockEntity objects that contain a prescribed MockEnum in their list?
thank you for your help.
14 years, 3 months
Communication link failure
by findbestopensource
Hello all,
We have launched a new website www.findbestopensource.com which lists
the best opensource product across platforms.
We are using hibernate and MySQL and c3p0 as connection pool provider.
I am getting the below error very frequently. Could you please guide
me.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
MESSAGE: The last packet successfully received from the server was
45,172,983 milliseconds ago. The last packet sent successfully to the
server was 45,172,993 milliseconds ago. is longer than the server
configured value of 'wait_timeout'. You should consider either
expiring and/or testing connection validity before use in your
application, increasing the server configured values for client
timeouts, or using the Connector/J connection property
'autoReconnect=true' to avoid this problem.
Regards
Aditya
www.findbestopensource.com
14 years, 3 months
different HQL condition in classes with inheritance for different subclasses
by Thomas Menke
Hi,
I am writing an application to manage a library. I am doing this for
learning purposes only.
I have different customer types that have an associated maximum number
of items that they may lend from the library.
The customers have a base class that looks like that (shortend for
simplicity)
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name
= "type")
abstract class Customer
{
...
public abstract int getItemsAllowed();
public Collection<ItemLoan> getItemLoans();
}
Then there may be subclasses like
@...
class NormalCustomer
{
public static final int MAX_ITEMS = 5;
public int getItemsAllowed() {return MAX_ITEMS;}
}
@...
class VIPCustomer
{
public static final int MAX_ITEMS = 30;
public int getItemsAllowed() {return MAX_ITEMS;}
}
Now I want to get a list with all customers that are currently lending
the maximum allowed number of items (getItemLoans().size() ==
getItemsAllowed()).
Is something like that possible with one hql query?
Thanks,
Thomas
14 years, 4 months