Hibernate xml mapping list ordered by Date

I have two DAO’s. First uses annotations to map entities, second is hibernate using xml. I have two entities.
News.java:

@Entity
@Table(name = "NEWS")
public class News implements Serializable{
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 883279937885116359L;
    /**
     * News id
     */
    @Id
    @GeneratedValue(generator = "news_seq")
    @SequenceGenerator(name="news_seq", sequenceName="NEWS_SEQ",allocationSize=1)
    @Column(name = "NEWS_ID", nullable = false, unique = true)
    private Long id;
    /**
     * News short text
     */
    @Column(name = "SHORT_TEXT")
    private String shortText;
    /**
     * News full text
     */
    @Column(name = "FULL_TEXT")
    private String fullText;
    /**
     * News title
     */
    @Column(name = "TITLE")
    private String title;
    /**
     * News creation date
     */
    @Column(name = "CREATION_DATE")
    private Date creationDate;
    /**
     * News modification date
     */
    @Column(name = "MODIFICATION_DATE")
    private Date modificationDate;
    /**
     * News author
     */
    @ManyToOne(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER)
    @JoinTable(
            name = "NEWS_AUTHOR",
            joinColumns = {@JoinColumn(name = "NEWS_ID")},
            inverseJoinColumns =  {@JoinColumn(name = "AUTHOR_ID")}
    )
    private Author author;
    /**
     * News comments
     */
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval= true)
    @JoinColumn(name = "NEWS_ID")
    @OrderBy("creationDate DESC")
    private List<Comment> comments;

Comment.java:

@Entity
@Table(name = "COMMENTS")
public class Comment implements Serializable{
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -5697896094322498108L;
    /**
     * Comment id
     */
    @Id
    @GeneratedValue(generator = "comment_seq")
    @SequenceGenerator(name="comment_seq", sequenceName="COMMENTS_SEQ",allocationSize=1)
    @Column(name = "COMMENT_ID", nullable = false, unique = true)
    private Long id;
    /**
     * Comment text
     */
    @Column(name = "COMMENT_TEXT")
    private String commentText;
    /**
     * Comment creation date
     */
    @Column(name = "CREATION_DATE")
    private Date creationDate;
    /**
     * Id of the news which the comment is added to
     */
    @ManyToOne(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER)
    @JoinColumn(name = "NEWS_ID")
    private News news;

And xml mapping.

<class name="news.entity.News" table="NEWS">
    <id name="id" type="java.lang.Long">
        <column name="NEWS_ID"/>
        <generator class="sequence">
            <param name="sequence">NEWS_SEQ</param>
        </generator>
    </id>
        <property name="title" column="TITLE" />
        <property name="shortText" column="SHORT_TEXT" />
        <property name="fullText" column="FULL_TEXT" />
        <property name="creationDate" type="java.util.Date" column="CREATION_DATE" />
        <property name="modificationDate" type="java.util.Date" column="MODIFICATION_DATE" />

        <list name="comments" access="field" cascade="all" >
            <key column="news_id" not-null="true" />
            <index column="comment_id" />
            <one-to-many class="com.epam.news.entity.Comment" />

        </list>

        <join table="NEWS_AUTHOR" inverse="true">
            <key column="NEWS_ID"/>
            <many-to-one name="author" column="AUTHOR_ID" not-null="true"/>
        </join>
    </class>

<class name="news.entity.Comment" table="COMMENTS">
        <id name="id" type="java.lang.Long">
            <column name="COMMENT_ID"/>
            <generator class="sequence">
                <param name="sequence">COMMENT_SEQ</param>
            </generator>
        </id>
        <property name="commentText" column="COMMENT_TEXT" />
        <property name="creationDate" column="CREATION_DATE" />
        <many-to-one name="news" fetch="select" column="news_id" cascade="all" insert="false" update="false"/>

    </class>

But I have much null elements in news.comments(I know why) and they ordered by comment_id. Can I have list of comments with out null elements and ordered by comment CREATION_DATE? I have to use just xml.


Source: xml

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.