本文以用户、订单、订单详情三者之间的关系来详细介绍jpa的功能使用。这个场景也比较符合实际开发中的业务场景。
实际的业务中,用户和订单是一对多的关系,订单和订单详情也是一对多的关系。
用户表(t_user),订单表(t_order),订单详情表(t_order_item)
三张表的结构如下:
这里就不对jpa的一些基本知识展开讲了,不是很清楚的可以自己先学习下jpa基本的知识。
首先准备好基本的实体类。
1. 用户表相关实体类
package com.example.jpa.entity;
import java.io.Serializable;
import java.util.List;
public class User implements Serializable {
private static final long serialVersionUID = 263434701950670170L;
private long userId;
private String username;
private String pwd;
public long getUserId() {
return userId;
}
public void setUserId(final long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(final String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(final String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return String.format("user_id: %d, username: %s, pwd: %s", userId, username, pwd);
}
}
package com.example.jpa.entity;
import javax.persistence.*;
import java.util.List;
import static javax.persistence.GenerationType.IDENTITY;
@Entity
@Table(name = "t_user")
public final class UserEntity extends User {
private static final long serialVersionUID = -3708998745561667721L;
@Id
@Column(name = "user_id")
@GeneratedValue(strategy = IDENTITY)
@Override
public long getUserId() {
return super.getUserId();
}
@Column(name = "username")
@Override
public String getUsername() {
return super.getUsername();
}
@Column(name = "pwd")
@Override
public String getPwd() {
return super.getPwd();
}
}
2. 订单表实体类
public class Order implements Serializable {
private static final long serialVersionUID = 661434701950670670L;
private long orderId;
private long userId;
private long addressId;
private String status;
public long getOrderId() {
return orderId;
}
public void setOrderId(final long orderId) {
this.orderId = orderId;
}
public long getUserId() {
return userId;
}
public void setUserId(final long userId) {
this.userId = userId;
}
public String getStatus() {
return status;
}
public void setStatus(final String status) {
this.status = status;
}
public long getAddressId() {
return addressId;
}
public void setAddressId(final long addressId) {
this.addressId = addressId;
}
@Override
public String toString() {
return String.format("order_id: %s, user_id: %s, address_id: %s, status: %s", orderId, userId, addressId, status);
}
}
package com.example.jpa.entity;
import java.io.Serializable;
public class OrderItem implements Serializable {
private static final long serialVersionUID = 263434701950670170L;
private long orderItemId;
private long orderId;
private long userId;
private String status;
public long getOrderItemId() {
return orderItemId;
}
public void setOrderItemId(final long orderItemId) {
this.orderItemId = orderItemId;
}
public long getOrderId() {
return orderId;
}
public void setOrderId(final long orderId) {
this.orderId = orderId;
}
public long getUserId() {
return userId;
}
public void setUserId(final long userId) {
this.userId = userId;
}
public String getStatus() {
return status;
}
public void setStatus(final String status) {
this.status = status;
}
@Override
public String toString() {
return String.format("order_item_id:%s, order_id: %s, user_id: %s, status: %s", orderItemId, orderId,userId, status);
}
}
3.订单详情实体类
package com.example.jpa.entity;
import java.io.Serializable;
public class OrderItem implements Serializable {
private static final long serialVersionUID = 263434701950670170L;
private long orderItemId;
private long orderId;
private long userId;
private String status;
public long getOrderItemId() {
return orderItemId;
}
public void setOrderItemId(final long orderItemId) {
this.orderItemId = orderItemId;
}
public long getOrderId() {
return orderId;
}
public void setOrderId(final long orderId) {
this.orderId = orderId;
}
public long getUserId() {
return userId;
}
public void setUserId(final long userId) {
this.userId = userId;
}
public String getStatus() {
return status;
}
public void setStatus(final String status) {
this.status = status;
}
@Override
public String toString() {
return String.format("order_item_id:%s, order_id: %s, user_id: %s, status: %s", orderItemId, orderId,userId, status);
}
}
package com.example.jpa.entity;
import javax.persistence.*;
@Entity
@Table(name = "t_order_item")
public final class OrderItemEntity extends OrderItem {
private static final long serialVersionUID = 5685474394188443341L;
@Id
@Column(name = "order_item_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Override
public long getOrderItemId() {
return super.getOrderItemId();
}
@Column(name = "order_id")
@Override
public long getOrderId() {
return super.getOrderId();
}
@Column(name = "user_id")
@Override
public long getUserId() {
return super.getUserId();
}
@Column(name = "status")
@Override
public String getStatus() {
return super.getStatus();
}
}
通过单元测试插入测试需要的数据。
@Test
public void insertData(){
//插入用户数据
UserEntity userEntity = new UserEntity();
userEntity.setUsername("snail");
userEntity.setPwd("123456");
userRepository.save(userEntity);
//插入订单数据
OrderEntity order = new OrderEntity();
order.setAddressId(1);
order.setOrderId(1);
order.setUserId(userEntity.getUserId());
order.setStatus("订单测试数据");
orderRepository.save(order);
//插入订单详情数据
OrderItemEntity orderItemEntity = new OrderItemEntity();
orderItemEntity.setOrderId(order.getOrderId());
orderItemEntity.setUserId(userEntity.getUserId());
orderItemEntity.setStatus("订单详情测试数据");
orderItemRepository.save(orderItemEntity);
}
查看下数据库的基础数据:
开始实现第一个需求,查询订单时收关联查询订单详情。
在order实体中加入以下代码:
private List<OrderItem> orderItemList;
public List<OrderItem> getOrderItemList() {
return orderItemList;
}
public void setOrderItemList(List<OrderItem> orderItemList) {
this.orderItemList = orderItemList;
}
在OrderEntity中加入以下代码:
@OneToMany(targetEntity = OrderItemEntity.class)
@JoinColumn(name = "order_id")
@Override
public List<OrderItem> getOrderItemList() {
return super.getOrderItemList();
}
这里主要用到了两个注解@OneToMany表示一对多的关系
这里还是通过单元测试 查询订单表的数据,看看是否能自动查询订单详情的数据。
@Test
public void selectOrder(){
List<OrderEntity> all = orderRepository.findAll();
System.out.println(new Gson().toJson(all));
}
以下是执行结果:
为了方便观看,把格式化好的数据一块贴出来:
[
{
"orderId":1,
"userId":1,
"addressId":1,
"status":"订单测试数据",
"orderItemList":[
{
"orderItemId":1,
"orderId":1,
"userId":1,
"status":"订单详情测试数据"
}
]
}
]
这里主要是其实主要就用到了两个注解:
@OneToMany(targetEntity = OrderItemEntity.class)
@JoinColumn(name = "order_id")
修改User实体类,添加以下代码:
private List<Order> orderList;
public List<Order> getOrderList() {
return orderList;
}
public void setOrderList(List<Order> orderList) {
this.orderList = orderList;
}
修改UserEntity类,添加以下代码:
@OneToMany(targetEntity = OrderEntity.class)
@JoinColumn(name = "user_id")
@Override
public List<Order> getOrderList() {
return super.getOrderList();
}
还是采用单元测试来测试我们的代码:
@Test
public void selectUser(){
List<UserEntity> all = userRepository.findAll();
System.out.println(new Gson().toJson(all));
}
[
{
"userId":1,
"username":"snail",
"pwd":"123456",
"orderList":[
{
"orderId":1,
"userId":1,
"addressId":1,
"status":"订单测试数据",
"orderItemList":[
{
"orderItemId":1,
"orderId":1,
"userId":1,
"status":"订单详情测试数据"
}
]
}
]
}
]
这样就实现了本来开始说的两个需求了。如果问题请留言讨论。
以上这种一对多的关联方式其实是一种单向的一对多关联,通过外键的方式。可以查看数据表会生产对应的外键信息。