您好我有以下Entity with EmbeddedId id(我使用lombok作为getter,setter和构造函数):
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
public class SignUpIdentity implements Serializable {
@NotNull
@Getter @Setter
@JoinColumn(name="training")
@ManyToOne
private Training training;
@NotNull
@Getter @Setter
@JoinColumn(name="user")
@ManyToOne
private User user;
}
@Entity
public class SignUp{
@EmbeddedId
@Getter
@Setter
private SignUpIdentity signUpIdentity;
@Getter
private Date date;
public SignUp () {
Calendar cal = Calendar.getInstance();
this.date = cal.getTime();
}
}
我还有一个Paging and Sorting存储库和一个控制器:
public interface SignUpRepository extends PagingAndSortingRepository<SignUp, SignUpIdentity> {
}
@Controller
@RequestMapping(path="/signUps")
public class SignUpController {
@Autowired
SignUpRepository signUpRepository;
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody SignUp createOrUpdate(@RequestBody SignUp titemBody) {
return signUpRepository.save(titemBody);
}
@RequestMapping(path="/find", method = RequestMethod.POST)
public SignUp readById(@RequestBody SignUpIdentity id) {
return signUpRepository.findOne(id);
}
}
问题是,我发送POST请求到/ signUps /发现它不会返回任何结果,即使它应该(我已添加注册培训:2和用户:“1234”)。这是我正在提出的要求的主体:
{
"training": {"id": 2},
"user": {"phoneNumber": "1234"}
}
这是回复:
{
"timestamp": 1518693385312,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/signUps/find"
}
我缺少什么?
这些是用户和培训实体:
@Entity
public class Training {
@Id
@Getter
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
@NotNull
@Getter @Setter
private String name;
@NotNull
@Getter
private Date dateOfInsert;
@OneToMany(mappedBy="signUpIdentity.training")
@JsonIgnore
@Getter @Setter
private Set<SignUp> signUp;
public Training() {
Calendar cal = Calendar.getInstance();
dateOfInsert = cal.getTime();
}
public Training(long id) {
Calendar cal = Calendar.getInstance();
dateOfInsert = cal.getTime();
this.id = id;
}
}
@Entity
public class User {
@Id
@Getter @Setter
private String phoneNumber;
@NotNull
@Getter @Setter
private String nameAndSurname;
@OneToMany(mappedBy="signUpIdentity.user")
@Getter @Setter
@JsonIgnore
private Set<SignUp> signUp;
public User() {
Calendar cal = Calendar.getInstance();
this.date = cal.getTime();
}
}
我甚至在存储库中找到了我的遗嘱,但它仍然没有给我任何回报:
@Query("select u from SignUp u where u.signUpIdentity.training.id = :trainingId and u.signUpIdentity.user.phoneNumber = :phoneNumber")
SignUp findByTrainingAndUser(@Param("trainingId") long trainingId,
@Param("phoneNumber") String phoneNumber);
@RequestMapping(path="/find", method = RequestMethod.POST)
public SignUp readById(@RequestBody SignUpIdentity id) {
return signUpRepository.findByTrainingAndUser(id.getTraining().getId(), id.getUser().getPhoneNumber());
}
答案 0 :(得分:1)
我认为在您的请求中发送ID会更简单,更容易
{ &#34; id&#34;:2 }
并在控制器中更新您的方法以使用ID
进行搜索答案 1 :(得分:0)
尝试更改SignUpRepository
以在通用界面中使用Long类型。
public interface SignUpRepository extends PagingAndSortingRepository<SignUp,
Long> {
}