java 使用JPA注释的项目实体与供应商建立多对一关系
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 使用JPA注释的项目实体与供应商建立多对一关系相关的知识,希望对你有一定的参考价值。
package com.project.model;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
@Entity
@Table(name="item")
public class Item {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String description;
@Column(precision=2)
private double price;
@Column(name="CREATED_DATE")
private Date createdDate;
@Version
@Column(name="LAST_UPDATED_TIME")
private Date updatedTime;
@JsonBackReference
@ManyToOne(optional=false, fetch=FetchType.LAZY)
@JoinColumn(name="supplier_id")
private Supplier supplier;
// @JsonManagedReference
@JsonIgnore
@OneToMany(mappedBy="items")
private List<OrderItem> orderItem = new ArrayList<OrderItem>();
public List<OrderItem> getOrderItem() {
return orderItem;
}
public void setOrderItem(List<OrderItem> orderItem) {
this.orderItem = orderItem;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
// @JsonIgnore to ignore this on the json result and avoid recursion
// instead of returning the whole supplier object we return only the id to avoid recursion
public Long getSupplier() {
return supplier.getId();
}
public void setSupplier(Supplier supplier) {
this.supplier = supplier;
}
public Date getUpdatedTime() {
return updatedTime;
}
public Date getCreatedDate() {
return createdDate;
}
public Long getId() {
return id;
}
}
以上是关于java 使用JPA注释的项目实体与供应商建立多对一关系的主要内容,如果未能解决你的问题,请参考以下文章
多对多注释未在选择查询中生成连接
JPA多对多映射
多对多关系 JPA 与实体
更新多对多关系中的实体
Liquibase 和 JPA 注释实体
JPA 与存储在不同数据库中的用户实体的多对多关系