gpt4 book ai didi

java - Collection 仅添加,从未阅读

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:56:28 26 4
gpt4 key购买 nike

Netbeans 给出:为 headerFields 的声明配置“使用集合的不平衡读/写”:

package net.bounceme.dur.usenet.model;

import java.io.Serializable;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.persistence.*;

@Entity
public class Article implements Serializable {

private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger(Article.class.getName());
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String subject;
@OneToMany(mappedBy = "article")
private List<HeaderField> headerFields = new ArrayList<>();

public Article() {
}

public Article(Message message) {
try {
subject = message.getSubject();
Enumeration e = message.getAllHeaders();

while (e.hasMoreElements()) {
Header header = (Header) e.nextElement();
@SuppressWarnings("unchecked")
SimpleEntry nameValue = new SimpleEntry(header.getName(), header.getValue());
HeaderField headerField = new HeaderField(nameValue);
headerFields.add(headerField);
}
} catch (MessagingException ex) {
Logger.getLogger(Article.class.getName()).log(Level.SEVERE, null, ex);
}
}

public Long getId() {
return id;
}

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

@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Article)) {
return false;
}
Article other = (Article) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}

@Override
public String toString() {
return subject;
}

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}
}

这让我怀疑是否从 Article 正确建立了与 HeaderField 的关系,或者这个警告表明了什么。

最佳答案

由于您使用的是 JPA 注释的类,其私有(private)字段将通过反射读取,因此此警告是不合适的,您应该简单地禁用它。它只是指你的私有(private)列表没有通过任何方法公开,类中的代码永远不会读取它的内容。

关于java - Collection 仅添加,从未阅读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11736582/

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