简体   繁体   English

Java Bean使用原语或包装

[英]Java Bean use primitive or Wrapper

I have got a Bean named "EmployeeModel" and I also got another Class "EmployeeManager" which has one method of removing (deleting) an employee. 我有一个名为“ EmployeeModel”的Bean,还有另一个类“ EmployeeManager”,它具有一种删除(删除)雇员的方法。

My EmployeeModel: 我的员工模型:

    package at.fh.swenga.employee.model;

import java.util.Date;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;

import org.springframework.format.annotation.DateTimeFormat;

public class EmployeeModel implements Comparable<EmployeeModel> {

    @Min(1)
    private int ssn;

    private String firstName;
    private String lastName;

    private int salary;


    @NotNull(message = "{0} is required")
    @DateTimeFormat(pattern = "dd.MM.yyyy")
    @Past(message = "{0} must be in the past")
    private Date dayOfBirth;

    public EmployeeModel() {
    }


    public EmployeeModel(int ssn, String firstName, String lastName, Integer salary,
            Date dayOfBirth) {
        super();
        this.ssn = ssn;
        this.firstName = firstName;
        this.lastName = lastName;
        this.salary = salary;
        this.dayOfBirth = dayOfBirth;
    }

    public int getSsn() {
        return ssn;
    }

    public void setSsn(int ssn) {
        this.ssn = ssn;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public Date getDayOfBirth() {
        return dayOfBirth;
    }

    public void setDayOfBirth(Date dayOfBirth) {
        this.dayOfBirth = dayOfBirth;
    }

    @Override
    public int compareTo(EmployeeModel o) {
        return ssn - o.getSsn();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ssn;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        EmployeeModel other = (EmployeeModel) obj;
        if (ssn != other.ssn)
            return false;
        return true;
    }
}

My Method in the EmployeeManager Class: 我在EmployeeManager类中的方法:

public boolean remove(int ssn) {
    return employees.remove(new EmployeeModel(ssn, null, null,null, null));
}

As you can see the method only takes the ssn which is of type "int". 如您所见,该方法仅采用类型为“ int”的ssn。 The Problem is when my constructor takes the salary as an int I have to provide it in the method too but I want to avoid this. 问题是当我的构造函数将薪水作为整数时,我也必须在方法中提供薪水,但我想避免这种情况。 As you can see my constructor has an wrapper Integer at the salary field but whenever I am starting my Application and trying to remove an Employee I am getting NullPointerExceptions? 如您所见,我的构造函数在salary字段具有包装器Integer,但是每当我启动我的应用程序并尝试删除Employee时,我都得到NullPointerExceptions吗?

My question is now why I am getting them and if I should just use also a wrapper class at the instantiation like "private Integer salary;" 现在我的问题是为什么我要得到它们,如果我也只在实例化时使用包装类,例如“私有整数薪水”; instead of using the primitve way? 而不是使用原始方式?

1) You should be getting a validation error when calling: 1)调用时,您应该收到验证错误:

new EmployeeModel(ssn, null, null,null, null)

The last parameter is the dayOfBirth, which you have a @NotNull validation check. 最后一个参数是dayOfBirth,您需要进行@NotNull验证检查。

2) If you find yourself calling a constructor like this with null parameters, then you likely have an issue, either with the required parameters needed to construct the object (Should ssn be the only required parameter), or you don't have enough valid data to properly instantiate the object. 2)如果您发现自己使用空参数调用这样的构造函数,那么您可能会遇到问题,要么是构造对象所需的必需参数(应该ssn是唯一的必需参数),要么是没有足够的有效值数据以正确实例化该对象。 If the dateOfBirth should be not null, then you should not be passing null to the constructor. 如果dateOfBirth不为null, dateOfBirth应将null传递给构造函数。

Maybe you should remove the default constructor EmployeeModel() and replace it with one that expects the minimal parameters, like: 也许您应该删除默认的构造函数EmployeeModel() ,并将其替换为需要最小参数的构造函数,例如:

public EmployeeModel(int ssn, Date dayOfBirth)

You shouldn't try to 'fake' the data just to overcome an issue like this. 您不应该仅仅为了克服此类问题而尝试“伪造”数据。 From looking at your code, it seems that SSN is the only real data that must be present to create an EmployeeModel, so maybe you should remove the @NotNull from the dayOfBirth and provide a single-argument constructor that accepts only the ssn. 通过查看您的代码,似乎SSN是创建EmployeeModel时必须存在的唯一真实数据,因此也许您应该从dayOfBirth删除@NotNull并提供仅接受ssn的单参数构造函数。 That should solve your problem. 那应该解决您的问题。 You will want to insure anywhere you do calculations, like on the salary, that you do defensive null checks before attempting the calculation. 您将要确保在进行计算的任何地方(如薪水),在尝试计算之前都要进行防御性的空检查。

public EmployeeModel(int ssn){
    this.ssn = ssn;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM