Java Servlet Gson().toJson无限循环

Java Servlet Gson().toJson无限循环,java,javascript,servlets,gson,Java,Javascript,Servlets,Gson,我在servlet中遇到了一些问题,每次我更改下拉菜单中的选项, 一个不同的值将被传递给servlet,然后它将产生一个无限循环。当我没有更改下拉列表中的选项(值没有更改)时,没有错误 这是mycode: 我的Javascript: <script> function loadStaff(){ //dropdown var positionDropDown = document.getElementById("positionsDropdown"); //value of the

我在servlet中遇到了一些问题,每次我更改下拉菜单中的选项, 一个不同的值将被传递给servlet,然后它将产生一个无限循环。当我没有更改下拉列表中的选项(值没有更改)时,没有错误

这是mycode:

我的Javascript:

<script>

function loadStaff(){
//dropdown
var positionDropDown = document.getElementById("positionsDropdown");
//value of the drop down
var positionID = positionDropDown.options[positionDropDown.selectedIndex].value;

    $.getJSON('loadStaff?positionID=' + positionID, function(data) {
            -- no populate code yet
});
}
</script>
输出错误:

> INFO:   WebModule[null] ServletContext.log(): The server side
> component of the HTTP Monitor has detected a
> java.lang.StackOverflowError. This happens when there is an infinite
> loop in the web module. Correct the cause of the infinite loop before
> running the web module again.
> 
> INFO:   The server side component of the HTTP Monitor has detected a
> java.lang.StackOverflowError. This happens when there is an infinite
> loop in the web module. Correct the cause of the infinite loop before
> running the web module again. WARNING:  
> StandardWrapperValve[AjaxServlet]: Servlet.service() for servlet
> AjaxServlet threw exception java.lang.StackOverflowError

> WARNING:   StandardWrapperValve[AjaxServlet]: Servlet.service() for
> servlet AjaxServlet threw exception java.lang.StackOverflowError  at
> sun.util.calendar.ZoneInfo.getOffsets(ZoneInfo.java:248)  at
> java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2276)
>   at
> java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2248)
>   at java.util.Calendar.setTimeInMillis(Calendar.java:1140)   at
> java.util.Calendar.setTime(Calendar.java:1106)    at
> java.text.SimpleDateFormat.format(SimpleDateFormat.java:955)  at
> java.text.SimpleDateFormat.format(SimpleDateFormat.java:948)  at
> java.text.DateFormat.format(DateFormat.java:336)  at
> com.google.gson.internal.bind.DateTypeAdapter.write(DateTypeAdapter.java:90)
>   at
> com.google.gson.internal.bind.DateTypeAdapter.write(DateTypeAdapter.java:41)
>   at
> com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
>   at
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
>   at
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
>   at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:892)  at
> com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
>   at
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
>   at
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
>   at
> com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
>   at
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
>   at
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
>   at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:892)  at
> com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
>   at
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
>   at
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
>   at
> com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
>   at
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
>   at
> com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
>   at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:892)
我还注意到,这些跟踪只是stacktrace的重复输出

编辑: 职员班

