按日期排序的Hibernate xml映射列表

时间:2015-06-17 08:37:08

标签: java xml hibernate

我有两个DAO。首先使用注释来映射实体,其次是使用xml进行休眠。我有两个实体。 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;

和xml映射。

<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>

但我在news.comments中有很多null元素(我知道为什么),并且他们通过comment_id排序。我可以使用null元素的注释列表,并通过CREATION_DATE注释排序吗?我只需要使用xml。

1 个答案:

答案 0 :(得分:0)

看起来您可以使用whereorder-by映射元素,如documentation中所述。

您可以使用where过滤掉空值,使用order-by进行排序。