JPA 搜索抛出错误"An instance of a null PK has been incorrectly provided for this find operation"



>问题:

我正在尝试按站点 ID 进行搜索以从AA_SITE表中获取记录。

但是我在执行调用代码时出现以下异常。知道为什么吗?

javax.ejb.EJBException: EJB Exception: ; nested exception is: 
    java.lang.IllegalArgumentException: An instance of a null PK has been incorrectly provided for this find operation.
    at weblogic.utils.StackTraceDisabled.unknownMethod()
Caused by: java.lang.IllegalArgumentException: An instance of a null PK has been incorrectly provided for this find operation.
    ... 1 more

我的搜索调用代码:

public Site ReadSite(Long siteId) {
    Query query =   em.createNamedQuery("Site.findSiteById", Site.class);
    query.setParameter("siteId",siteId);
    Site site = (Site)query.getSingleResult();    
    return site;
}

实体代码:

package au.sdm.aa.entity;
import au.audit.utility.model.entities.v1.VersionableEntity;  
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;   
@Entity
@NamedQueries({@NamedQuery(name = "Site.findAllSitesByManagerId", 
                            query = "select o from Site o where o.managementId=:managmentId and o.parentSite IS NULL ORDER BY o.id ASC ",
                            hints={
                                @QueryHint(name=QueryHints.BATCH,value="o.colocatedSiteList"),
                                @QueryHint(name=QueryHints.BATCH_TYPE,value="EXISTS")
                            }),
                @NamedQuery(name = "Site.isThisSiteExsists", 
                            query = "select COUNT(o.id) from Site o where o.id =:siteId"),
                @NamedQuery(name = "Site.findSiteById", 
                            query = "select o from Site o where o.id =:siteId")
                })
@SequenceGenerator(name = "siteIdSequenceGen", sequenceName = "AA_SITE_ID_SEQ", allocationSize = 1, initialValue = 1)
@Table(name = "AA_SITE")
public class Site extends VersionableEntity {
    @SuppressWarnings("compatibility:4655825307977473383")
    private static final long serialVersionUID = 1L;
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "siteIdSequenceGen")
    @Id
    @Column(nullable = false)
    private Long id;
    @ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.REFRESH )
    @JoinColumn(name = "PARENT_ID")
    private Site parentSite;
    @OneToMany(mappedBy = "parentSite", fetch=FetchType.LAZY, cascade = {CascadeType.REFRESH,CascadeType.REMOVE})
    @OrderBy("id ASC")
    private List<Site> colocatedSiteList;
    @OneToMany(mappedBy = "documentSite",  cascade = {CascadeType.ALL },orphanRemoval = true, fetch=FetchType.LAZY)
    private List<SiteDocument> siteDocumentList;
   @Column(name = "MANAGER_ID",nullable=false)
    private Long  managementId;
    @OneToMany(mappedBy = "contactSite", cascade = {CascadeType.ALL },orphanRemoval = true, fetch=FetchType.LAZY)
    private List<SiteContact> siteContactList;
    @OneToMany(mappedBy = "classSite",  cascade = {CascadeType.ALL },orphanRemoval = true,fetch=FetchType.LAZY)
    private List<SiteClass> siteClassList;          
    @OneToMany(mappedBy = "appSite", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    private List<Application> applicationList;
    public Site() {
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }                        
}

不确定为什么会发生此问题。 如果有人可以提供任何有效的建议,那将是一个很大的帮助。

您已在 ReadSite 方法中提供了 siteId 参数作为null最终导致触发查询:

select o from Site o where o.id = null

进行此设置:

@Id
@Column(nullable = false)

在实体 ID 上,你将获得生成的异常。

相关内容

最新更新