@Entity
public class Staff implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "last_name")
    private String lastName;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "first_name")
    private String firstName;
    @Size(max = 45)
    @Column(name = "middle_name")
    private String middleName;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 6)
    @Column(name = "gender")
    private String gender;
    @Basic(optional = false)
    @NotNull
    @Column(name = "date_of_birth")
    @Temporal(TemporalType.DATE)
    private Date dateOfBirth;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "nationality")
    private String nationality;
    @Basic(optional = false)
    @NotNull
    @Column(name = "date_hired")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateHired;
    @Size(max = 20)
    @Column(name = "status")
    private String status;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "staff")
    private Collection<StaffApointments> staffApointmentsCollection;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "staff")
    private StaffContact staffContact;
    @JoinColumn(name = "account_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Account accountId;
    @JoinColumn(name = "position_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Position positionId;

    public Staff() {
    }

    public Staff(Integer id) {
        this.id = id;
    }

    public Staff(Integer id, String lastName, String firstName, String gender, Date dateOfBirth, String nationality, Date dateHired) {
        this.id = id;
        this.lastName = lastName;
        this.firstName = firstName;
        this.gender = gender;
        this.dateOfBirth = dateOfBirth;
        this.nationality = nationality;
        this.dateHired = dateHired;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getMiddleName() {
        return middleName;
    }

    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getNationality() {
        return nationality;
    }

    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public Date getDateHired() {
        return dateHired;
    }

    public void setDateHired(Date dateHired) {
        this.dateHired = dateHired;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @XmlTransient
    public Collection<StaffApointments> getStaffApointmentsCollection() {
        return staffApointmentsCollection;
    }

    public void setStaffApointmentsCollection(Collection<StaffApointments> staffApointmentsCollection) {
        this.staffApointmentsCollection = staffApointmentsCollection;
    }

    public StaffContact getStaffContact() {
        return staffContact;
    }

    public void setStaffContact(StaffContact staffContact) {
        this.staffContact = staffContact;
    }

    public Account getAccountId() {
        return accountId;
    }

    public void setAccountId(Account accountId) {
        this.accountId = accountId;
    }

    public Position getPositionId() {
        return positionId;
    }

    public void setPositionId(Position positionId) {
        this.positionId = positionId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Staff)) {
            return false;
        }
        Staff other = (Staff) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Staff[ id=" + id + " ]";
    }

}
@实体
公共类Staff实现可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@基本(可选=假)
@列(name=“id”)
私有整数id;
@基本(可选=假)
@NotNull
@尺寸(最小值=1,最大值=45)
@列(name=“last_name”)
私有字符串lastName;
@基本(可选=假)
@NotNull
@尺寸(最小值=1,最大值=45)
@列(name=“first_name”)
私有字符串名;
@尺寸(最大值=45)
@列(name=“middle_name”)
私有字符串名称;
@基本(可选=假)
@NotNull
@尺寸(最小值=1,最大值=6)
@列(name=“gender”)
私人字符串性别;
@基本(可选=假)
@NotNull
@列(name=“出生日期”)
@时态(TemporalType.DATE)
私人出生日期;
@基本(可选=假)
@NotNull
@尺寸(最小值=1,最大值=45)
@列(name=“国籍”)
私人国籍;
@基本(可选=假)
@NotNull
@列(name=“雇用日期”)
@时态(TemporalType.TIMESTAMP)
私人约会;
@尺寸(最大值=20)
@列(name=“status”)
私有字符串状态;
@OneToMany(cascade=CascadeType.ALL,mappedBy=“staff”)
私人收款staffapointenscollection;
@OneTONE(cascade=CascadeType.ALL,mappedBy=“staff”)
私人员工联系人;员工联系人;
@JoinColumn(name=“account\u id”,referencedColumnName=“id”)
@多通(可选=假)
私人帐户账号;
@JoinColumn(name=“position\u id”,referencedColumnName=“id”)
@多通(可选=假)
私人职位ID;
公职人员(){
}
公职人员(整数id){
this.id=id;
}
公职人员(整数id、字符串姓氏、字符串姓氏、字符串性别、出生日期、字符串国籍、雇用日期){
this.id=id;
this.lastName=lastName;
this.firstName=firstName;
这个。性别=性别;
this.dateOfBirth=出生日期;
国籍=国籍;
this.dateHired=dateHired;
}
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共字符串getLastName(){
返回姓氏;
}
public void setLastName(字符串lastName){
this.lastName=lastName;
}
公共字符串getFirstName(){
返回名字;
}
public void setFirstName(字符串firstName){
this.firstName=firstName;
}
公共字符串getMiddleName(){
返回中间名;
}
public void setMiddleName(字符串middleName){
this.middleName=middleName;
}
公共字符串getGender(){
返回性别;
}
公共无效设置性别(字符串性别){
这个。性别=性别;
}
公共日期getDateOfBirth(){
出生返回日期;
}
公共无效设置出生日期(出生日期){
this.dateOfBirth=出生日期;
}
公共字符串getNational(){
返回国籍;
}
公共国籍(字符串国籍){
国籍=国籍;
}
公共日期getDateHired(){
返回日期;
}
公共作废setDateHired(雇用日期){
this.dateHired=dateHired;
}
公共字符串getStatus(){
返回状态;
}
公共无效设置状态(字符串状态){
这个状态=状态;
}
@XmlTransient
公共集合GetStaffaPointsCollection(){
返回StataPointsCollection;
}
公共作废SetStataPointsCollection(集合StataPointsCollection){
this.staffApointmentsCollection=staffApointmentsCollection;
}
公共StaffContact getStaffContact(){
返回员工联系人;
}
公共无效设置StaffContact(StaffContact StaffContact){
this.staffContact=staffContact;
}
公共帐户getAccountId(){
返回accountId;
}
公共无效setAccountId(帐户ID){
this.accountId=accountId;
}
公共位置getPositionId(){
返回位置ID;
}
公共无效setPositionId(职位职位ID){
this.positionId=positionId;
}
@凌驾
公共int hashCode(){
int hash=0;
hash+=(id!=null?id.hashCode():0);
返回散列;
}
@凌驾
公共布尔等于(对象){
//TODO:警告-如果未设置id字段,此方法将不起作用
if(!(人员的对象实例)){
返回false;
}
Staff other=(Staff)对象;
如果((this.id==null&&other.id!=null)| |(this.id!=null&&!this.id.equals(other.id))){
返回false;
}
返回true;
}
@凌驾
公共字符串toString(){
返回“entity.Staff[id=“+id+”]”;
}
}
以下是职位类别:

