【发布时间】:2021-05-23 20:26:34
【问题描述】:
我最近创建了一个车辆管理系统 系统来源于spring MySQL数据库和服务器端 我想创建另一个表(在运行时自动),它只显示现有表的 2 列。
问题是我做错了什么? 最终目标 - 添加/删除/编辑车辆时,两个表将同步工作且不会发生碰撞 我很乐意为您提供帮助
下面是“汽车”类
import javax.persistence.*;
import java.time.LocalDate;
@Entity
@Table(name = "car")
public class Car {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long carId;
private String licensePlate;
private int carType;
private boolean suv;
private int engineCapacity;
private int year;
private String note;
private int status;
private LocalDate careDate;
private LocalDate editDate;
public Car() {
}
public Car(long carId) {
this.carId = carId;
}
public long getCarId() {
return carId;
}
public void setCarId(long carId) {
this.carId = carId;
}
public String getLicensePlate() {
return licensePlate;
}
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
public int getCarType() {
return carType;
}
public void setCarType(int carType) {
this.carType = carType;
}
public boolean isSuv() {
return suv;
}
public void setSuv(boolean SUV) {
this.suv = SUV;
}
public int getEngineCapacity() {
return engineCapacity;
}
public void setEngineCapacity(int engineCapacity) {
this.engineCapacity = engineCapacity;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public LocalDate getCareDate() {
return careDate;
}
public void setCareDate(LocalDate careDate) {
this.careDate = careDate;
}
public LocalDate getEditDate() {
return editDate;
}
public void setEditDate(LocalDate editDate) {
this.editDate = editDate;
}
}
而 CarType 类只需要创建另一个具有相关列(car_id 和 car_type)的 MySQL 表
package com.example.CarSystemMatanElbaz.model;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Entity
public class CarType {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY,orphanRemoval = true)
@JoinColumn(name= "car_id")
private Car carId;
@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY,orphanRemoval = true)
@JoinColumn(name= "car_type")
private Car carType;
public CarType() {
}
public CarType(long id, Car carId, Car carType) {
this.id = id;
this.carId = carId;
this.carType = carType;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Car getCarId() {
return carId;
}
public void setCarId(Car carId) {
this.carId = carId;
}
public Car getCarType() {
return carType;
}
public void setCarType(Car carType) {
this.carType = carType;
}
}
【问题讨论】:
-
看来你只需要查看
-
“视图”是什么意思?你能更具体吗? @scaisEdge
-
在 db as mysql 中,您可以创建一个不重复表的视图,而只是创建一个查询来公开您需要的列.. 但是'我不在春天,我不能告诉你如何在春天做这个..
-
你想做什么还不清楚。为什么在同一个数据库中有两个具有相同值的表?您的 CarType 实体有两个 Car 类型的属性,一个是 carId,另一个是 carType。这不是圆形的吗?如果你有两个类 A 和 B 并且你想对两者都进行操作,你可以简单地将它放在一个事务中。比如 - 在 A 中插入实体 a - 在 B 中插入实体 b。就是这样。
标签: java mysql sql spring spring-boot