我得到编译时错误
错误

Caused by: java.lang.NullPointerException
    at org.hibernate.hql.internal.ast.HqlSqlWalker.setAlias(HqlSqlWalker.java:1093)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.aliasedSelectExpr(HqlSqlBaseWalker.java:2469)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.constructor(HqlSqlBaseWalker.java:2603)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2315)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2185)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1471)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:571)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:342)
    at com.sun.proxy.$Proxy88.createQuery(Unknown Source)
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:78)
    ... 207 more

当我在下面添加查询时
存储库代码
@Query(value = "select " +
        " new map(CAST(gym.districtId as text) as districtId,"+
        "ge.name as equipments ) " +
        " FROM Gym as gym LEFT JOIN GymEquipment as ge " +
        " Where gym.id=?1 AND CAST(ge.gymId as text) = CAST(gym.guid as text))")
List<Map<String, Object>> findGymById(Integer id);

我搜索这个错误,但没有解决我的问题。下面是额外的信息,你们可以复习一下。
桌子
CREATE TABLE public.gym_equipment
(
    id integer NOT NULL DEFAULT nextval('equipment_id_seq'::regclass),
    name character varying(80) COLLATE pg_catalog."default" NOT NULL,
    image_url character varying COLLATE pg_catalog."default",
    price integer,
    status character varying COLLATE pg_catalog."default",
    created_date timestamp with time zone,
    modified_date timestamp with time zone,
    guid uuid NOT NULL,
    gym_id uuid,
    equipment_id uuid,
    CONSTRAINT gym_equipment_id PRIMARY KEY (guid),
    CONSTRAINT gym_equipment_equipment_fk FOREIGN KEY (equipment_id)
        REFERENCES public.equipment (guid) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT gym_equipment_gym_fk FOREIGN KEY (gym_id)
        REFERENCES public.gym (guid) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
)
WITH (
    OIDS = TRUE
)
TABLESPACE pg_default;

ALTER TABLE public.gym_equipment
    OWNER to postgres;

CREATE TABLE public.gym
(
    id integer NOT NULL DEFAULT nextval('gym_id_seq'::regclass),
    display_name character varying COLLATE pg_catalog."default",
    legal_name character varying COLLATE pg_catalog."default",
    description character varying COLLATE pg_catalog."default",
    gym_type character varying COLLATE pg_catalog."default",
    email character varying COLLATE pg_catalog."default",
    contact_mobile integer,
    average_rating integer,
    instant_bookable boolean,
    gps_longitude bigint,
    gps_latitude bigint,
    status character varying COLLATE pg_catalog."default",
    created_date timestamp with time zone DEFAULT now(),
    modified_date timestamp with time zone DEFAULT now(),
    local_name character varying COLLATE pg_catalog."default",
    review_count integer,
    send_notification boolean,
    url character varying COLLATE pg_catalog."default",
    favourite boolean,
    guid uuid NOT NULL DEFAULT uuid_generate_v4(),
    city_id uuid,
    country_id uuid,
    province_id uuid,
    district_id uuid,
    parent_guid uuid,
    CONSTRAINT gym_pkey PRIMARY KEY (guid),
    CONSTRAINT gym_city_fk FOREIGN KEY (city_id)
        REFERENCES public.city (guid) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT gym_country_fk FOREIGN KEY (country_id)
        REFERENCES public.country (guid) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT gym_district_fk FOREIGN KEY (district_id)
        REFERENCES public.district (guid) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT gym_province_fk FOREIGN KEY (province_id)
        REFERENCES public.province (guid) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
)
WITH (
    OIDS = TRUE
)
TABLESPACE pg_default;

ALTER TABLE public.gym
    OWNER to postgres;

波乔
/*
 * Generated by Telosys Tools Generator ( version 2.1.1 )
 */