@Entity
public class Position implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "name")
    private String name;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "positionId")
    private Collection<Staff> staffCollection;

    public Position() {
    }

    public Position(Integer id) {
        this.id = id;
    }

    public Position(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlTransient
    public Collection<Staff> getStaffCollection() {
        return staffCollection;
    }

    public void setStaffCollection(Collection<Staff> staffCollection) {
        this.staffCollection = staffCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Position)) {
            return false;
        }
        Position other = (Position) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Position[ id=" + id + " ]";
    }

}
@实体
公共类位置实现可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@基本(可选=假)
@列(name=“id”)
私有整数id;
@基本(可选=假)
@NotNull
@尺寸(最小值=1,最大值=45)
@列(name=“name”)
私有字符串名称;
@OneToMany(cascade=CascadeType.ALL,mappedBy=“posi
@Entity
public class Staff implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "last_name")
    private String lastName;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "first_name")
    private String firstName;
    @Size(max = 45)
    @Column(name = "middle_name")
    private String middleName;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 6)
    @Column(name = "gender")
    private String gender;
    @Basic(optional = false)
    @NotNull
    @Column(name = "date_of_birth")
    @Temporal(TemporalType.DATE)
    private Date dateOfBirth;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "nationality")
    private String nationality;
    @Basic(optional = false)
    @NotNull
    @Column(name = "date_hired")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateHired;
    @Size(max = 20)
    @Column(name = "status")
    private String status;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "staff")
    private Collection<StaffApointments> staffApointmentsCollection;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "staff")
    private StaffContact staffContact;
    @JoinColumn(name = "account_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Account accountId;
    @JoinColumn(name = "position_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Position positionId;

    public Staff() {
    }

    public Staff(Integer id) {
        this.id = id;
    }

    public Staff(Integer id, String lastName, String firstName, String gender, Date dateOfBirth, String nationality, Date dateHired) {
        this.id = id;
        this.lastName = lastName;
        this.firstName = firstName;
        this.gender = gender;
        this.dateOfBirth = dateOfBirth;
        this.nationality = nationality;
        this.dateHired = dateHired;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getMiddleName() {
        return middleName;
    }

    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getNationality() {
        return nationality;
    }

    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public Date getDateHired() {
        return dateHired;
    }

    public void setDateHired(Date dateHired) {
        this.dateHired = dateHired;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @XmlTransient
    public Collection<StaffApointments> getStaffApointmentsCollection() {
        return staffApointmentsCollection;
    }

    public void setStaffApointmentsCollection(Collection<StaffApointments> staffApointmentsCollection) {
        this.staffApointmentsCollection = staffApointmentsCollection;
    }

    public StaffContact getStaffContact() {
        return staffContact;
    }

    public void setStaffContact(StaffContact staffContact) {
        this.staffContact = staffContact;
    }

    public Account getAccountId() {
        return accountId;
    }

    public void setAccountId(Account accountId) {
        this.accountId = accountId;
    }

    public Position getPositionId() {
        return positionId;
    }

    public void setPositionId(Position positionId) {
        this.positionId = positionId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Staff)) {
            return false;
        }
        Staff other = (Staff) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Staff[ id=" + id + " ]";
    }

}
@Entity
public class Position implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "name")
    private String name;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "positionId")
    private Collection<Staff> staffCollection;

    public Position() {
    }

    public Position(Integer id) {
        this.id = id;
    }

    public Position(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlTransient
    public Collection<Staff> getStaffCollection() {
        return staffCollection;
    }

    public void setStaffCollection(Collection<Staff> staffCollection) {
        this.staffCollection = staffCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Position)) {
            return false;
        }
        Position other = (Position) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Position[ id=" + id + " ]";
    }

}