2016-12-16 62 views
0

讓我們看一下表結構: -如何在共享主鍵的情況下訪問對象作爲JPA中的一對一關係?

CREATE TABLE `customer` (
    `id` bigint(20) UNSIGNED AUTO_INCREMENT NOT NULL, 
    `first_name` varchar(150) COLLATE utf8_unicode_ci NOT NULL, 
    `last_name` varchar(150) COLLATE utf8_unicode_ci NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 
ALTER TABLE `customer` ADD PRIMARY KEY (`id`); 

------------------------------------------------------------- 

CREATE TABLE `address` (
    `id` bigint(20) UNSIGNED NOT NULL, 
    `street` varchar(512) COLLATE utf8_unicode_ci NOT NULL, 
    `city` varchar(512) COLLATE utf8_unicode_ci NOT NULL, 
    `country` varchar(512) COLLATE utf8_unicode_ci NOT NULL, 
    `postal_code` varchar(512) COLLATE utf8_unicode_ci NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 
ALTER TABLE `address` ADD PRIMARY KEY (`id`); 

地址&客戶通過id通過一個一對一的關係映射。那就是customer.id被分配給address_id [I know that a column of a table may role as a primary-key which is generated from some other table]。

讓我們看看實體: -

@Entity 
public class Customer { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 
    private String firstName; 
    private String lastName; 

    @OneToOne 
    @PrimaryKeyJoinColumn 
    private Address address; 

    // getters & setters 

    @Override 
    public String toString() { 
     return String.format(
      "Customer[id=%d, firstName='%s', lastName='%s', address='%s']", 
      id, firstName, lastName, address 
     ); 
    } 
} 

////////////////////////////////////////////////////////////////////////// 

@Entity 
public class Address { 

    @Id @GeneratedValue(generator = "customForeignGenerator") 
    @org.hibernate.annotations.GenericGenerator(
      name = "customForeignGenerator", 
      strategy = "foreign", 
      parameters = @Parameter(name = "property", value = "customer") 
    ) 
    private Long id; 
    private String street; 
    private String city; 
    private String country; 
    private String postalCode; 

    @OneToOne(mappedBy="address") 
    @PrimaryKeyJoinColumn 
    public Customer customer; 

    // getters & setters 

    @Override 
    public String toString() { 
     return String.format(
      "Address[id='%d', street='%s', city='%s', country='%s', postalCode='%s']", 
      id, street, city, country, postalCode 
     ); 
    } 

} 

控制器類: -

@RestController 
@RequestMapping("/api/customers") 
public class CustomerRestController { 

    // CustomerRepository extends CrudRepository<Customer, Long> 
    @Autowired 
    private CustomerRepository customerRepository; 

    // AddressRepository extends CrudRepository<Address, Long> 
    @Autowired 
    private AddressRepository addressRepository; 

    @RequestMapping("/customer-with-address/{customerId}") 
    public Customer getCustomerWithAddress(@PathVariable("customerId") Long customerId) { 
     return customerRepository.findOne(customerId); 
    } 

    @RequestMapping("/save-customer-with-address") 
    public Customer getCustomer() { 
     Customer customer = new Customer(); 
     customer.setFirstName("ABC"); 
     customer.setLastName("XYZ"); 
     Customer customer1 = customerRepository.save(customer); 

     Address address = new Address(); 
     address.setStreet("street " + customer1.getId()); 
     address.setCity("city " + customer1.getId()); 
     address.setCountry("country " + customer1.getId()); 
     address.setPostalCode("postal_code " + customer1.getId()); 
     address.setCustomer(customer1); 
     addressRepository.save(address); 

     return customer; 
    } 
} 

問題: -

我跟着JPA Hibernate One-to-One relationship

  1. /customer-with-address/5屬於遞歸。我如何從遞歸轉義?
  2. /save-customer-with-address將數據保存到數據庫之後,將客戶對象的地址返回爲空。這是因爲地址未設置爲客戶對象。當我試圖爲客戶對象設置地址時,它也落入遞歸中。那麼,我該如何設置/獲取客戶對象的地址對象?
  3. one-to-one從任何實體做的映射,就像我以前做的那樣。但是這裏的映射是從兩側完成的。 我可以從任何一方做什麼?

N.B: -所有的問題都可以很容易,如果我把一個額外的列customer_id地址表來解決。但我不想添加customer_id。我提前感謝您的寶貴意見。

回答

相關問題