gpt4 book ai didi

java - 非空属性引用 Hibernate 的 transient 值

转载 作者:行者123 更新时间:2023-12-02 02:27:02 24 4
gpt4 key购买 nike

我有两个有关系的表1:n

多对一员工及部门一名员工只能拥有一个部门。

一对多一个部门有多名员工。

部门表已创建了部门,无需创建新部门来添加员工(仅存在一个)

我的模型:功能/多对一

@Entity(name="Funcionario")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "tipo", length = 1, discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("F")
@Table(name = "funconarios")
public abstract class Funcionario implements Autenticar {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "codFuncionario")
private String codFuncionario;

@ManyToOne
@JoinColumn(name="codDepart", referencedColumnName="codDepart",nullable=false)
private Departamento departamento;

//private Endereco endereco;
@Column(name = "matricula")
private String matricula;
@Column(name = "nome")
private String nome;
@Column(name = "cpf")
private String cpf;
@Column(name = "rg")
private String rg;
@Column(name = "usuario")
private String usuario;
@Column(name = "senha")
private String senha;
@Column(name = "dataEntrada")
private Date dataEntrada;
@Column(name = "dataSaida")
private Date dataSaida;
@Column(name = "dataNascimento")
private Date dataNascimento;
@Column(name = "salario")
private Double salario;

//construtores
public Funcionario() {
}

我的模型部门/一对多:

@Entity
@Table(name="departamento")
public class Departamento {
@Id
@GeneratedValue
@Column(name="codDepart")
private Integer codDepartamento;
@Column(name="nome")
private String nomeDepartamento;

@OneToMany(mappedBy="departamento", cascade= CascadeType.ALL)
private Set<Funcionario> funcionarios;

public Departamento() {
}

public Integer getCodDepartamento() {
return codDepartamento;
}

public void setCodDepartamento(Integer codDepartamento) {
this.codDepartamento = codDepartamento;
}

public String getNomeDepartamento() {
return nomeDepartamento;
}

public void setNomeDepartamento(String nomeDepartamento) {
this.nomeDepartamento = nomeDepartamento;
}

@Override
public String toString() {
return "Departamento [codDepartamento=" + codDepartamento + ", nomeDepartamento=" + nomeDepartamento
+ ", funcionarios=" + funcionarios + "]";
}



}

我的方法保存:

public void save(T entity) {
getSession().saveOrUpdate(entity);
}

我的主要:

public static void main(String[] args) {
Departamento depart = new Departamento();
Funcionario funcionario = new Vendedor();
funcionario.setNome("Maria");
funcionario.setCpf("11111111111");
funcionario.setRg("2222222222");
funcionario.setMatricula("124");
funcionario.setDataNascimento(java.sql.Date.valueOf("2005-02-01"));
funcionario.setDataEntrada(java.sql.Date.valueOf("2005-02-01"));
funcionario.setSalario(987.00);
funcionario.setUsuario("xd");
funcionario.setSenha("xd");
Departamento departamento = new Departamento();
departamento.setNomeDepartamento("teste");
funcionario.setDepartamento(departamento);

GenericDAO absx = new DepartamentoDAO(Funcionario.class);
absx.beginTransaction();
absx.save(funcionario);
absx.commit();

}

我的错误堆栈跟踪:

WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. 
The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
Unsaved transient entity: ([model.Departamento#<null>]) Dependent entities: ([[model.Vendedor#<null>]])
Non-nullable association(s): ([model.Vendedor.departamento]) Exception in thread "main"
org.hibernate.TransientPropertyValueException: Not-null property
references a transient value - transient instance must be saved before
current operation : model.Vendedor.departamento -> model.Departamento

at org.hibernate.action.internal.UnresolvedEntityInsertActions.checkNoUnresolvedActionsAfterOperation(UnresolvedEntityInsertActions.java:122)

at org.hibernate.engine.spi.ActionQueue.checkNoUnresolvedActionsAfterOperation(ActionQueue.java:436)

at org.hibernate.internal.SessionImpl.checkNoUnresolvedActionsAfterOperation(SessionImpl.java:651)
at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:683)

at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:673)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:668)

at dao.GenericDAO.save(GenericDAO.java:54)
at view.eeee.main(eeee.java:35)

最佳答案

你必须级联改变。放入您的 Funcionario 类中:

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="codDepart", referencedColumnName="codDepart",nullable=false)
private Departamento departamento;

关于java - 非空属性引用 Hibernate 的 transient 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57246694/

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