close
使用 Hibernate Tool 去產生 Domain Code ( Model ) 時, 當 User.java & Authority.java 有相互參考時, 在 spring boot 使用 @RestController 會發生 Direct self-reference leading to cycle 的錯誤訊息, 解決的方法可以在 Domain Code ( Model ) 上加上 @JsonManagedReference & @JsonBackReference 來防止錯誤.
User.java
/** * User generated by hbm2java */ @Entity @Table(name = "user") public class User implements java.io.Serializable { private Long id; private User userByCreateUser; private User userByUpdateUser; private String username; private String password; private String firstname; private String lastname; private String email; private Boolean enabled; private Date lastpasswordresetdate; private Date activeDate; private Date inactiveDate; private Date createDate; private Date updateDate; private Set authorities = new HashSet(0); private Set usersForCreateUser = new HashSet(0); private Set usersForUpdateUser = new HashSet(0); public User() { } public User(String username, String password, String firstname, String lastname, String email, Date lastpasswordresetdate) { this.username = username; this.password = password; this.firstname = firstname; this.lastname = lastname; this.email = email; this.lastpasswordresetdate = lastpasswordresetdate; } public User(User userByCreateUser, User userByUpdateUser, String username, String password, String firstname, String lastname, String email, Boolean enabled, Date lastpasswordresetdate, Date activeDate, Date inactiveDate, Date createDate, Date updateDate, Set authorities, Set usersForCreateUser, Set usersForUpdateUser) { this.userByCreateUser = userByCreateUser; this.userByUpdateUser = userByUpdateUser; this.username = username; this.password = password; this.firstname = firstname; this.lastname = lastname; this.email = email; this.enabled = enabled; this.lastpasswordresetdate = lastpasswordresetdate; this.activeDate = activeDate; this.inactiveDate = inactiveDate; this.createDate = createDate; this.updateDate = updateDate; this.authorities = authorities; this.usersForCreateUser = usersForCreateUser; this.usersForUpdateUser = usersForUpdateUser; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "create_user") public User getUserByCreateUser() { return this.userByCreateUser; } public void setUserByCreateUser(User userByCreateUser) { this.userByCreateUser = userByCreateUser; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "update_user") public User getUserByUpdateUser() { return this.userByUpdateUser; } public void setUserByUpdateUser(User userByUpdateUser) { this.userByUpdateUser = userByUpdateUser; } @Column(name = "username", nullable = false, length = 50) public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } @Column(name = "password", nullable = false, length = 100) public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } @Column(name = "firstname", nullable = false, length = 50) public String getFirstname() { return this.firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } @Column(name = "lastname", nullable = false, length = 50) public String getLastname() { return this.lastname; } public void setLastname(String lastname) { this.lastname = lastname; } @Column(name = "email", nullable = false, length = 50) public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } @Column(name = "enabled") public Boolean getEnabled() { return this.enabled; } public void setEnabled(Boolean enabled) { this.enabled = enabled; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "lastpasswordresetdate", nullable = false, length = 19) public Date getLastpasswordresetdate() { return this.lastpasswordresetdate; } public void setLastpasswordresetdate(Date lastpasswordresetdate) { this.lastpasswordresetdate = lastpasswordresetdate; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "active_date", length = 19) public Date getActiveDate() { return this.activeDate; } public void setActiveDate(Date activeDate) { this.activeDate = activeDate; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "inactive_date", length = 19) public Date getInactiveDate() { return this.inactiveDate; } public void setInactiveDate(Date inactiveDate) { this.inactiveDate = inactiveDate; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "create_date", length = 19) public Date getCreateDate() { return this.createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "update_date", length = 19) public Date getUpdateDate() { return this.updateDate; } public void setUpdateDate(Date updateDate) { this.updateDate = updateDate; } @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "user_authority", catalog = "my_spring", joinColumns = { @JoinColumn(name = "user_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "authority_id", nullable = false, updatable = false) }) @JsonManagedReference public Set getAuthorities() { return this.authorities; } public void setAuthorities(Set authorities) { this.authorities = authorities; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "userByCreateUser") public Set getUsersForCreateUser() { return this.usersForCreateUser; } public void setUsersForCreateUser(Set usersForCreateUser) { this.usersForCreateUser = usersForCreateUser; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "userByUpdateUser") public Set getUsersForUpdateUser() { return this.usersForUpdateUser; } public void setUsersForUpdateUser(Set usersForUpdateUser) { this.usersForUpdateUser = usersForUpdateUser; } }
Authority.java
/** * Authority generated by hbm2java */ @Entity @Table(name = "authority") public class Authority implements java.io.Serializable { private Long id; private String name; private String description; private Set users = new HashSet(0); public Authority() { } public Authority(String name, String description) { this.name = name; this.description = description; } public Authority(String name, String description, Set users) { this.name = name; this.description = description; this.users = users; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } @Column(name = "name", nullable = false, length = 50) public String getName() { return this.name; } public void setName(String name) { this.name = name; } @Column(name = "description", nullable = false, length = 100) public String getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "user_authority", catalog = "my_spring", joinColumns = { @JoinColumn(name = "authority_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "user_id", nullable = false, updatable = false) }) @JsonBackReference public Set getUsers() { return this.users; } public void setUsers(Set users) { this.users = users; } }
參考:
http://keenformatics.blogspot.com/2013/08/how-to-solve-json-infinite-recursion.html
文章標籤
全站熱搜
留言列表