2015-12-10 66 views
0

我有2個實體Customer和Address,關係是一個地址可以屬於多個客戶。JPA:問題持續存在一個OneToMany關係的2個實體

以下是客戶類,您可以看到它具有對地址對象的引用,在底層客戶表中它是地址的標識。我省略了getter和setter以及一些簡單的變量。

@Entity 
@Table(name = "customer") 
public class Customer implements Serializable { 

@Id 
@GeneratedValue 
@Column(name = "customer_id") 
private int customerId; 
@ManyToOne 
@JoinColumn(name = "store_id") 
private Store store; 
@ManyToOne 
@JoinColumn(name = "address_id") 
private Address address; 
........ 
} 

以下是地址類。

//Address Class 
@Entity 
@Table(name = "address") 
public class Address implements Serializable { 

@Id 
@GeneratedValue 
@Column(name = "address_id") 
private int addressId; 
@JoinColumn(name = "city_id") 
@ManyToOne 
private City city; 
@OneToMany(cascade = CascadeType.ALL, mappedBy = "address") 
@JsonIgnore 
List<Customer> customers; 
...... 
} 

我試圖在一次調用中堅持一個新客戶和一個新地址,像下面一樣保持。我省略了一些我設定的變量。

Customer cus = new Customer(); 
Address addr= new Address(); 
........ 
cus.setAddress(addr) 
List<Customer> cusList= new ArrayList<>(); 
cusList.add(cus); 
addr.setCustomers(cusList); 
entityManager.persist(cus)  

但我得到一個錯誤,說客戶表中的address_id爲空。我原以爲JPA會插入新的地址,然後插入新的地址標識列設置爲新的地址標識的客戶?我的思想錯了嗎?或者我在映射中犯了一個錯誤,或者我如何堅持這些實體?

另一種方法,我可以做到這一點是先堅持住地址,然後堅持客戶,但寧願如果可能的話在一個堅持做。

下面是基礎表。

//Customer Table 
CREATE TABLE `customer` (
    `customer_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT, 
    `store_id` tinyint(3) unsigned NOT NULL, 
    `first_name` varchar(45) NOT NULL, 
    `last_name` varchar(45) NOT NULL, 
    `email` varchar(50) DEFAULT NULL, 
    `address_id` smallint(5) unsigned NOT NULL, 
    `active` tinyint(1) NOT NULL DEFAULT '1', 
    `create_date` datetime NOT NULL, 
    `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE  CURRENT_TIMESTAMP, 
    PRIMARY KEY (`customer_id`), 
    KEY `idx_fk_store_id` (`store_id`), 
    KEY `idx_fk_address_id` (`address_id`), 
    KEY `idx_last_name` (`last_name`), 
    CONSTRAINT `fk_customer_address` FOREIGN KEY (`address_id`) REFERENCES `address` (`address_id`) ON UPDATE CASCADE, 
    CONSTRAINT `fk_customer_store` FOREIGN KEY (`store_id`) REFERENCES `store` (`store_id`) ON UPDATE CASCADE 
) ENGINE=InnoDB AUTO_INCREMENT=608 DEFAULT CHARSET=utf8; 

/Address Table 

CREATE TABLE `address` (
    `address_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT, 
    `address` varchar(50) NOT NULL, 
    `address2` varchar(50) DEFAULT NULL, 
    `district` varchar(20) NOT NULL, 
    `city_id` smallint(5) unsigned NOT NULL, 
    `postal_code` varchar(10) DEFAULT NULL, 
    `phone` varchar(20) NOT NULL, 
    `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    PRIMARY KEY (`address_id`), 
    KEY `idx_fk_city_id` (`city_id`), 
    CONSTRAINT `fk_address_city` FOREIGN KEY (`city_id`) REFERENCES `city` (`city_id`) ON UPDATE CASCADE 
) ENGINE=InnoDB AUTO_INCREMENT=619 DEFAULT CHARSET=utf8; 

謝謝。

回答

1

如果要保存新Address你需要添加CascadeType.ALL

@ManyToOne(cascade = CascadeType.ALL) 
@JoinColumn(name = "address_id") 
private Address address; 

而且通過這種方式,所有保存Customer(你不需要給客戶添加到地址列表,因爲客戶請參閱地址只是一個外鍵address_id

Customer cus = new Customer(); 
Address addr = new Address(); 
cus.setAddress(addr) 
entityManager.persist(cus) 

但是,這不是一個非常方便的方法,因爲地址是類似的參考。因此,通過節省每個客戶來更新地址參考是很不尋常的。

+0

謝謝你的工作。 – JCS

+0

@JCS不客氣 –

相關問題