JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-002使用@Embeddable

一、数据库

二、代码

1.

 1 package org.jpwh.model.simple;
 2 
 3 import javax.persistence.Column;
 4 import javax.persistence.Embeddable;
 5 import javax.validation.constraints.NotNull;
 6 
 7 /**
 8  * 
 9  * Instead of <code>@Entity</code>, this component POJO is marked with <code>@Embeddable</code>. It
10  * has no identifier property.
11  */
12 @Embeddable
13 public class Address {
14 
15     @NotNull // Ignored for DDL generation!
16     @Column(nullable = false) // Used for DDL generation!
17     protected String street;
18 
19     @NotNull
20     @Column(nullable = false, length = 5) // Override VARCHAR(255)
21     protected String zipcode;
22 
23     @NotNull
24     @Column(nullable = false)
25     protected String city;
26 
27     /**
28      * Hibernate will call this no-argument constructor to create an instance, and then
29      * populate the fields directly.
30      */
31     protected Address() {
32     }
33 
34     /**
35      * You can have additional (public) constructors for convenience.
36      */
37     public Address(String street, String zipcode, String city) {
38         this.street = street;
39         this.zipcode = zipcode;
40         this.city = city;
41     }
42 
43     public String getStreet() {
44         return street;
45     }
46 
47     public void setStreet(String street) {
48         this.street = street;
49     }
50 
51     public String getZipcode() {
52         return zipcode;
53     }
54 
55     public void setZipcode(String zipcode) {
56         this.zipcode = zipcode;
57     }
58 
59     public String getCity() {
60         return city;
61     }
62 
63     public void setCity(String city) {
64         this.city = city;
65     }
66 }

You should override the equals() and hashCode() methods of Address and compare instances by value.

2.

 1 package org.jpwh.model.simple;
 2 
 3 import org.jpwh.model.Constants;
 4 
 5 import javax.persistence.AttributeOverride;
 6 import javax.persistence.AttributeOverrides;
 7 import javax.persistence.Column;
 8 import javax.persistence.Embedded;
 9 import javax.persistence.Entity;
10 import javax.persistence.GeneratedValue;
11 import javax.persistence.Id;
12 import javax.persistence.Table;
13 import java.io.Serializable;
14 import java.math.BigDecimal;
15 
16 @Entity
17 @Table(name = "USERS")
18 public class User implements Serializable {
19 
20     @Id
21     @GeneratedValue(generator = Constants.ID_GENERATOR)
22     protected Long id;
23 
24     public Long getId() {
25         return id;
26     }
27 
28     protected String username;
29 
30     public User() {
31     }
32 
33     public String getUsername() {
34         return username;
35     }
36 
37     public void setUsername(String username) {
38         this.username = username;
39     }
40 
41     // The Address is @Embeddable, no annotation needed here...
42     protected Address homeAddress;
43 
44     public Address getHomeAddress() {
45         return homeAddress;
46     }
47 
48     public void setHomeAddress(Address homeAddress) {
49         this.homeAddress = homeAddress;
50     }
51 
52     @Embedded // Not necessary...
53     @AttributeOverrides({
54             @AttributeOverride(name = "street",
55                     column = @Column(name = "BILLING_STREET")), // NULLable!
56             @AttributeOverride(name = "zipcode",
57                     column = @Column(name = "BILLING_ZIPCODE", length = 5)),
58             @AttributeOverride(name = "city",
59                     column = @Column(name = "BILLING_CITY"))
60     })
61     protected Address billingAddress;
62 
63     public Address getBillingAddress() {
64         return billingAddress;
65     }
66 
67     public void setBillingAddress(Address billingAddress) {
68         this.billingAddress = billingAddress;
69     }
70 
71     public BigDecimal calcShippingCosts(Address fromLocation) {
72         // Empty implementation of business method
73         return null;
74     }
75 
76     // ...
77 }

 

3.

三、

posted @ 2016-04-07 11:35  shamgod  阅读(194)  评论(0编辑  收藏  举报
haha