jpa 实体类更新表结构_片段4:文档结构实体的JPA模型

jpa 实体类更新表结构

Thinking in JSON

用JSON思考

每日行情 (Quote of the Day)

Before software can be reusable it first has to be usable.

在软件可重用之前,它首先必须是可用的。

— Ralph Johnson

—拉尔夫·约翰逊

:)

:)

In SQL, sometimes you want to store directly JSON documents without creating a relational table (like MongoDB or key-value pair). JSON documents support embedded fields, so related data and lists of data can be stored with the document instead of an external table.

在SQL中,有时您想直接存储JSON文档而不创建关系表(例如MongoDB或键值对)。 JSON文档支持嵌入式字段,因此相关数据和数据列表可以与文档一起存储,而不是与外部表一起存储。

In this snippet, you’ll find how to map a JSON document structure model in JPA without having a direct relational table.

在此代码段中,您将找到如何在JPA中映射JSON文档结构模型而又没有直接的关系表。

步骤1:定义实体 (Step 1: Define Entity)

Document structure of employee entity that has enum, collection, map, nested object, and nested collection.

具有枚举,集合,映射,嵌套对象和嵌套集合的员工实体的文档结构。

jpa 实体类更新表结构_片段4:文档结构实体的JPA模型_第1张图片

步骤2:JPA的模型实体 (Step 2: Model entity for JPA)

enum EmployeeStatus {
ACTIVE, FIRED
}@Embeddable
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
class Head {
@Column(name = "h_name")
private String name; public Head(String name) {
this.name = name;
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Head head = (Head) o;
return Objects.equals(name, head.name);
} @Override
public int hashCode() {
return Objects.hash(name);
} @Override
public String toString() {
return "Head{" +
"name='" + name + ''' +
'}';
}
}@Embeddable
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
class Department {
@Column(name = "d_name")
private String name;
private Head head;
@ElementCollection(fetch = FetchType.EAGER)
private List
addresses; public Department(String name, Head head, List
addresses) {
this.name = name;
this.head = head;
this.addresses = addresses;
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Department that = (Department) o;
return Objects.equals(name, that.name) &&
Objects.equals(head, that.head) &&
Objects.equals(addresses, that.addresses);
} @Override
public int hashCode() {
return Objects.hash(name, head, addresses);
} @Override
public String toString() {
return "Department{" +
"name='" + name + ''' +
", head=" + head +
", addresses=" + Arrays.toString(addresses.toArray()) +
'}';
}
}@Embeddable
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
class Address {
private String addressLine; public Address(String addressLine) {
this.addressLine = addressLine;
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Address address = (Address) o;
return Objects.equals(addressLine, address.addressLine);
} @Override
public int hashCode() {
return Objects.hash(addressLine);
} @Override
public String toString() {
return "Address{" +
"addressLine='" + addressLine + ''' +
'}';
}
}@Entity
@Table(name = "EMPLOYEES")
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
class Employee { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "e_name")
private String name;
@Embedded
private Department department;
@ElementCollection(fetch = FetchType.EAGER)
private Map skills;
@ElementCollection(fetch = FetchType.EAGER)
private List
addresses;
@Enumerated(EnumType.STRING)
private EmployeeStatus status;
private LocalDate joiningDate; public Employee(String name,
Department department,
Map skills,
List
addresses) {
this.name = name;
this.department = department;
this.skills = skills;
this.addresses = addresses;
this.joiningDate = LocalDate.now();
this.status = EmployeeStatus.ACTIVE;
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return Objects.equals(id, employee.id);
} @Override
public int hashCode() {
return Objects.hash(id);
} @Override
public String toString() {
return "Employee{" + "id=" + id + '}';
}
}

步骤3:JPA储存库 (Step 3: JPA Repository)

interface EmployeeRepository extends JpaRepository {
}

步骤4:用法 (Step 4: Usage)

@SpringBootApplication
@EnableJpaRepositories(considerNestedRepositories = true)
@Slf4j
public class JPAModelForDocumentStructureEntity { private final EmployeeRepository employeeRepository; public JPAModelForDocumentStructureEntity(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
} public static void main(String[] args) {
SpringApplication.run(JPAModelForDocumentStructureEntity.class, args);
} @EventListener
public void run(ApplicationReadyEvent readyEvent) {
Map skills = new HashMap<>();
skills.put("Java", 90);
skills.put("Python", 80);
List
addresses = List.of(new Address("addressLine1"), new Address("addressLine2"));
Department department = new Department("d-name", new Head("h-name"), addresses);
Employee employee = new Employee("e-name", department, skills, addresses); // Save
Employee saved = employeeRepository.save(employee); // Find by Id
Employee findById = employeeRepository.findById(saved.getId()).get(); log(findById);
} private void log(Employee employee) {
log.info("{}", employee);
log.info("-----------------");
log.info("Name: {}", employee.getName());
log.info("Department: {}", employee.getDepartment());
log.info("Skills: {}", employee.getSkills());
log.info("Addresses: {}", employee.getAddresses());
log.info("Status: {}", employee.getStatus());
log.info("Joining Date: {}", employee.getJoiningDate());
log.info("-----------------");
}
}

翻译自: https://medium.com/lead-by-examples/snippet-4-jpa-model-for-document-structure-entity-a2e1ad849452

jpa 实体类更新表结构

你可能感兴趣的:(python,人工智能,jpa,深度学习,机器学习)