Reputation: 310
Hibernate criteria.list() returns two entries instead of one. In the database is only one entry for TaxTable with 2 corresponding TaxEntries. I think there is something wrong with the joins I do. Here is my code: TaxTable:
@Entity
@Table(name = "tax_table")
public class TaxTable implements Serializable {
@Id @GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy = "taxTable",cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<TaxEntry> entries;
...
TaxEntry:
@Entity
@Table(name = "tax_entry")
public class TaxEntry implements Serializable {
@Id @GeneratedValue
private Long id;
private String name;
private BigDecimal percentage;
@ManyToOne
@JoinColumn(name = "account_id")
private Account toAccount;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "taxtable_id")
private TaxTable taxTable;
...
To fetch the result I use following code:
@SuppressWarnings("unchecked")
@Transactional
public List<TaxTable> findAll() {
Session session = sessionFactory.getCurrentSession();
Criteria taxTableCriteria = session.createCriteria(TaxTable.class);
taxTableCriteria.addOrder(Order.asc("name"));
return taxTableCriteria.list();
}
In my test I create a TaxTable with 2 taxEntries:
...
taxEntryVAT.setTaxTable(taxTable);
taxEntryWT.setTaxTable(taxTable);
taxEntries.add(taxEntryVAT);
taxEntries.add(taxEntryWT);
taxTable.setEntries(taxEntries);
taxTableDao.create(taxTable);
And the fetch will return a list of 2 taxTables with the same id instead of one:
List<TaxTable> result = taxTableDao.findAll();
Any ideas?
Thanks
Upvotes: 0
Views: 84
Reputation: 2691
use : criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
Upvotes: 1