开发者

Selecting from a view in H2 doesn't work

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)

<pre>

@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 开发者_如何学Goselecting rows from a view in H2 database? Thanks


Ok fixed, I had to use a smaller number for LOCK_TIMEOUT value, so now I can connect to database and see values. Selecting from view problem also fixed.

I have to say H2 is really neat and elegant. I'm glad, I switched the unit test db to H2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