【发布时间】:2021-06-25 09:19:35
【问题描述】:
我有两个表,我想通过连接将这两个表合并为一个,但是我无法在 user id 和 app_user_id 中显示 id 等所有列。
我正在创建一个新实体,这两个表的连接产生了结果:
@Entity
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class UserDetails implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String id;
@Column(name = "mobile_number")
private Long mobileNumber;
// private String userId;
@Column(name = "app_user")
private Long appUserId;
public Long getApp_user_id() {
return appUserId;
}
public void setApp_user_id(Long app_user_id) {
this.appUserId = app_user_id;
}
@NotNull
@Pattern(regexp = Constants.LOGIN_REGEX)
@Size(min = 1, max = 50)
@Column(length = 50, unique = true, nullable = false)
private String login;
@Size(max = 50)
@Column(name = "first_name", length = 50)
private String firstName;
@Size(max = 50)
@Column(name = "last_name", length = 50)
private String lastName;
@Email
@Size(min = 5, max = 254)
@Column(length = 254, unique = true)
private String email;
@NotNull
@Column(nullable = false)
private boolean activated = false;
@Size(min = 2, max = 10)
@Column(name = "lang_key", length = 10)
private String langKey;
@Size(max = 256)
@Column(name = "image_url", length = 256)
private String imageUrl;
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Long getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(Long mobileNumber) {
this.mobileNumber = mobileNumber;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean isActivated() {
return activated;
}
public void setActivated(boolean activated) {
this.activated = activated;
}
public String getLangKey() {
return langKey;
}
public void setLangKey(String langKey) {
this.langKey = langKey;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserDetails that = (UserDetails) o;
return activated == that.activated &&
Objects.equals(id, that.id) &&
Objects.equals(mobileNumber, that.mobileNumber) &&
Objects.equals(login, that.login) &&
Objects.equals(firstName, that.firstName) &&
Objects.equals(lastName, that.lastName) &&
Objects.equals(email, that.email) &&
Objects.equals(langKey, that.langKey) &&
Objects.equals(imageUrl, that.imageUrl);
}
@Override
public int hashCode() {
return Objects.hash(id, mobileNumber, login, firstName, lastName, email, activated, langKey, imageUrl);
}
@Override
public String toString() {
return "UserDetails{" +
"id=" + id +
", mobileNumber=" + mobileNumber +
", login='" + login + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", activated=" + activated +
", langKey='" + langKey + '\'' +
", imageUrl='" + imageUrl + '\'' +
'}';
}
}
我为新实体创建了一个新的存储库:
@Repository
public interface UserDetailsRepository extends JpaRepository<UserDetails,Long> {
@Query(value = "SELECT a.id as app_user, u.id, u.login as login ,u.first_name ,u.last_name ,u.email ,u.activated ,u.lang_Key ,u.image_Url ,a.mobile_number FROM jhi_user AS u join app_user AS a WHERE a.user_id=u.id ", nativeQuery = true)
List<UserDetails> joinTable();
}
在这个存储库中,我编写了一个在 SQL 命令中运行的查询,但在春季我得到了不同的结果。
这是我春天的结果:
UserDetails{id=0ea8e6e2-fb1f-43e8-aee1-b14ea2b9ef17, mobileNumber=780344248, login='semon@test.com', firstName='semon', lastName='hyari', email='semon@test.com', activated=true, langKey='en', imageUrl='null'}
我想显示这个结果
UserDetails{id=0ea8e6e2-fb1f-43e8-aee1-b14ea2b9ef17,**userId="value"**, mobileNumber=780344248, login='semon@test.com', firstName='semon', lastName='hyari', email='semon@test.com', activated=true, langKey='en', imageUrl='null'}
【问题讨论】:
-
我认为这是因为你缺少 getter/setter 是实体类。
标签: spring-boot hibernate spring-mvc spring-data-jpa spring-data