1. one to many:
1.1 db tables:
CREATE TABLE customer
(
customer_id bigint NOT NULL,
adresse VARCHAR (255) NOT NULL,
city VARCHAR (255) NOT NULL,
name VARCHAR (255) NOT NULL,
phone VARCHAR (255) NOT NULL,
state VARCHAR (255) NOT NULL,
tax_id INTEGER NOT NULL,
zip VARCHAR (255) NOT NULL,
PRIMARY KEY (customer_id)
);
CREATE TABLE customer_order
(
order_id bigint NOT NULL,
date_placed DATE NOT NULL,
date_promised DATE,
status VARCHAR (255) NOT NULL,
terms VARCHAR (255) NOT NULL,
customer_id bigint,
PRIMARY KEY (order_id)
);
ALTER TABLE customer_order ADD CONSTRAINT FK86DB8BAD595591FB FOREIGN KEY (customer_id) REFERENCES customer
1.2 Customer class ( one side)
@OneToMany(mappedBy = "customer",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<CustomerOrder> CustomerOrders;
1.3 CustomerOrder ( many side )
@ManyToOne(fetch=FetchType.EAGER) //fetch=FetchType.LAZY or EAGER
@JoinColumn(name="customer_id")
//@Fetch(FetchMode.JOIN)
private Customer customer;
Keys: a. cascade -> ALL: save customer will also save orders of it.
b. fetch = FetchType.LAZY: lazy loading, if you want the order loaded with customer, you have
to access it like this:
public Customer getCustomerAndOrdersById(Long id) {
Customer customer = customerDao.getById(id);
for (CustomerOrder c : customer.getCustomerOrders())
; // no n+1 issue.
}
c. If fetch = FetchType.EAGER: no problem. It is alway loaded with customer loading.
1.4 If you remove the ,cascade = CascadeType.ALL, you have to save both customer and his orders
manually, and load them separately.
分享到:
相关推荐
One2Many JPA OrderBy 与 InMemory 排序示例 订购方式 用于按顺序创建实体的卷曲通过示例 curl -iX POST ...
使用MySql示例的带有Hibernate的Spring Data JPA 该项目使用MySql Example描述了带有Spring Data JPA和Hibernate的Spring Boot示例。 描述 该项目显示了存储在MySql数据库中的用户列表。 使用以下端点,可以实现...
JPA2.0 加入了大量必须的 ORM 映射增强特性,包括:支持通过使用 @ElementCollection 标注来增强 collections, maps 和 lists( 这里不是指实体之间的关联关系 ) 集合,支持 map 的单向 one-to-many 关联 (JPA1.0...
Spring Boot和JPA的Hibernate教程和演示
Java Enterprise Edition (Java EE) continues to be one of the leading Java technologies and platforms from Oracle (previously Sun). Beginning Java EE 6 Platform with GlassFish 3, Second Edition is this...
support simple JP-QL queries (restrictions and many-to-one joins) The medium target is: support other key/value stores support other NoSQL families support complex joins and aggregations The project...
https://www.callicoder.com/hibernate-spring-boot-jpa-one-to-many-mapping-example/ 或一对多哪种方法是最好的 https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa
Java Enterprise Edition (Java EE) continues to be one of the leading Java technologies and platforms from Oracle (previously Sun). Beginning Java EE 6 Platform with GlassFish 3, Second Edition is this...
一对多双向关系映射带有Spring Boot的Java Web Services和RESTful API 本教程将引导您完成使用Spring Boot,Spring Data JPA,Lombok,H2和Swagger映射JPA / Hibernate一对多双向关系的步骤。 下表关系图说明了此...
类与类之间的关系主要体现在表与表之间的关系进行操作,它们都市对对象进行操作,我们程序中把所有的表与类都映射在一起,它们通过配置文件中的many-to-one、one-to-many、many-to-many、 4. 说下Hibernate的缓存...
网上书店