Hibernate : Sorting ManyToMany mapping

天涯浪子 提交于 2019-12-23 18:13:22

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/14294712/hibernate-sorting-manytomany-mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!