Hi,
I have an entity class that has a String collection. I want that collection to be ordered, so I used the @OrderBy annotation (from JPA).
The problem is that the annotation seems to be ignored, no matter what value I give to the annotation (even invalid ones):
- @OrderBy
- @OrderBy("ASC")
- @OrderBy("DESC")
- @OrderBy("some invalid order by clause")
Here is the entity class:
Code:
@Entity
@Table(name = "document_types")
public class DocumentType extends AbstractEntity {
...
private Set<String> allowedAttachmentTypes = Sets.newLinkedHashSet();
....
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(
name = "allowed_attachment_types",
joinColumns = {@JoinColumn(name = "document_type_id")}
)
@OrderBy
@Column(name = "extension")
public Set<String> getAllowedAttachmentTypes() {
return allowedAttachmentTypes;
}
...
}
Here is the DDL for the collection table:
Code:
CREATE TABLE allowed_attachment_types (
document_type_id INT NOT NULL,
extension VARCHAR(32) NOT NULL,
PRIMARY KEY (document_type_id, extension),
FOREIGN KEY (document_type_id) REFERENCES document_types (id)
) ENGINE = InnoDB;
When an entity is fetched, the following SQL is ran:
Hibernate: select documentty0_.id as id2_, documentty0_.name as name2_ from document_types documentty0_ where documentty0_.id=?
Hibernate: select allowedatt0_.document_type_id as document1_2_0_, allowedatt0_.extension as extension0_ from allowed_attachment_types allowedatt0_ where allowedatt0_.document_type_id=?
As you can see, the SELECT for retrieving the allowedAttachmentTypes does not have an ORDER BY clause.
I could use a SortedSet or whatever, but I should still be able to use the annotation.
The JPA JavaDocs say:
Quote:
...
The OrderBy annotation may be applied to an element collection. When OrderBy is applied to an element collection of basic type, the ordering will be by value of the basic objects and the property or field name is not used.
...
Is this a bug or am I doing something wrong?
Hibernate version: 3.5.6
Database: MySQL