gpt4 book ai didi

java - 当我在springboot API上使用@OneToMany和@ManyToOne时出现错误消息

转载 作者:太空宇宙 更新时间:2023-11-04 09:21:41 27 4
gpt4 key购买 nike

该应用程序具有以下三个类别:货物(职位),功能人员(雇员)和萨拉里奥(工资)。当我注册一名员工时,他需要有薪水和职位。我有两个工作控制器,一个用于员工,一个用于职位,但是我需要他们之间建立联系。

这是Funcionarios(员工)类:

  package name of the package;

import java.sql.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotNull;

@Entity
public class Funcionarios {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@OneToMany
private Long id;

@NotNull
private String nome;
@NotNull
private String email;
@NotNull
private Date nascimento;
@NotNull
private Date dataAdmissao;

public Long getId() {
return id;
}

public String getNome() {
return nome;
}

public String getEmail() {
return email;
}

public Date getNascimento() {
return nascimento;
}

public Date getDataAdmissao() {
return dataAdmissao;
}

public void setId(Long id) {
this.id = id;
}

public void setNome(String nome) {
this.nome = nome;
}

public void setEmail(String email) {
this.email = email;
}

public void setNascimento( Date nascimento) {
this.nascimento = nascimento;
}

public void setDataAdmissao(Date dataAdmissao) {
this.dataAdmissao = dataAdmissao;
}

}


这是货运(职位)类:

package name of the package;

import java.io.Serializable;
import java.time.LocalDateTime;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Cargos implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ManyToOne
@JoinColumn(name = "id")
private Funcionarios id;

private String descricao;
private double valor;
private double gratificacaoTeto;
private LocalDateTime dataCriacao = LocalDateTime.now();

public Funcionarios getId() {
return id;
}

public String getDescricao() {
return descricao;
}

public void setId(Funcionarios id) {
this.id = id;
}

public void setDescricao(String descricao) {
this.descricao = descricao;
}

public void setValor(double valor) {
this.valor = valor;
}

public void setGratificacaoTeto(double gratificacaoTeto) {
this.gratificacaoTeto = gratificacaoTeto;
}

public void setDataCriacao(LocalDateTime dataCriacao) {
this.dataCriacao = dataCriacao;
}

public double getValor() {
return valor;
}

public double getGratificacaoTeto() {
return gratificacaoTeto;
}

public LocalDateTime getDataCriacao() {
return dataCriacao;
}

}


这是Salarios(薪金)课程

package package name of the package;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Salarios {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@ManyToOne
@JoinColumn(name = "id")
private Funcionarios id;

private int data;
private double gratificacao;

@ManyToOne
private Cargos cargos;

@ManyToOne
private Funcionarios funcionario;

public Funcionarios getId() {
return id;
}

public void setId(Funcionarios id) {
this.id = id;
}

public int getData() {
return data;
}

public void setData(int data) {
this.data = data;
}

public double getGratificacao() {
return gratificacao;
}

public void setGratificacao(double gratificacao) {
this.gratificacao = gratificacao;
}

public Cargos getCargos() {
return cargos;
}

public void setCargos(Cargos cargos) {
this.cargos = cargos;
}

public Funcionarios getFuncionario() {
return funcionario;
}

public void setFuncionario(Funcionarios funcionario) {
this.funcionario = funcionario;
}

}


如果有帮助,这里是表格以及它们在数据库中的位置:



当我尝试使用springboot的onetomay和manytoone时,出现以下错误。


  启动ApplicationContext时出错。显示条件报告
  在启用“调试”的情况下重新运行您的应用程序。 2019-10-05 17:04:50.314
  错误6716-[[restartedMain] o.s.boot.SpringApplication
  :应用程序运行失败
  
  org.springframework.beans.factory.BeanCreationException:错误
  在类路径中创建名称为“ entityManagerFactory”的bean
  资源
  [org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]:
  调用init方法失败;嵌套异常为
  org.hibernate.AnnotationException:外键引用
  软件包的名称。功能来自
  name.of.package.Cargos的列数错误。
  应为0
  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778)
  〜[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]在
  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
  〜[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]在
  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
  〜[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]在
  org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:320)
  〜[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]在
  org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
  〜[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]在
  org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
  〜[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]在
  org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
  〜[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]在
  org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105)
  〜[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]在
  org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
  〜[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]在
  org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
  〜[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]在
  org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
  〜[spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]在
  org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744)
  [spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]位于
  org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391)
  [spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]位于
  org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
  [spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]位于
  org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
  [spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]位于
  org.springframework.boot.SpringApplication.run(SpringApplication.java:1204)
  [spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]位于
  包的名称.BolsasApplication.main(BolsasApplication.java:13)
  [classes /:na]位于sun.reflect.NativeMethodAccessorImpl.invoke0(Native
  方法)〜[na:1.8.0_221]在
  sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)
  〜[na:1.8.0_221]在
  sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)
  〜[na:1.8.0_221],位于java.lang.reflect.Method.invoke(未知源)
  〜[na:1.8.0_221]

最佳答案

如果您只需要按员工介绍薪水,我会更好。在实体中同时将双向关系建模为被引用的对象或集合要困难得多。

您定义此方法的方式无效,因为id不能同时为主生成的列和外键。

也许:https://www.javaworld.com/article/2077819/understanding-jpa-part-2-relationships-the-jpa-way.html?page=2
帮助。

关于java - 当我在springboot API上使用@OneToMany和@ManyToOne时出现错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58252154/

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