我没有找到任何解决方案来管理失败,所以我决定创建一个新的问题。我有一个简单的类JPA生成与JSON失败
@Entity
public class Reservation {
// private Integer RESERVATION_ID;
// private Integer id;
private long code;
private Date date;
private Client reservationClient;
private WashType reservationWashType;
private Vehicle reservationVehicle;
private Wash reservationWash;
private Worker reservationWorkerPesel;
private Review reservationReview;
private ReservationReminder reservationReminder;
}
当我运行这样的查询:
@Query("SELECT r FROM Reservation r JOIN FETCH r.reservationReminder where r.reservationWorkerPesel = :worker")
List<Reservation> findByReservationWorkerPesel(@Param("worker") Worker worker);
,起初我一切看起来不错,但后来我做这样的一些操作:
public List<ReservationReminder> findByReservationWorkerPesel(Worker worker) {
List<ReservationReminder> reservationReminderList = new ArrayList<>();
List<Reservation> byReservationWorkerPesel = reservationDao.findByReservationWorkerPesel(worker);
for (Reservation r : byReservationWorkerPesel) {
if (r.getReservationReminder() != null && r.getReservationReminder().getChecked() == false)
reservationReminderList.add(r.getReservationReminder());
}
return reservationReminderList;
}
然后当我看到JSON是怎么样的时候 - 这很奇怪,因为:
[{“reservationReminderId”:2,“reservation”:{“code”:263022,“date”:1487851200000,“reservationClient”:{“clientPesel”:“91122619197”,“name”:“Client 1” “:”Client 1“,”email“:”[email protected]“,”phone“:”234567890“,”accountNumber“:”34567897654345678987654356“,”clientUser“:{”userId“:3,”login“ “客户端”, “passwordHash”: “$ 2A $ 10 $ 0jJMMzeh2CTRagk3hwRSlurx.mxLgR1aAUQOYBD9QFqbISeoTSVN。”, “的UserRole”:{ “角色ID”:3 “名称”: “客户端”, “用户”:[{ “用户id”:8, “登录”: “clien5”, “passwordHash”: “$ 2A $ 10 $ 6WrmwpwOdhv6UXBo2mYq8ucKiQTwvIwTHw23myo6.oujflh8pqKR。”, “的UserRole”:{ “角色ID”:3 “名称”: “客户端”, “用户”:[{“用户id “:8,” 登录 “:” clien5" , “passwordHash”: “$ 2A $ 10 $ 6WrmwpwOdhv6UXBo2mYq8ucKiQTwvIwTHw23myo6.oujflh8pqKR”, “的UserRole”:{ “角色ID”:3 “名称”: “客户端”, “用户”: [{ “用户id”:8中, “登录”: “clien5”, “passwordHash”: “$ 2A $ 10 $ 6WrmwpwOdhv6UXBo2mYq8ucKiQTwvIwTHw23myo6.oujflh8pqKR。”, “的UserRole”:{ “角色ID”:3 “名称”: “客户端”, “用户”:[{ “用户id”:8中, “登录”: “clien5”, “passwordHash”:“$ 2A $ 10 $ 6WrmwpwOdhv6UXBo2mYq8ucKiQTwvIwTHw23my o6.oujflh8pqKR “” 的UserRole。 “:{” 角色ID “:3”,名称 “:” 客户端”, “用户”:
....
[{ “用户id” :8中, “登录”: “clien5”, “passwordHash”: “$ 2A $ 10 $ 6WrmwpwOdhv6UXBo2mYq8ucKiQTwvIwTHw23myo6.oujflh8pqKR”, “的UserRole”:{ “角色ID”:3 “名称”: “客户端”, “用户”:[ { “用户id”:8中, “登录”: “clien5”, “passwordHash”: “$ 2A $ 10 $ 6WrmwpwOdhv6UXBo2mYq8ucKiQTwvIwTHw23myo6.oujflh8pqKR”, “的UserRole”:{ “角色ID”:3 “名称”: “客户端”,”用户 “:[{” 用户id “:8中,” 登录 “:” clien5" , “passwordHash”: “$ 2A $ 10 $ 6WrmwpwOdhv6UXBo2mYq8ucKiQTwvIwTHw23myo6.oujflh8pqKR”, “的UserRole。”:{ “角色ID”:3 “名称”:”客户端 “ ”用户“:[{ ”用户id“:8, ”登录“: ”clien5“, ”passwordHash“:{ ”时间戳“:1489015140465, ”状态“:200, ”错误“: ”OK“,” excep “:”org.springframework.http.converter.HttpMessageNotWritableException“,”message“:”无法写入内容:无限递归(StackOverflowError)(通过引用链:com.carwash.domains.User [\“userRole \”] > com.carwash.domains.Role [\ “用户\”] - > org.hibernate.collection.internal.PersistentBag [0] - > com.carwash.domains.User [\ “的UserRole \”] - > com.carwash .domains.Role [\ “用户\”] - > org.hibernate.collection.internal.PersistentBag [0] - > com.carwash.domains.User [\ “的UserRole \”] - > com.carwash.domains.Role [\ “用户\”] - > org.hibernate.collection.internal.PersistentBag [0] - > com.carwash.domains.User [\ “的UserRole \”] - > com.carwash.domains.Role [\“用户\ “] - > org.hibernate.collection.internal.PersistentBag [0] - > com.carwash.domains.User [\” 的UserRole \ “] - > com.carwash.domains.Role [\” 用户\“] - > org.hibernate.collection.internal.PersistentBag [0] - > com.carwash.domains.User [\ “的UserRole \”] - > com.carwash.domains.Role [\ “用户\”] - > org.hibernate作为.collection.internal.PersistentBag [0] - > com.carwash.domains.User [\ “的UserRole \”] - > com.carwa sh.domains.Role [\ “用户\”] - > org.hibernate.collection.internal.PersistentBag [0] - > com.carwash.domains.User [\ “的UserRole \”] - > com.carwash.domains。作用[\ “用户\”] - > org.hibernate.collection.internal.PersistentBag [0] - > com.carwash.domains.User [\ “的UserRole \”] - > com.carwash.domains.Role [\”用户\ “] - > org.hibernate.collection.internal.PersistentBag [0] - > com.carwash.domains.User [\” 的UserRole \ “] - > com.carwash.domains.Role [\” 用户\“] -
...
\“] - >组织。hibernate.collection.internal.PersistentBag [0] - > com.carwash.domains.User [\ “的UserRole \”] - > com.carwash.domains.Role [\ “用户\”] - > org.hibernate.collection。 internal.PersistentBag [0] - > com.carwash.domains.User [\ “的UserRole \”] - > com.carwash.domains.Role [\ “用户\”] - > org.hibernate.collection.internal.PersistentBag [ 0] - > com.carwash.domains.User [\ “的UserRole \”] - > com.carwash.domains.Role [\ “用户\”] - > org.hibernate.collection.internal.PersistentBag [0] - > com.carwash.domains.User [\ “的UserRole \”] - > com.carwash.domains.Role [\ “用户\”] - > org.hibernate.collection.internal.PersistentBag [0] - > com.carwash。 domains.User [\ “的UserRole \”] - > com.carwash.domains.Role [\ “用户\”] - > org.hibernate.collection.internal.PersistentBag [0] - > com.carwash.domains.User [ \“userRole \”])“,”path“:”/ api/reservationreminder“}
我在做什么错?
也许可以说你的东西 - 我不知道为什么一个GET方法后(仅获得),我得到了一些这些错误?
我刚刚意识到问题出在哪里,但我不能让如何使用'@ JsonBackReference'和'@ JsonManagedReference'?应在哪个网站上放置'@ JsonBackReference'和哪个网站'@ JsonManagedReference'? – bielas
好吧,但为什么你使用'@JsonManagedReference 私人设置 userRoles;'而不是'@JsonBackReference 私人设置 userRoles;'? –
bielas
再次编辑;)..我无法更好地解释它。也请看看我链接的其他问题。有人也认为它(也许更好)。 – KLHauser