一个二传手不断给我 NullPointerException,我不知道为什么。这是图表:
这是主要课程:
@Entity
@Access(AccessType.PROPERTY)
public class Registration extends DomainEntity {
// Constructors -----------------------------------------------------------
public Registration() {
super();
}
// Attributes -------------------------------------------------------------
private Date moment;
private String announcement;
@NotNull
@Temporal(TemporalType.TIMESTAMP)
@Past
@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm")
public Date getMoment() {
return moment;
}
public void setMoment(Date moment) {
this.moment = moment;
}
@NotBlank
public String getAnnouncement() {
return announcement;
}
public void setAnnouncement(String announcement) {
this.announcement = announcement;
}
// Relationships ----------------------------------------------------------
private Payment payment;
private Customer owner;
//@Valid
@OneToOne(mappedBy="registration", optional=true, cascade = CascadeType.ALL)
public Payment getPayment() {
return payment;
}
public void setPayment(Payment payment) {
this.payment = payment;
payment.setRegistration(this);
}
@NotNull
@Valid
@ManyToOne(optional = false)
public Customer getOwner() {
return owner;
}
public void setOwner(Customer owner) {
this.owner = owner;
}
}
这是依赖类:
@Entity
@Access(AccessType.PROPERTY)
public class Payment extends DomainEntity {
// Constructors -----------------------------------------------------------
public Payment() {
super();
}
// Attributes -------------------------------------------------------------
private Date moment;
private Double fee;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm")
public Date getMoment() {
return moment;
}
public void setMoment(Date moment) {
this.moment = moment;
}
@Digits(fraction=2, integer=2)
@Min(0)
public Double getFee() {
return fee;
}
public void setFee(Double fee) {
this.fee = fee;
}
// Relationships ----------------------------------------------------------
private Registration registration;
@OneToOne(optional = false, cascade=CascadeType.ALL)
public Registration getRegistration() {
return registration;
}
public void setRegistration(Registration registration) {
this.registration = registration;
}
}
填充这些类的信息来自一个form:form
标签。optional = false
如果我将 ( ) 放在两个类的 OneToOne 注释中,一切正常。但是,当我放入optional=true
注册类时,注册类中的设置器“ payment.setRegistration(this)
”给了我NullPointerException
,但我完全确定付款不为空,因为我在保存注册之前将其打印到屏幕上。那有什么问题呢?
编辑:
添加了存储库和服务层代码:
存储库:
@Repository
public interface RegistrationRepository extends JpaRepository<Registration, Integer> {
}
服务:
@Service
@Transactional
public class RegistrationService {
// Managed repository -----------------------------------------------------
@Autowired
private RegistrationRepository registrationRepository;
// Supporting services ----------------------------------------------------
@Autowired
private CustomerService customerService;
// Constructors -----------------------------------------------------------
public RegistrationService() {
super();
}
// Simple CRUD methods ----------------------------------------------------
public Registration createRegistration() {
Registration result;
Date moment;
Customer owner;
moment = new Date(System.currentTimeMillis() - 1);
owner = customerService.findByPrincipal();
result = new Registration();
result.setMoment(moment);
result.setOwner(owner);
owner.addRegistration(result);
return result;
}
public void save(Registration registration) {
Assert.notNull(registration);
registrationRepository.save(registration);
}
public void delete(Registration registration) {
Assert.notNull(registration);
Assert.isTrue(registration.getId() != 0);
registrationRepository.delete(registration);
}
// Business methods -------------------------------------------------------
public Collection<Registration> findByPrincipal() {
Collection<Registration> result;
Customer customer;
customer = customerService.findByPrincipal();
result = customer.getRegistrations();
return result;
}
public void registerPrincipal(Registration registration) {
Assert.notNull(registration);
Customer owner;
Date paymentMoment;
Date registrationMoment;
owner = customerService.findByPrincipal();
registration.setOwner(owner);
save(registration);
}
}