2017-03-09 79 views
0

我没有找到任何解决方案来管理失败,所以我决定创建一个新的问题。我有一个简单的类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方法后(仅获得),我得到了一些这些错误?

enter image description here

回答

1

你有你的UserUserRole对象之间的Infinite recursion。每当用户序列化时,他的相关用户角色也会被序列化。由于用户角色也与用户有关系,所以你有递归。

解决这可能是使用@JsonManagedReference(添加到用户的关系)和@JsonBackReference(在维吾尔的UserRole)。另请参见这里:Infinite Recursion with Jackson JSON and Hibernate JPA issue

@Entity 
public class User 
    ... 
    @JsonManagedReference 
    private Set<UserRole> userRoles; 


@Entity 
public class UserRole 
    ... 
    @JsonBackReference 
    private User user; 

@JsonManagedReference将意味着序列化过程中的关系部分考虑进去。所以相关的用户角色也会被序列化。由于那里的相关连接被标记为@JsonBackReference,因此序列化停止进一步。

+0

我刚刚意识到问题出在哪里,但我不能让如何使用'@ JsonBackReference'和'@ JsonManagedReference'?应在哪个网站上放置'@ JsonBackReference'和哪个网站'@ JsonManagedReference'? – bielas

+0

好吧,但为什么你使用'@JsonManagedReference 私人设置 userRoles;'而不是'@JsonBackReference 私人设置 userRoles;'? – bielas

+0

再次编辑;)..我无法更好地解释它。也请看看我链接的其他问题。有人也认为它(也许更好)。 – KLHauser

0

@KLHauser

那么如何,如果我有一个类

@Entity 
public class ReservationReminder { 
private int reservationReminderId; 
private Reservation reservation; 
private boolean isChecked; 
private Date checkedDate; 

和预订类

@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; 

@Entity 
public class Worker { 

private String workerPesel; 
private String name; 
private String surname; 
private Date startDateWorking; 
private String accountNumber; 
private List<Review> workerReview; 
private Adress workerAdress; 
private List<LaborHistory> workerLaborHistory; 
private Wash workerWash; 
//private List<WorkerWorkerTime> workerWorkTime; 
// private Role WORKER_ROLE; 
private User workerUser; 
private List<Reservation> workerReservation; 

而且我想从工人到加载ReservationReminder类管理的情况下预订课程?如果我使用@JsonIgonre像

@OneToOne(mappedBy = "reservationReminder", fetch = FetchType.LAZY) 
@JsonIgnore 
public Reservation getReservation() { 
    return reservation; 
} 

我只得到了与checkedDate,和器isChecked一个reservationReminderId的JSON ReservationReminder

+0

如果您想要将ReservationReminder对象与Reservation及其Worker一起加载,则不应在任何对应关系中使用@ JsonIgnore。这意味着在生成Json时该关系将被忽略。 – KLHauser

+0

也尝试过使用'@ JsonManagedReference'和'@JsonBackReference',但没有工作 – bielas

+0

首先,您需要将这些注释添加到'User'和'UserRole'中,正如我在答案中所描述的那样。那么如果这个无限递归是固定的,我们会看得更远。这里的关系后面的序列化路径看起来像ReservationReminder - > Reservation - > Worker - > User - > UserRole – KLHauser