在过去的两周里,我一直在学习Hibernate,但是我现在遇到了一个我无法解决的问题,我也不幸运地在网上找到了解决方案。
我的问题:我希望一条消息有多个响应。我有@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
,@JoinColumn(name = "message_id")
,@OrderBy("response_date DESC")
作为响应列表。response_date
属于响应表。每当我试图查找消息时,我总是看到以下错误:
[错误] [2023年3月13日上午3:14:28:025|GMT-05:00] [主要] [组织.休眠.引擎.jdbc.spi.SqlExceptionHelper.日志异常(SqlExceptionHelper.java:142)] -“订单子句”中存在未知列“消息su0_.response_date”
线程“main”中出现异常。组织休眠。异常。SQLGrammarException:无法提取结果集...
我从数据库中删除了表,然后注解掉了@OrderBy("response_date DESC")
注解,添加了一条消息,然后尝试再次查找该消息,但仍然显示相同的错误。
我到底做错了什么?
消息.java
@Entity
@Table(name = "Message")
public class Message implements Serializable {
@Transient
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "message_id", nullable = false)
private int messageID;
@Column(name = "type", nullable = false, length = 50)
private String messageType;
@Column(name = "category", nullable = false, length = 100)
private String category;
@Column(name = "details", nullable = false, length = 2000)
private String details;
@Column(name = "date_created", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date dateCreated;
@Column(name = "resolved")
private boolean resolved;
@OneToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "student_id")
private Student student;
@OneToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "staff_id")
private Staff staff;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "message_id")
@OrderBy("response_date DESC") // commented out here and still seeing the same error about unknown column
private List<Response> responses;
// Constructors
// Getters & Setters
}
响应.java
@Entity
@Table(name = "Response")
public class Response implements Serializable {
@Transient
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "response_id", nullable = false)
private int responseID;
@Column(name = "content", nullable = false, length = 2000)
private String content;
@Column(name = "response_date", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date responseDate;
@OneToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "message_id")
private Message message;
// Constructors
// Getters & Setters
}
我用来做参考的是我的另一门课...
员工.java
@Entity
@Table(name = "Staff")
@PrimaryKeyJoinColumn(name = "user_id")
public class Staff extends User implements Serializable {
@Transient
private static final long serialVersionUID = 1L;
@Column(name = "role", length = 255)
private String role;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "staff_id")
@OrderBy("date_created DESC") // order by date_created which is from the message table and it worked
private List<Message> assignedMessages;
// Constructors
// Getters & Setters
}
在向消息类添加响应列表之前,一切都很正常,当我查找一个工作人员时,我会返回由该工作人员发送的消息列表。
我哪里做错了?任何帮助或解释都将不胜感激。
1条答案
按热度按时间xxls0lw81#
在
@OrderBy
注解中,应使用实体字段而不是数据库列名: