gpt4 book ai didi

java - 数据库中的外键始终为空

转载 作者:行者123 更新时间:2023-12-01 16:38:20 25 4
gpt4 key购买 nike

我在 Spring 中遇到此错误:enter image description here当我尝试连接两个表实体时。而且数据库中的外键始终为空,为什么?我的实体类 - 任务、ListeExecJob。请帮助我

任务:

@SuppressWarnings("serial")
@Entity
@Table(name ="task")
public class Task implements Serializable{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;


@Column(name = "nom_job", length = 20,nullable = false)
private String nom_job;

@Column(name = "type_commande", length = 20,nullable = false)
private String type_commande;

@Column(name = "description", length = 100, nullable = false)
private String description;

@Column(name = "script", length = 100, nullable = false)
private String script;

@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm")
@Column(name = "date_execution")
private Date date_execution;

@Column(name = "active")
private boolean active;

@ManyToOne
@JoinColumn(name="id_liste")
private ListeExecJob liste;


public ListeExecJob getListe() {
return liste;
}
public void setListe(ListeExecJob liste) {
this.liste = liste;
}


public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNom_job() {
return nom_job;
}
public void setNom_job(String nom_job) {
this.nom_job = nom_job;
}
public String getType_commande() {
return type_commande;
}
public void setType_commande(String type_commande) {
this.type_commande = type_commande;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getScript() {
return script;
}
public void setScript(String script) {
this.script = script;
}
public Date getDate_execution() {
return date_execution;
}
public void setDate_execution(Date date_execution) {
this.date_execution = date_execution;
}

public boolean getActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}



public Task() {

}
public Task(Integer id, String nom_job, String type_commande, String description, String script,
Date date_execution, boolean active) {
super();
this.id=id;
this.nom_job = nom_job;
this.type_commande = type_commande;
this.description = description;
this.script = script;
this.date_execution = date_execution;
this.active=active ;

}

}

ListeExecJob.java ____________________________________________________

@SuppressWarnings("serial")
@Entity
@Table(name ="liste")
@JsonIgnoreProperties(
value = {"dateCreation"},
allowGetters = true
)
public class ListeExecJob implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idListe")
private int idListe;


@Column(name = "status")
private String status;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "date_creation")
@CreatedDate
private Date date_creation;

@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm")
@JoinColumn(name = "date_execution")
private Date date_execution;

@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm")
@Column(name = "fin_execution")
private Date fin_execution;

@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm")
@Column(name = "next_execution")
private Date next_execution;


@OneToMany(mappedBy="liste",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JsonIgnore
private List<Task> task;


public int getIdListe() {
return idListe;
}

public void setIdListe(int idListe) {
this.idListe = idListe;
}


public Date getDate_creation() {
return date_creation;
}

public void setDate_creation(Date date_creation) {
this.date_creation = date_creation;
}

public Date getDate_execution() {
return date_execution;
}

public void setDate_execution(Date date_execution) {
this.date_execution = date_execution;
}

public Date getFin_execution() {
return fin_execution;
}

public void setFin_execution(Date fin_execution) {
this.fin_execution = fin_execution;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

@JsonIgnore
public List<Task> getTask() {

if (task == null) {
task = new ArrayList<>();
}


return this.task;
}
public void addTask(Task task) {
getTask().add((Task) task);
((Task) task).setListe(this);
}

public void removeTask(Task task) {
getTask().remove(task);
task.setListe(null);
}


@JsonSetter
public void setTask(List<Task> task) {
this.task = task;
}


public ListeExecJob() {

}

public ListeExecJob(int idListe, String status, Date date_creation, Date date_execution, Date b
fin_execution,
Date next_execution) {
super();
this.idListe = idListe;
this.status = status;
this.date_creation = date_creation;
this.date_execution = date_execution;
this.fin_execution = fin_execution;
this.next_execution = next_execution;
}
}

任务服务

@Service
@Transactional(propagation= Propagation.SUPPORTS)
@Primary
public class TaskService {

@Autowired
private TaskRepository repository;

@Autowired
private ListeExecJobService service;

@Autowired
public TaskService(TaskRepository repository) {
super();
this.repository = repository;
}

public List<Task> listAllTask(){
return repository.findAll();
}
public Task addTask(Task task){

ListeExecJob ab = service.getByreferenece(task.getListe().getIdListe());

ab.addTask(task);
return repository.save(task);

/* task.setListe(ab);
System.out.println(task.getListe().getIdListe());
ab.addTask(task);*/
}

public Task updateTask(Integer id , Task task){
Task job1 = new Task();
job1 = task;
job1.setId(id);
return addTask(job1);
}
public void deleteTask(Integer id){
repository.deleteById(id);
}
public Task getByreferenece(Integer id){
return repository.findById(id).isPresent()? repository.findById(id).get():null;
}
}

ListeExecJobService

 @Service
@Transactional
@Primary
public class ListeExecJobService {

@Autowired
private ListeExecJobRepository SJIRepos;

@Autowired
public ListeExecJobService(ListeExecJobRepository SJIRepos) {
super();
this.SJIRepos = SJIRepos;
}

public List<ListeExecJob> listAllListeExecJob(){
return SJIRepos.findAll();
}
public ListeExecJob addListeExecJob(ListeExecJob SJI){
return SJIRepos.save(SJI);
}
public ListeExecJob getByreferenece(Integer idListe){
return SJIRepos.findById(idListe).isPresent()? SJIRepos.findById(idListe).get():null;

}
public void deleteListeExecJob(Integer idListe){
SJIRepos.deleteById(idListe);
}


public ListeExecJob updateListeExecJob(Integer idListe , ListeExecJob sji){
ListeExecJob sji01 = new ListeExecJob();
sji01 = sji;
sji01.setIdListe(idListe);
return addListeExecJob(sji01);
}

public void deleteById(Integer idListe) {
SJIRepos.deleteById(idListe);
}

}

最佳答案

在任务类中尝试@JoinColumn(name="id_liste",referencedColumnName = "idListe"),连接列有问题

关于java - 数据库中的外键始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61913249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com