Getting values using boolean field in sleep mode
I am using postgres and I have a table like this:
CREATE TABLE entity_transaction
(
id bigint NOT NULL DEFAULT nextval('entity_transactionid_seq'::regclass),
transaction_ref character varying(11) NOT NULL,
transaction_type character varying(10) NOT NULL,
is_remitted boolean DEFAULT false);
I created a model class as follows for this table.
@Entity
@Table(name = "entity_transaction")
public class Transaction implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id")
Long id;
@Column(name = "transaction_ref")
String transactionRef;
@Column(name = "transaction_type")
String transactionType;
@Column(name = "is_remitted")
Boolean isRemitted;
I'm just trying to get values from this table using the supplied "isRemitted", true or false. For this I am trying the following query
@Repository(value = "collectionsRepositary")
public class CollectionsRepositoryImpl implements CollectionsRepository {
@Resource
private SessionFactory sessionFactory;
@Override
public List<Transaction> getUnmatchedList() {
Query query = createQuery("select t from Transaction where t.transactionType = 'c' and t.isRemitted is true");
return query.list();
}
public Query createQuery(String queryString) {
return getSession().createQuery(queryString);
}
private Session getSession() {
return sessionFactory.getCurrentSession();
}
}
But I cannot get the values returning an empty list. This table has data that contains this boolean and the type of transaction.
Can anyone give me the correct way?
thanks in advance,
Not sure, but it is true
sounds especially for me. I think your query should work with = true
.
EDIT: Sorry, I didn't see this, your request should look like this:
select t from Transaction t where t.transactionType = 'c' and t.isRemitted = true
instead of this:
select t from Transaction t.transactionType = 'c' and t.isRemitted is true
Missing where
.
Check the generated request; Hibernate should render java.lang.Boolean
as BIT and maybe PG boolean
shouldn't be BIT but a similar (but different) type (like TINYINT).
In my experience, I have always mapped Boolean fields to tiny or custom type; check SO.