使用hibernate访问数据时的java.lang.StackOverflowError

时间:2017-09-12 23:14:52

标签: java json spring hibernate web-services

当我尝试在hibernate中加入2个表时,我收到错误。这里复制了代码。

/*ADDRESS table entity mapping */

@Entity
@Table(name = "ADDRESS")
public class AddressEntity {

    @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
    @JoinColumn(name = "user_id")
    private UserEntity user;

    // setters and getters and other attributes.

}


/* APPUSER table entity mapping */

@Entity
@Table(name = "APPUSER")
public class UserEntity {

    @Id
    @GeneratedValue
    @Column(name = "USER_ID")
    private Long userId;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
    private Set<AddressEntity> addresses;

    // setters and getters and other attributes.

}

问题在于地址和用户实体的映射。每个用户可以有多个地址,这是要求。请帮忙。

下面的复制表DDL语句。用户和地址表之间是一对多的关系。

SQL表:

CREATE TABLE `appuser` (
    `user_id` int(11) NOT NULL AUTO_INCREMENT,
    `first_name` varchar(255) NOT NULL,
    `last_name` varchar(255) DEFAULT NULL,
    `email_address` varchar(1024) NOT NULL,
    `password` varchar(1024) NOT NULL,
    `active` tinyint(1) DEFAULT '1',
    `popularity_rating` int(11) DEFAULT NULL,
    `join_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8

CREATE TABLE `address` (
    `address_id` int(11) NOT NULL AUTO_INCREMENT,
    `address_line1` varchar(38) NOT NULL,
    `address_line2` varchar(38) NOT NULL,
    `city` varchar(38) NOT NULL,
    `state` varchar(38) NOT NULL,
    `zip` varchar(10) NOT NULL,
    `address_type` varchar(45) NOT NULL,
    `user_id` int(11) NOT NULL,
    PRIMARY KEY (`address_id`),
    KEY `fk_address_user_idx` (`user_id`),
    CONSTRAINT `fk_address_user` FOREIGN KEY (`user_id`) REFERENCES `appuser` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8

以上加入ADDRESS和USER表给出了StackOverflowError。

预期结果是:

{
    "userId": 1,
    "firstName": "alb",
    "lastName": "va",
    "emailAddress": "alb@va.com",
    "password": "albva",
    "addresses": [
        {
            "addressLine1": "222 plano pkwy",
            "addressLine2": "apt 22",
            "city": "plano",
            "state": "tx",
            "zip": "75034",
            "country": null
        }, {
            "addressLine1": "555 plano pkwy",
            "addressLine2": "apt 11",
            "city": "plano",
            "state": "tx",
            "zip": "75024",
            "country": null
        }
    ],
    "popularityRating": 6,
    "joinDate": 1504897922000,
    "roles": []
}

结果我在调用Web服务时遇到了无限循环。这导致堆栈溢出错误:

{"userId":2,"firstName":"rax","lastName":"tax","emailAddress":"rax@tax.com","password":"raxtax","addresses":[{"addressLine1":"2001 spring creek","addressLine2":"apt 724","city":"plano","state":"tx","zip":"75074","country":null,"addressType":null,"user":{"userId":2,"firstName":"rax","lastName":"tax","emailAddress":"rax@tax.com","password":"raxtax","addresses":[{"addressLine1":"2001 spring creek","addressLine2":"apt 724","city":"plano","state":"tx","zip":"75074","country":null,"addressType":null,"user":{"userId":2,"firstName":"rax","lastName":"tax","emailAddress":"rax@tax.com","password":"raxtax","addresses":[{"addressLine1":"2001 spring creek","addressLine2":"apt 724","city":"plano","state":"tx","zip":"75074","country":null,"addressType":null,"user":{"userId":2,"firstName":"rax","lastName":"tax","emailAddress":"rax@tax.com","password":"raxtax","addresses":[{"addressLine1":"2001 spring creek","addressLine2":"apt 724","city":"plano","state":"tx","zip":"75074","country":null,"addressType":null,"user":{"userId":2,"firstName":"rax","lastName":"tax","emailAddress":"rax@tax.com","password":"raxtax","addresses":[{"addressLine1":"2001 spring creek","addressLine2":"apt 724","city":"plano","state":"tx","zip":"75074","country":null,"addressType":null,"user":{"userId":2,"firstName":"rax","lastName":"tax","emailAddress":"rax@tax.com","password":"raxtax","addresses":[{"addressLine1":"2001 spring creek","addressLine2":"apt 724","city":"plano","state":"tx","zip":"75074","country":null,"addressType":null,"user":{"userId":2,"firstName":"rax","lastName":"tax","emailAddress":"rax@tax.com","password":"raxtax","addresses":[{"addressLine1":"2001 spring creek","addressLine2":"apt 724","city":"plano","state":"tx","zip":"75074","country":null,"addressType":null,"user": ...

1 个答案:

答案 0 :(得分:0)

当你尝试连接两个如你所提到的彼此依赖的表时,必然存在无限循环,因为表A试图从表B中获取数据。表B依赖于表A并尝试获取来自表A的数据。它进入无限循环并继续由此导致堆栈溢出异常。 解决方案是使用@JsonManagedReference和@JsonBackReference来注释您的字段。这样,标有@JsonManagedReference的类被序列化,而标有@JsonBackReference的类则不会被序列化。这打破了无限循环。在您的代码中,您的实体看起来像这样;

/*ADDRESS table entity mapping */

@Entity
@Table(name = "ADDRESS")
public class AddressEntity {

@ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
@JoinColumn(name = "user_id")
@JsonBackReference
private UserEntity user;

// setters and getters and other attributes.

}


/* APPUSER table entity mapping */

@Entity
@Table(name = "APPUSER")
public class UserEntity {

@Id
@GeneratedValue
@Column(name = "USER_ID")
private Long userId;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
@JsonManagedReference
private Set<AddressEntity> addresses;

// setters and getters and other attributes.

}