zoukankan      html  css  js  c++  java
  • 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.

    三、

  • 相关阅读:
    (一)SpringCloud Alibaba Nacos服务注册和配置中心:Nacos简介
    (八)SpringCloud初级篇——Eureka服务注册与发现:服务发现Discovery
    (七)SpringCloud初级篇——Eureka服务注册与发现:actuator微服务信息完善
    (六)SpringCloud初级篇——Eureka服务注册与发现:集群Eureka构建步骤
    Windows系统修改Hosts文件
    (五)SpringCloud初级篇——Eureka服务注册与发现:单机Eureka构建步骤
    centos7 安装zookeeper
    热部署Devtools
    【阅读笔记】《C程序员 从校园到职场》第三章 程序的样式(版式和注释)
    【阅读笔记】《C程序员 从校园到职场》第二章 学校到职场
  • 原文地址:https://www.cnblogs.com/shamgod/p/5362816.html
Copyright © 2011-2022 走看看