Skip to main content
 首页 » 编程设计

java之为什么 JPA 实体在 rowKey 属性为主键时抛出异常

2024年06月03日29开发

我正在使用 JavaEE 构建一个非常简单的 Java 全堆栈应用程序,因此我使用 JSF 和 Prime Faces 6.2 在前端呈现 xthml,在后端使用 EJB、JPA 和 Hibernate 和 postgresql,然而,当我从 Prime Faces 的数据表组件中设置了 rowKey="#{person.id}"。抛出下一个异常。

“00:13:36,307 错误 [io.undertow.request](默认任务 55)UT005023:对/javaee-app/faces/listPersons.xhtml 的异常处理请求:javax.servlet.ServletException ... Caused by :java.lang.NullPointerException"

listPersons.xhtml 
Prime Faces DataTable Component(opening tag and its attributes) 
 
<p:dataTable id="persons" 
                 value="#{personBean.persons}" 
                 var="person" 
                 editable="true" 
                 rowKey="#{person.id}" 
                 selection="#{personBean.personSelected}" 
                 selectionMode="single"> 

The exception thrown that appears when trying to render the page is this.

但是,如果我设置 rowKey="#{person.name}"或什至 rowKey="#{person.email}"而不是 rowKey="#{person.id}"问题就消失了,xthml 页面是正确呈现。

<p:dataTable id="persons" 
             value="#{personBean.persons}" 
             var="person" 
             editable="true" 
             rowKey="#{person.name}" 
             selection="#{personBean.personSelected}" 
             selectionMode="single"> 

with rowKey="#{person.name}" or rowKey="#{person.email}" xhtml page is rendered correctly"

Postgresql Database person

模型/实体

@Entity 
@Table(name = "person") 
@NamedQueries({ 
@NamedQuery(name = "Person.findAll", query = "SELECT p FROM Person p"),  
@NamedQuery(name = "Person.findById", query = "SELECT p FROM Person p  
WHERE p.id = :id") 
, @NamedQuery(name = "Person.findByName", query = "SELECT p FROM  
Person p WHERE p.name = :person_name") 
, @NamedQuery(name = "Person.findByLastname", query = "SELECT p FROM  
Person p WHERE p.lastname = :lastname") 
, @NamedQuery(name = "Person.findByEmail", query = "SELECT p  
FROM Person p WHERE p.email = :email") 
, @NamedQuery(name = "Person.findByPhone", query = "SELECT p  
FROM Person p WHERE p.phone = :phone")}) 
public class Person implements Serializable { 
private static final long serialVersionUID = 1L; 
 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Integer id; 
 
@Basic(optional = false) 
@NotNull 
@Size(min = 1, max = 60) 
@Column(name = "person_name") 
private String name; 
 
@Basic(optional = false) 
@NotNull 
@Size(min = 1, max = 60) 
private String lastname; 
 
@Basic(optional = false) 
@NotNull 
@Size(min = 1, max = 60) 
private String email; 
 
@Size(max = 60) 
private String phone; 
 
@OneToMany(mappedBy = "person", fetch = FetchType.EAGER) 
private List<Users> users; 
 
public Person() { } 
 
public Person(Integer id) { 
    this.id = id; 
} 
 
public int getId() { 
    return id; 
} 
 
public void setId(int id) { 
    this.id = id; 
} 
 
public String getName() { 
    return name; 
} 
 
public void setName(String name) { 
    this.name = name; 
} 
 
public String getLastname() { 
    return lastname; 
} 
 
public void setLastname(String lastname) { 
    this.lastname = lastname; 
} 
 
 
public String getEmail() { 
    return email; 
} 
 
public void setEmail(String email) { 
    this.email = email; 
} 
 
public String getPhone() { 
    return phone; 
} 
 
public void setPhone(String phone) { 
    this.phone = phone; 
} 
 
public List<Users> getUsers() { 
    return users; 
} 
 
public void setUsers(List<Users> users) { 
    this.users = users; 
} 
 
@Override 
public int hashCode() { 
    int hash = 0; 
    hash += (id != null ? id.hashCode() : 0); 
    return hash; 
} 
 
@Override 
public boolean equals(Object object) { 
    if (!(object instanceof Person)) { 
        return false; 
    } 
    Person other = (Person) 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 "Person [id = " + id + ", name=" + name 
            + ", lastName=" + lastname + " email=" + email + ", phone=" + phone + "]"; 
} 

Any idea to solve this problem guys? Thanks in advance

我还添加了应用服务器中生成的错误图像,在我的例子中是 Wildfly 8.2

picture1 Wildfly 8.2

picture2 Wildfly 8.2

请您参考如下方法:

id是Integer类型:

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

当 getter 返回 int 时:

public int getId() { 
    return id; 
} 

更改 getId 以返回整数。