Java 具有相同名称的实体,始终为第一个id插入
我有一个Java 具有相同名称的实体,始终为第一个id插入,java,mysql,swing,jpa,Java,Mysql,Swing,Jpa,我有一个jCombobox,在那里我加载所有学生实体。 可以让两名学生拥有相同的姓名,然后将两人都加载到jComboBox 问题是,当我将数据插入数据库时,无论我在jcombobox中选择这两个学生中的哪一个,它总是插入第一个“id”(在这种情况下,同名学生分别是id1和id6),因此,它总是为id为1的学生插入 我肯定错过了什么,有人能帮我找到吗?谢谢 我添加了一些代码,看看现在是否更容易理解,谢谢 实体条件: @Entity @DynamicUpdate(value=true) @Named
jCombobox
,在那里我加载所有学生实体
。
可以让两名学生拥有相同的姓名
,然后将两人都加载到jComboBox
问题是,当我将数据插入数据库时,无论我在jcombobox
中选择这两个学生中的哪一个,它总是插入第一个“id
”(在这种情况下,同名学生分别是id
1和id
6),因此,它总是为id为1的学生插入
我肯定错过了什么,有人能帮我找到吗?谢谢
我添加了一些代码,看看现在是否更容易理解,谢谢
实体
条件:
@Entity
@DynamicUpdate(value=true)
@NamedQueries({
@NamedQuery(name = "Condutor.findAll", query = "SELECT c FROM Condutor c"),
@NamedQuery(name = "Condutor.findByCondutorId", query = "SELECT c FROM Condutor c WHERE c.id = :id"),
@NamedQuery(name = "Condutor.findByCondutorNome", query = "SELECT c FROM Condutor c WHERE c.nome = :nome")})
public class Condutor implements EntidadeBase, Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idCondutor")
private Integer id;
@Column(name = "nomeCondutor")
private String nome;
@ManyToOne
@JoinColumn(name = "setorCondutor")
private Setor setor;
@ManyToOne
@JoinColumn(name = "pessoaCondutor")
private Pessoa pessoa;
@ManyToOne
@JoinColumn(name = "empresaCondutor")
private Empresa empresa;
@ManyToOne
@JoinColumn(name = "statusCondutor")
private Status status;
@OneToMany(mappedBy = "condutor", cascade = CascadeType.ALL)
private List<CondutorInfracao> condInfracoes;
public Condutor() {
}
public Condutor(String nome, Setor setor, Pessoa pessoa, Empresa empresa, Status status) {
this.nome = nome;
this.setor = setor;
this.pessoa = pessoa;
this.empresa = empresa;
this.status = status;
}
/**
* @return the id
*/
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the nome
*/
public String getNome() {
return nome;
}
/**
* @param nome the nome to set
*/
public void setNome(String nome) {
this.nome = nome;
}
/**
* @return the setor
*/
public Setor getSetor() {
return setor;
}
/**
* @param setor the setor to set
*/
public void setSetor(Setor setor) {
this.setor = setor;
}
public Pessoa getPessoa() {
return pessoa;
}
public void setPessoa(Pessoa pessoa) {
this.pessoa = pessoa;
}
public Empresa getEmpresa() {
return empresa;
}
public void setEmpresa(Empresa empresa) {
this.empresa = empresa;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public List<CondutorInfracao> getCondInfracoes() {
return condInfracoes;
}
public void setCondInfracoes(List<CondutorInfracao> condInfracoes) {
this.condInfracoes = condInfracoes;
}
@Override
public int hashCode() {
int hash = 7;
hash = 73 * hash + Objects.hashCode(this.id);
hash = 73 * hash + Objects.hashCode(this.nome);
hash = 73 * hash + Objects.hashCode(this.setor);
hash = 73 * hash + Objects.hashCode(this.pessoa);
hash = 73 * hash + Objects.hashCode(this.empresa);
hash = 73 * hash + Objects.hashCode(this.status);
hash = 73 * hash + Objects.hashCode(this.condInfracoes);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Condutor other = (Condutor) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
if (!Objects.equals(this.nome, other.nome)) {
return false;
}
if (!Objects.equals(this.setor, other.setor)) {
return false;
}
if (!Objects.equals(this.pessoa, other.pessoa)) {
return false;
}
if (!Objects.equals(this.empresa, other.empresa)) {
return false;
}
if (!Objects.equals(this.status, other.status)) {
return false;
}
if (!Objects.equals(this.condInfracoes, other.condInfracoes)) {
return false;
}
return true;
}
@Override
public String toString() {
return this.nome;
}
@Override
public Serializable getId() {
return id;
}
}
savebutton的代码
:
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
CondutorInfracao condutorInfracao = new CondutorInfracao();
condutorInfracao.setCondutor((Condutor) jCCondutor.getSelectedItem());
condutorInfracao.setInfracao((Infracao) jCInfracaoDescr.getSelectedItem());
condutorInfracao.setLinha((Linha) jCLinhaDescr.getSelectedItem());
condutorInfracao.setDataInfracao(dataParaInserir(data));
System.out.println("DATA " +dataParaInserir(data));
Locale brasil = new Locale("pt", "BR");
NumberFormat nf = NumberFormat.getCurrencyInstance(brasil);
String str = valorField.getText();
Number str2 = null;
try {
str2 = nf.parse(str);
} catch (ParseException ex) {
Logger.getLogger(CondutorInfracaoView.class.getName()).log(Level.SEVERE, null, ex);
}
BigDecimal bd = new BigDecimal(str2.doubleValue());
BigDecimal ajusted = bd.setScale(nf.getMaximumFractionDigits(), BigDecimal.ROUND_HALF_UP);
condutorInfracao.setValor_infracao(ajusted);
try {
CondutorInfracaoDao ci = new CondutorInfracaoDao();
ci.save(condutorInfracao);
JOptionPane.showMessageDialog(null, "Gravado !");
modelo.limpaLista();
preencherTabela();
} catch (Exception erro) {
JOptionPane.showMessageDialog(null, "Erro na Gravação:" + erro);
}
refresh();
clearSelection();
}
填充jComboBox
的方法:
public void loadComboCondutorDescr() {
AutoCompleteDecorator.decorate(this.jCCondutor);
CondutorDao condutorDao = new CondutorDao();
List<Condutor> listaCondutores = condutorDao.consultarCondutores();//cria uma lista para receber a lista vindo do dao
for (Condutor set : listaCondutores) {
jCCondutor.addItem(set);
}
}
}在本例中,将组合框列表填充到数据库中
public void loadComboStudent() {
AutoCompleteDecorator.decorate(this.jCStudent);
StudentDao studentDao = new StudentDao();
List<Student> listaStudents = studentDao.consultarStudents();
for (Student set : listaStudents) {
jCStudent.addItem(set);
oc.save(set );
}
}
public void loadComboStudent(){
AutoCompleteDecorator.decoration(this.jcstuden);
StudentDao StudentDao=新StudentDao();
List listaStudents=studentDao.consultarStudents();
用于(学生集:列表学生){
jCStudent.addItem(集);
oc.保存(设置);
}
}
将此列表保存到数据库中。在这种情况下,将组合框列表填充到数据库中
public void loadComboStudent() {
AutoCompleteDecorator.decorate(this.jCStudent);
StudentDao studentDao = new StudentDao();
List<Student> listaStudents = studentDao.consultarStudents();
for (Student set : listaStudents) {
jCStudent.addItem(set);
oc.save(set );
}
}
public void loadComboStudent(){
AutoCompleteDecorator.decoration(this.jcstuden);
StudentDao StudentDao=新StudentDao();
List listaStudents=studentDao.consultarStudents();
用于(学生集:列表学生){
jCStudent.addItem(集);
oc.保存(设置);
}
}
将此列表保存到数据库中。在寻找答案一段时间后,我开始一步一步地进行测试,总是在jComboBox
中选择一个名称,该名称被列出两次(两个都有不同的Id
,(为了清楚起见)),当我将鼠标移离jComboBox
时,选择将转到顶部列出的选项。这就是为什么它总是对相同的对象执行插入
。只列出一次的名称不会出现这种情况。在寻找答案一段时间后,我开始一步一步地进行测试,总是在jComboBox
中选择一个名称,它列出了两次(都有不同的Id
,(为了清楚起见)),当我将鼠标移离jComboBox
时,选择将转到顶部列出的选项。这就是为什么它总是对相同的对象执行插入
。仅列出一次的名称不会出现这种情况。我想您的学生对象需要实现equals(…)
方法,以便具有相同名称的每个学生对象仍然是唯一的。因此,将行ID设置为下拉列表的值,而不是name@camickr,我添加了另一个代码部分。这就是你的意思吗?为了更快地获得更好的帮助,请发布或。我添加了更多信息。我想你的学生对象需要实现equals(…)
方法,这样每个同名的学生对象仍然可以是唯一的。因此,将行ID设置为下拉列表的值,而不是name@camickr,我添加了另一个代码部分。这就是你的意思吗?为了更快地获得更好的帮助,发布一个或。我添加了一些更多的信息。不确定它是否有效,因为我只是在从jComboBox
中选择一个后保存student
,而不是在jComboBox
加载了对象之后。还有很多关于学生
的信息。当你选择这个值时,你会得到所有的组合框值并保存它。这时你说,我只是选择学生
,然后我设置他的电话
,地址
和其他东西。所有这些加上学生
成为occorrecia
。listaStudents
是方法的返回
,而不是我想插入到数据库中的内容。这就是为什么我说我不确定是否会起作用,或者我不理解你说的。这与否有关系?不确定它是否会起作用,因为我只是在我从jComboBox
中选择一个后保存student
,而不是在jComboBox
加载了对象之后。还有很多关于学生
的信息。当你选择这个值时,你会得到所有的组合框值并保存它。这时你说,我只是选择学生
,然后我设置他的电话
,地址
和其他东西。所有这些加上学生
成为occorrecia
。listaStudents
是方法的返回
,而不是我想插入到数据库中的内容。这就是为什么我说我不确定这是否有效,或者,我不理解你说的。这与你的行为有关吗?
public void loadComboStudent() {
AutoCompleteDecorator.decorate(this.jCStudent);
StudentDao studentDao = new StudentDao();
List<Student> listaStudents = studentDao.consultarStudents();
for (Student set : listaStudents) {
jCStudent.addItem(set);
oc.save(set );
}
}