2015-12-14 27 views
1

我有兩個表: 客戶和地址:JPA - 一對多 - 如何合併實體的新舊值在JPA用最短的代碼

客戶表:

CREATE TABLE `customer` (
    `customer_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT, 
    `user_name` varchar(45) NOT NULL, 
    `password` varchar(45) NOT NULL, 
    `encrypt_key` varchar(200) NOT NULL, 
    `first_name` varchar(45) NOT NULL, 
    `last_name` varchar(45) NOT NULL, 
    `email` varchar(50) DEFAULT NULL, 
    `active` tinyint(1) NOT NULL DEFAULT '1', 
    `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    `self_description` varchar(2000) NOT NULL, 
    PRIMARY KEY (`customer_id`), 
    KEY `idx_last_name` (`last_name`), 
) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8; 

地址表:

CREATE TABLE `address` (
    `account_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT, 
    `customer_id` smallint(5) unsigned NOT NULL, 
    `address_type` varchar(15) NOT NULL, -- Office, Branch-1, Branch-2, 
    `door_num` varchar(50) NOT NULL, 
    `landmark` varchar(150) DEFAULT NULL, 
    `street` varchar(50) DEFAULT NULL, 
    `area_name` varchar(25) NOT NULL, 
    `district` varchar(25) NOT NULL, 
    `city` varchar(25) NOT NULL, 
    `postal_code` varchar(10) DEFAULT NULL, 
    `phone1` varchar(20) NOT NULL, 
    `phone2` varchar(20), 
    `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    PRIMARY KEY (`account_id`), 
    KEY `idx_fk_city` (`city`) 
) ENGINE=InnoDB AUTO_INCREMENT=201 DEFAULT CHARSET=utf8; 

在Customer.java

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) 
@JoinColumn(name = "customer_id", nullable = false) 
private List<Address> addresses; 

In Address.java 注意到客戶,因爲我使用uni-directction。

在CustomerDaoImpl.java

public boolean updateEntity(Customer customer) { 

    session = sessionFactory.openSession(); 
    tx = session.beginTransaction(); 
    session.saveOrUpdate(customer); 
    tx.commit(); 
    session.close(); 

    return false; 
} 

的問題是:
我做的更新,這是每次創建新的用戶。但我必須更新客戶及其子地址和主題對象。

我的請求體:

{ 
     "customerId": 102, 
     "addresses": [ 
     { 
      "accountId": 203, 
      "addressType": "main office", 
      "areaName": "area3", 
      "city": "city3", 
      "district": "district3", 
      "doorNum": "89", 
      "landmark": "landmark3", 
      "phone1": "646432365465", 
      "phone2": "4534542355675", 
      "postalCode": "453245", 
      "street": "street3" 
     } 
     ], 
     "active": 1, 
     "email": "[email protected]", 
     "encryptKey": "wwwwwfsad", 
     "firstName": "ccc", 
     "lastName": "ddd", 
     "password": "user2", 
     "selfDescription": "user2", 
     "userName": "user2", 
     "theme": { 
     "themeId": 402, 
     "description": "theme2", 
     "name": "theme2", 
     "categoryId": 301 
     } 
    } 

請告訴我什麼,所有的變化我必須做的。

+0

您需要先獲取舊的客戶實體,然後在客戶中添加新數據,然後更新客戶實體。而已。 –

回答

0

如果要更新數據庫中的記錄:首先必須從數據庫檢索記錄到持久性上下文然後更新其列值。我認爲你直接嘗試更新數據庫記錄,這是錯誤的。它只是創建具有不同ID的新對象並將其保存在數據庫中。

Customer retrievedCustomer = session.get(customer); 

make you changes on retrievedCustomer... 

session.update(retrievedCustomer); 
+0

你能否向我解釋一下詳情。 – Sun

+0

在hibernate中,我們只需要對象,我的意思是hibernate只是知道對象或實體。所以要更新數據庫中的記錄,我們應該在hibernate上下文中將該記錄作爲對象進行檢索,然後更新對象的字段並將其保存到數據庫中。 – samadadi