我正在使用Sping Boot ,Java,Hibernate和MySQL来创建消息传递应用程序。我的SQL语句在MySQL Workbench中运行得很好,但在我的应用程序中却不起作用。在进行SQL调用时,我收到错误:White Label Error Page
以下是MySQL工作台中的两个类及其关系(为简洁起见,删除了构造函数、Getter和Setter):
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotEmpty(message = "Username is required.")
@Size(min = 3, max = 25, message = "Username must be between 8 and 25 characters long.")
private String userName;
@NotEmpty(message = "Email is required.")
@Email(message = "Please enter a valid email.")
private String email;
@NotEmpty(message = "Password is required.")
@Size(min = 8, max = 128, message = "Password must be between 8 and 128 characters.")
private String password;
@NotEmpty(message = "Confirm password is required.")
@Size(min = 8, max = 128, message = "Password must be between 8 and 128 characters.")
private String confirmPassword;
@Size(min = 0, max = 225, message = "Bio must less than 255 characters.")
private String bio = "";
@Lob
private Blob avi;
@Lob
private Blob header;
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private List < PublicMessage > publicMessages;
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private List < PrivateMessage > privateMessages;
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private List < Friend > friends;
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private List < Reply > replies;
@Column(updatable = false)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date createdAt;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date updatedAt;
}
@Entity
@Table(name = "privateMessages")
public class PrivateMessage {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Size(min = 1, max = 255, message = "Body must be between 1 and 255 characters.")
private String body;
private Long recipientId;
private String recipientUserName;
@ManyToOne(fetch = FetchType.LAZY)
@JoinTable(name = "user_privateMessages")
private User user;
@Column(updatable = false)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date createdAt;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date updatedAt;
}
字符串
来自PrivateMessageRepository的SQL语句:
@Query(value = "SELECT * FROM MessagingApp.private_messages LEFT JOIN MessagingApp.user_private_messages ON MessagingApp.private_messages.id = MessagingApp.user_private_messages.id WHERE user_id = ?1 AND recipient_id = ?2 ORDER BY MessagingApp.private_messages.created_at DESC;", nativeQuery = true)
public List<PrivateMessage> retrieveAllBetweenTwoUsers(Long senderId, Long receiverId);
型
MySQL Workbench ERR Diagram的
我很熟悉Hibernate,重复的列名会导致这个错误,解决方法是在SQL语句中使用别名&“AS”子句。我已经尝试过使用private_messages.id和user_private_messages.id单独和一起使用,但都无济于事。
我被难倒了,因为只有两列名为“id”,分别是PrivateMessages.id(主键)和连接表(user_private_messages)中的“id”列,它将用户链接到他们的私人消息。任何和所有的帮助是非常感谢!
1条答案
按热度按时间jgzswidk1#
该问题是由于id列同时存在于private_messages表和user_private_messages表中,导致Hibernate尝试将结果集Map到实体模型时发生冲突。
通过为每个表提供唯一的别名,可以避免冲突,并解决自动发现本机SQL查询期间的“重复SQL别名”错误。
下面的示例说明了如何修改查询以解决重复SQL别名问题:
字符串