我创建了一个带有实体的JHipster Monolithic应用程序:profile,follow& voyage导致以下架构:
在我的网络Angular应用程序中,我想显示特定用户关注的用户的航程列表。以下SQL查询将为我提供所需的结果集:
SELECT PROFILE.HANDLE, VOYAGE.NAME
FROM PROFILE, VOYAGE, FOLLOWING
WHERE FOLLOWING.USER_ID = 4
AND VOYAGE.USER_ID = PROFILE.USER_ID
AND PROFILE.USER_ID = FOLLOWING.FOLLOWING_ID
在我的JHipster应用程序中创建返回此结果集的REST端点需要哪些步骤?
我是否需要执行以下操作?
我是Spring JPA的新手,并且一直在努力实现它。
===更新===
以下是我项目中的实体类:
'简介'实体:
/**
* A Profile.
*/
@Entity
@Table(name = "profile")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Profile implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
@NotNull
@Column(name = "handle", nullable = false)
private String handle;
@Column(name = "bio")
private String bio;
@OneToOne
@JoinColumn(unique = true)
private User user;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getHandle() {
return handle;
}
public Profile handle(String handle) {
this.handle = handle;
return this;
}
public void setHandle(String handle) {
this.handle = handle;
}
public String getBio() {
return bio;
}
public Profile bio(String bio) {
this.bio = bio;
return this;
}
public void setBio(String bio) {
this.bio = bio;
}
public User getUser() {
return user;
}
public Profile user(User user) {
this.user = user;
return this;
}
public void setUser(User user) {
this.user = user;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Profile profile = (Profile) o;
if (profile.id == null || id == null) {
return false;
}
return Objects.equals(id, profile.id);
}
@Override
public int hashCode() {
return Objects.hashCode(id);
}
@Override
public String toString() {
return "Profile{" +
"id=" + id +
", handle='" + handle + "'" +
", bio='" + bio + "'" +
'}';
}
}
'关注'实体:
/ ** *以下。 * / @实体 @Table(姓名="以下") @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public class以下实现Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
@OneToOne
@JoinColumn(unique = true)
private User following;
@ManyToOne
private User user;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public User getFollowing() {
return following;
}
public Following following(User user) {
this.following = user;
return this;
}
public void setFollowing(User user) {
this.following = user;
}
public User getUser() {
return user;
}
public Following user(User user) {
this.user = user;
return this;
}
public void setUser(User user) {
this.user = user;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Following following = (Following) o;
if (following.id == null || id == null) {
return false;
}
return Objects.equals(id, following.id);
}
@Override
public int hashCode() {
return Objects.hashCode(id);
}
@Override
public String toString() {
return "Following{" +
"id=" + id +
'}';
}
}
' Voyage'实体:
/**
* A Voyage.
*/
@Entity
@Table(name = "voyage")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Voyage implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
@NotNull
@Column(name = "name", nullable = false)
private String name;
@NotNull
@Column(name = "start_date", nullable = false)
private ZonedDateTime startDate;
@Column(name = "distance")
private Integer distance;
@ManyToOne
private User user;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public Voyage name(String name) {
this.name = name;
return this;
}
public void setName(String name) {
this.name = name;
}
public ZonedDateTime getStartDate() {
return startDate;
}
public Voyage startDate(ZonedDateTime startDate) {
this.startDate = startDate;
return this;
}
public void setStartDate(ZonedDateTime startDate) {
this.startDate = startDate;
}
public Integer getDistance() {
return distance;
}
public Voyage distance(Integer distance) {
this.distance = distance;
return this;
}
public void setDistance(Integer distance) {
this.distance = distance;
}
public User getUser() {
return user;
}
public Voyage user(User user) {
this.user = user;
return this;
}
public void setUser(User user) {
this.user = user;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Voyage voyage = (Voyage) o;
if (voyage.id == null || id == null) {
return false;
}
return Objects.equals(id, voyage.id);
}
@Override
public int hashCode() {
return Objects.hashCode(id);
}
@Override
public String toString() {
return "Voyage{" +
"id=" + id +
", name='" + name + "'" +
", startDate='" + startDate + "'" +
", distance='" + distance + "'" +
'}';
}
}
答案 0 :(得分:1)
按照评论中的建议修改架构后,您需要添加2个查询:
1.VoyageRepository:
List<Voyage> findByProfileIn(Collection<Profile> profiles);
2.ProfileRepository
List<Profile> findByFollowingID(long Id);
然后在您的Voyage服务中结合这两个查询:
return voyageRepository.findByProfileIn(profileRepository.findbyFollowingId(id));
然后在你的VoyageRessource中公开它。
答案 1 :(得分:0)
我的工作是:
1.修改架构,使“配置文件”具有一对多 与'跟随'和'的关系'航程',即删除了 JHI_USER&amp;的关系'跟随'和'航程'
2.在VoyageRepository
中定义以下查询public interface VoyageRepository extends JpaRepository<Voyage,Long> {
@Query("select v.name, v.profile.handle from Voyage v, Following f where f.profile.user.id = 4 and f.following.id = v.profile.id")
List<Voyage> getFeed();
}
我已经在上面的代码中硬编码了id值(4),因为我还没有弄清楚如何将参数传递给查询。 JPA是精神上的弯曲!