gpt4 book ai didi

java - Hibernate xml 映射列表按日期排序

转载 作者:太空宇宙 更新时间:2023-11-04 13:45:31 24 4
gpt4 key购买 nike

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

评论.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 排序。我可以拥有不包含空元素并按评论 CREATION_DATE 排序的评论列表吗?我必须只使用 xml。

最佳答案

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

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

关于java - Hibernate xml 映射列表按日期排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30886374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com