views:

1345

answers:

1

I've got classes with mappings like this:

@Entity
public class CurrencyTable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    @Version
    @Column(nullable=false)
    private Timestamp version;

    @Column(length=32, unique=true)
    private String refCode;

   @OneToMany(mappedBy="currencyTable", fetch=FetchType.LAZY, cascade =  {CascadeType.ALL})
   @MapKey(name="currency")
   private Map<String, CurrencyTableRate> rateMap = new HashMap<String, CurrencyTableRate>();
}

@Entity
public class CurrencyTableRate{
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    @Version
    @Column(nullable=false)
    private Timestamp version;

    @Column(length=3)
    private String currency;

    @Basic
    private BigDecimal rateValue;

    @ManyToOne(optional=false,fetch=FetchType.LAZY)
    private CurrencyTable currencyTable;
}

There is one row in CurrencyTable and three rows in CurrencyTableRate referring to the CurrencyTable in database.

When I load CurrencyTable using HQL:

from CurrencyTable where refCode = :refCode

I get an entity with three entries in rateMap, but if I try this:

from CurrencyTable table left outer join fetch table.rateMap where refCode = :refCode

there is only one entry in rateMap.

I looked on query generated by Hibernate and ran it manually - it returned three rows, as expected, so it seems to be a problem with mapping them after fetching. Has anyone encuntered such a problem? I use Hibernate version 3.2.6.ga and Oracle 10g

A: 

First of all I recommend you to add an alias to refCode. I don't think it will have an impact on the result, but just in case.

from CurrencyTable table left outer join fetch table.rateMap where table.refCode = :refCode

Second, turn on your SQL code and analyse what' really going on at SQL level. I had similar issues with HQL in such cases

from CurrencyTable table left outer join fetch table.rateMap map where map.id = :id

which I have to rewrite to

from CurrencyTable table left outer join fetch table.rateMap map where EXISTS (SELECT a.id from CurrencyTable table a INNER JOIN a.rateMap m WHERE m.id = :id and table.id=a.id)

Hope my hints will help.

FoxyBOA
I added aliases and it didn't have impact. As I wrote in question I looked on SQL query generated by Hibernate and ran it manually - it returned three rows, as expected. I used a workaround with running first query (without left outer join fetch) and then calling Hibernate.initialize on the map. As it's in my work I have no time to debug Hibernate to find out what's going on but such behaviour upsets me :-)
Tadeusz Kopec