JPA搜索引发错误“此查找操作已错误地提供了一个空PK的实例”

时间:2019-01-21 05:46:59

标签: java sql jpa ejb

问题:

我正在尝试通过站点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;
    }                        
}

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

1 个答案:

答案 0 :(得分:0)

您在siteId方法中提供的ReadSite参数为null,最终导致触发查询的方式为:

select o from Site o where o.id = null

具有此设置:

@Id
@Column(nullable = false)

在实体ID上,您将生成生成的异常。