I recently replaced PostgreSql unit test database with an in memory H2
Database. I couldn't figure out why couple tests are failing, it was
working fine with Postgre. There are approx. 280+ unit tests in this
application. The failing dao tests are selecting from a view and the
entity has a EmbeddedId, those the columns of that view.
See below code (Note: I changed names, code and the sql to hide the
real names when writing this email, but this was a working unit test
with Postgre db)
@Table(name = "item_view") // <- item_view is a database view
public class ItemV implements Serializable
{
.....
@EmbeddedId // <- entity has an embedded id
private ItemVId id;
.....
@Embeddable
public static class ItemVId implements Serializable //<- This is the
embeddedId
{
@Column(name = "item_id", updatable=false, insertable=false)
private Long ItemId; //<- col no.1 of view
@Column(name = "item_type_id", updatable=false,
insertable=false)
private Integer ItemTypeId; //<- col no.2 of view
.....
ItemType is an enum
And the view is
CREATE OR REPLACE VIEW item_view AS
( ( ( SELECT pt.id as item_id, cit.id as
item_type_id
FROM xyz pt, item_type cit
WHERE pt.name::text = 'xyz'::text
UNION
SELECT z.id as item_id, cit.id as
item_type_id
FROM zzz z, item_type cit
WHERE z.name::text = 'zzz'::text)
..............
and the dao method is
public ItemView find(Long itemId, ItemType itemType)
{
String hql = " from ItemV iv where iv.id.itemId = :itemId and
iv.id.itemTypeId = :itemTypeId ");
List<ItemView> result = (List<ItemView>)getEntityManager()
.createQuery(hql)
.setParameter("itemId", itemId)
.setParameter("itemTypeId", itemType.getId())
.setMaxResults(1)
.getResultList();
return result.isEmpty()
? null : result.get(0);
}
This dao method always returns empty results, never finding existing
rows in the view??? I know those rows exist because when I do getAll()
on the same dao I see results and I see a matching row for the
criteria.
Is there anything special about selecting rows from a view in H2
database?
Thanks
--
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/h2-database?hl=en.