Search code examples
javahibernatejpadbunit

Hibernate : Sorting ManyToMany mapping


Consider the following mapping with JPA annotations

@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(name = "infotype_validations", 
    joinColumns = { @JoinColumn(name = "info_type_id") }, 
    inverseJoinColumns = { @JoinColumn(name = "validation_id") }
)
@OrderBy(value="validation_id desc")
public Set<Validation> getValidation() {
    return validation;
}

My intention is to have a jointable in the database and each time the getValidation() is called in my services the records get returned ordered by validation_id. Now to test my functionality I make use of DbUnit. Each time I start a testclass my database gets created and hibernate creates my tables afterwhich DbUnit fills them with data. When I comment @OrderBy my tests pass but when I uncomment it, I get table infotype_validations can't be found. I've looked at the available documentation online and it seems it is perfectly possible to have @OrderBy in this kind of mapping. So what am I missing ?


Solution

  • You need to use the field name not the column name.

    //Assuming the field is validationId
    @OrderBy(value="validationId desc")
    public Set<Validation> getValidation() {
        return validation;
    }
    

    Also make sure that the infotype_validations table exists within your database and the spelling matches.