@Entity
@Table(name = "gym", schema = "public")
@NamedQueries({
        @NamedQuery(name = "Gym.countAll", query = "SELECT COUNT(x) FROM Gym x")
})
public class Gym implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "guid", nullable = false)
    private String guid;
 @Column(name = "id", nullable = false)
    private Integer id;

    @Column(name = "display_name", length = 2147483647)
    private String displayName;

    @Column(name = "legal_name", length = 2147483647)
    private String legalName;

    @Column(name = "description", length = 2147483647)
    private String description;

    @Column(name = "gym_type", length = 2147483647)
    private String gymType;

    @Column(name = "email", length = 2147483647)
    private String email;

    @Column(name = "contact_mobile")
    private Integer contactMobile;

    @Column(name = "average_rating")
    private Integer averageRating;

    @Column(name = "instant_bookable")
    private Boolean instantBookable;

    @Column(name = "gps_longitude")
    private Long gpsLongitude;

    @Column(name = "gps_latitude")
    private Long gpsLatitude;

    @Column(name = "status", length = 2147483647)
    private String status;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_date")
    private Date createdDate;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "modified_date")
    private Date modifiedDate;

    @Column(name = "local_name", length = 2147483647)
    private String localName;

    @Column(name = "review_count")
    private Integer reviewCount;

    @Column(name = "send_notification")
    private Boolean sendNotification;

    @Column(name = "url", length = 2147483647)
    private String url;

    @Column(name = "favourite")
    private Boolean favourite;

    @Column(name = "parent_guid")
    private String parentGuid;

    // "cityId" (column "city_id") is not defined by itself because used as FK in a link
    // "countryId" (column "country_id") is not defined by itself because used as FK in a link
    // "provinceId" (column "province_id") is not defined by itself because used as FK in a link
    // "districtId" (column "district_id") is not defined by itself because used as FK in a link

    @Column(name = "city_id")
    private String cityId;

    @Column(name = "country_id")
    private String countryId;

    public String getCityId() {
        return cityId;
    }

    public void setCityId(String cityId) {
        this.cityId = cityId;
    }

    public String getCountryId() {
        return countryId;
    }

    public void setCountryId(String countryId) {
        this.countryId = countryId;
    }

    public String getProvinceId() {
        return provinceId;
    }

    public void setProvinceId(String provinceId) {
        this.provinceId = provinceId;
    }

    @Column(name = "province_id")
    private String provinceId;



    //----------------------------------------------------------------------
    // ENTITY LINKS ( RELATIONSHIP )
    //----------------------------------------------------------------------
    @OneToMany(mappedBy = "gym", targetEntity = GymStaff.class)
    private List<GymStaff> listOfGymStaff;

    @ManyToOne
    @JoinColumn(name = "city_id", referencedColumnName = "guid",insertable = false,updatable = false)
    private City city;

    @OneToMany(mappedBy = "gym", targetEntity = GymAmenity.class)
    private List<GymAmenity> listOfGymAmenity;

    @OneToMany(mappedBy = "gym", targetEntity = GymEquipment.class)
    private List<GymEquipment> listOfGymEquipment;

    @OneToMany(mappedBy = "gym", targetEntity = GymImage.class)
    private List<GymImage> listOfGymImage;

    @ManyToOne
    @JoinColumn(name = "country_id", referencedColumnName = "guid",insertable = false,updatable = false)
    private Country country;

    @ManyToOne
    @JoinColumn(name = "province_id", referencedColumnName = "guid",insertable = false,updatable = false)
    private Province province;

    @OneToMany(mappedBy = "gym", targetEntity = Review.class)
    private List<Review> listOfReview;

    @ManyToOne
    @JoinColumn(name = "district_id", referencedColumnName = "guid",insertable = false,updatable = false)
    private District district;

    @Column(name = "district_id")
    private String districtId;


    //----------------------------------------------------------------------
    // CONSTRUCTOR(S)
    //----------------------------------------------------------------------
    public Gym() {
        super();
    }

    //----------------------------------------------------------------------
    // GETTER & SETTER FOR FIELDS
    //----------------------------------------------------------------------

}

最佳答案

这是一个老问题,这不是上述问题的确切答案,更重要的是为什么我收到了与上述相同的异常。希望它能帮助别人。
我写了一个这样的HQL查询。

String queryString = "SELECT new map(sm.abcId as abcId, sm.showName as showName) "
                + " FROM AbcMasterModel as sm "
                + " JOIN axcProfileModel as  apm ON sm.profileId= apm.axcProfileId"
                + " JOIN xyzProfileModel as bp ON sm.band = bp.xyzId " ;

有人更改了模型,profileId字段变成profile
profileId字段前面是这样的
    @Cloumn(name="profile_id")
    private Integer profile;

改变之后
    @ManyToOne
    @JoinColumn(name="profile_id",nullable=true)
    private axcProfileModel profile;

因为我有一个基于该列的连接,所以我得到了上面提到的错误;
at org.hibernate.hql.internal.ast.HqlSqlWalker.setAlias(HqlSqlWalker.java:1093)

所以我修改了我的查询并将其更改为。
String queryString = "SELECT new map(sm.abcId as abcId, sm.showName as showName) "
                + " FROM AbcMasterModel as sm "
                + " JOIN axcProfileModel as apm ON sm.profile = apm.axcProfileId"
                + " JOIN xyzProfileModel as bp ON sm.band = bp.xyzId " ;

这解决了我的问题。

10-07 12:23