Java Spring中带有hibernate和OneToOne注释的空指针异常
一个setter经常给我NullPointerException,我不知道为什么。这是示意图: 这是主要课程:Java Spring中带有hibernate和OneToOne注释的空指针异常,java,spring,hibernate,nullpointerexception,mapping,Java,Spring,Hibernate,Nullpointerexception,Mapping,一个setter经常给我NullPointerException,我不知道为什么。这是示意图: 这是主要课程: @Entity @Access(AccessType.PROPERTY) public class Registration extends DomainEntity { // Constructors ----------------------------------------------------------- public Registration()
@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标记。如果我在两个类的OneToOne注释中都添加optional=false,那么一切都正常。但是,当我在Registration类中输入optional=true时,Registration类中的setter payment.setRegistrationthis会给我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);
}
}
对不起,你真的能用我们的注册付款吗@OneToOneoptional=true,cascade=CascadeType。都说你可以。因为你没有提供stacktrade,所以我们无能为力。尝试更改付款映射->更改为False可选。请提供一点代码,用于持久化交易的实体代码,这就是我要说的。如果我在两个类中都设置optional=true,则一切正常。正确的代码应该是:optional=true->Registration class optional=false->Payment classEdited以反映此更改。您不想将它们设置为optional对吗?至少有一个是可空的,也就是可选的,我想说,支付是可选的,因为你可以注册而不需要支付,但是你不能在没有注册的情况下支付