org.hibernate.PropertyValueException:not-null属性引用一对一关系中的null或transient值

public class Site implements java.io.Serializable { private Integer id; private String siteNumber; private String siteId; private SiteClass siteClass; private SiteCategory siteCategory; private Boolean disabled = false; private ContractParent contractParent; private Boolean criticalSite = false; private Date dateOfHandover; private Date onlineDate; private Location location; private String name; private String displayName; private String displayWithSiteNum; //One to One private SiteSecurity siteSecurity; private SiteDetails details; private SiteAvr avr; private SiteRectifier rectifier; private Cycle pmCycle; public Site() { } public Site(Integer siteId) { this.id = siteId; } @GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "location")) @Id @GeneratedValue(generator = "generator") @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name = "siteID", length = 45) public String getSiteId() { return siteId; } public void setSiteId(String siteId) { this.siteId = siteId; } @OneToOne(fetch = FetchType.LAZY,cascade=CascadeType.ALL) @PrimaryKeyJoinColumn public Location getLocation() { return location; } public void setLocation(Location location) { this.location = location; } @OneToOne( fetch = FetchType.LAZY, mappedBy = "site") @PrimaryKeyJoinColumn public SiteSecurity getSiteSecurity() { return siteSecurity; } public void setSiteSecurity(SiteSecurity siteSecurity) { this.siteSecurity = siteSecurity; } @Column(name = "siteNumber", length = 45) public String getSiteNumber() { return siteNumber; } public void setSiteNumber(String siteNumber) { this.siteNumber = siteNumber; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "siteCategory") public SiteCategory getSiteCategory() { return siteCategory; } public void setSiteCategory(SiteCategory siteCategory) { this.siteCategory = siteCategory; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "site") @LazyCollection(LazyCollectionOption.EXTRA) public Set getSiteColocates() { return siteColocates; } public void setSiteColocates(Set siteColocates) { this.siteColocates = siteColocates; } @OneToMany( fetch = FetchType.LAZY, mappedBy = "site") public Set getBatteryBanks() { return batteryBanks; } public void setBatteryBanks(Set batteryBanks) { this.batteryBanks = batteryBanks; } @Column(name = "disabled") public Boolean getDisabled() { return disabled; } public void setDisabled(Boolean disabled) { this.disabled = disabled; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "contract") public ContractParent getContractParent() { return contractParent; } public void setContractParent(ContractParent contract) { this.contractParent = contract; } @OneToOne( fetch = FetchType.LAZY, mappedBy = "site") @PrimaryKeyJoinColumn public SiteDetails getDetails() { return details; } public void setDetails(SiteDetails details) { this.details = details; } @OneToOne( fetch = FetchType.LAZY, mappedBy = "site") @PrimaryKeyJoinColumn public SiteAvr getAvr() { return avr; } public void setAvr(SiteAvr avr) { this.avr = avr; } @OneToOne( fetch = FetchType.LAZY, mappedBy = "site") @PrimaryKeyJoinColumn public SiteRectifier getRectifier() { return rectifier; } public void setRectifier(SiteRectifier rectifier) { this.rectifier = rectifier; } } 

这是位置等级

 package domain; import static javax.persistence.GenerationType.IDENTITY; import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; import javax.persistence.UniqueConstraint; @Entity @Table(name = "wrh_location", catalog = "",uniqueConstraints = @UniqueConstraint(columnNames = { "name", "parentLocation", "locationType" })) public class Location implements java.io.Serializable { private Integer id; private String name; private Warehouse warehouse; private LocationType locationType; private Location parentLocation; public Location() { } public Location(Integer id) { this.id = id; } public Location(String name,LocationType locationType) { this.name = name; this.locationType = locationType; } public Location(String name,LocationType locationType,Location parentLocation ) { this.name = name; this.locationType = locationType; this.parentLocation = parentLocation; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "name", unique = true, length = 64) public String getName() { return this.name; } public void setName(String name) { this.name = name; } @OneToOne(fetch = FetchType.LAZY, mappedBy = "location") @PrimaryKeyJoinColumn public Warehouse getWarehouse() { return warehouse; } public void setWarehouse(Warehouse warehouse) { this.warehouse = warehouse; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "locationType") public LocationType getLocationType() { return locationType; } public void setLocationType(LocationType locationType) { this.locationType = locationType; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "parentLocation") public Location getParentLocation() { return parentLocation; } public void setParentLocation(Location parentLocation) { this.parentLocation = parentLocation; } } 

在解决了一对一的n + 1问题之后,这个问题就出现了

引起:org.hibernate.PropertyValueException:not-null属性引用null或transient值:domain.Location.warehouse at org.hibernate.engine.Nullability.checkNullability(Nullability.java:100)〜[hibernate3.jar:3.6。 7.Final]