0
我在嘗試在休眠中連接2個表時遇到錯誤。在這裏複製代碼。使用休眠訪問數據時java.lang.StackOverflowError
/*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語句如下。它是用戶和地址表之間的一對多關係。
SQLS表:
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
上面加入地址和用戶表給我的StackOverflowError。
預期的結果是:
{
"userId": 1,
"firstName": "alb",
"lastName": "va",
"emailAddress": "[email protected]",
"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服務,我得到的是一個無限loop.Which導致堆棧溢出錯誤:
{"userId":2,"firstName":"rax","lastName":"tax","emailAddress":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","password":"raxtax","addresses":[{"addressLine1":"2001 spring creek","addressLine2":"apt 724","city":"plano","state":"tx","zip":"75074","country":null,"addressType":null,"user": ...
哪裏堆棧跟蹤? –