领域对象+值对象在JPA下的落地
in developCo-De with 0 comment

领域对象+值对象在JPA下的落地

in developCo-De with 0 comment

数据库结构

CREATE TABLE t_demo_user(
    id VARCHAR(90) NOT NULL,
    username VARCHAR(90) NOT NULL,
    password VARCHAR(90) NOT NULL,
    address_city VARCHAR(90) NOT NULL,
    address_distinct VARCHAR(90) NOT NULL,
    address_street VARCHAR(90) NOT NULL,
    address_unit VARCHAR(90) NOT NULL,
    address_number VARCHAR(90) NOT NULL,
    PRIMARY KEY (id)
);

领域对象 User

@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Entity
@Table(name = "t_demo_user")
public class User {
  @EmbeddedId private UserId userId;
  private String username;
  private String password;
  @Embedded private Address address;
}

ID对象 UserId

@Data
@Embeddable
public class UserId implements java.io.Serializable {
  @Column(name = "id")
  @GeneratedValue
  private String value;
}

值对象 Address

@Data
@Embeddable
public class Address {
  @Column(name = "address_city")
  private String city;

  @Column(name = "address_distinct")
  private String distinct;

  @Column(name = "address_street")
  private String street;

  @Column(name = "address_unit")
  private String unit;

  @Column(name = "address_number")
  private String number;
}
Comments are closed.