我正在使用 JSON 和 JPA,并且有主要方法来对我的对象进行一些测试。
每当我尝试将 Java 实体转换为 JSON 字符串时,都会出现堆栈溢出错误
String jsonString = gson.toJson(cus1)
如果我在这种情况下使用常规 POJO 作为 DTO,则不会出现错误。但是,当我使用实体类时,为什么会出现堆栈溢出错误?
@Entity
@Table(name = "Customers")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c")
, @NamedQuery(name = "Customer.findById", query = "SELECT c FROM Customer c WHERE c.id = :id")
, @NamedQuery(name = "Customer.findByName", query = "SELECT c FROM Customer c WHERE c.name = :name")
, @NamedQuery(name = "Customer.findByAge", query = "SELECT c FROM Customer c WHERE c.age = :age")})
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private int age;
@JoinColumn(name = "city", referencedColumnName = "id")
@ManyToOne(optional = false)
private City city;
//getters, setters and constructor
噜噜哒
相关分类