jpa-@sqlresultsetmapping如何使用

093gszye  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(436)

我在使用时面临一个问题 @SqlResultSetMapping .
我有两个实体类叫做 PackageMappingModel 以及 UsersEntity . 我尝试通过连接两个表并尝试将结果Map到这些实体来获得结果集。
我用的是 @SqlResultSetMappingPackageMappingModel 分类如下

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.FetchType;
import javax.persistence.FieldResult;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedNativeQuery;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;

@NamedNativeQuery (
        name = "PackageEntityQuery",
        query = "select pd.pkg_id, pd.pkg_name, pd.description, pd.creation_date as creat_date, pd.modified_date, 
                pd.user_id, usr.uid, usr.fname as first_name, usr.lname as last_name
                from package_schema.my_packages pd 
                inner join usersschema.users usr on pd.user_id = usr.uid
                where pd.user_id = 'abcduser'"
        resultSetMapping = "PackageListMapping"
    )
@SqlResultSetMapping(
        name = "PackageListMapping",
        entities = {
            @EntityResult (
                    entityClass = PackageMappingModel.class,
                    fields = {
                            @FieldResult (name = "packageId", column = "pkg_id"),
                            @FieldResult (name = "packageName", column = "pkg_name"),
                            @FieldResult (name = "createDate", column = "creat_date"),
                            @FieldResult (name = "pkgModifiedDate", column = "modified_date"),
                            @FieldResult (name = "pkgDescription", column = "description"),
                            @FieldResult (name = "pkgUserId", column = "user_id"),
                    }
                ),
            @EntityResult (
                    entityClass = UsersEntity.class,
                    fields = {
                            @FieldResult (name = "pkgUserId", column = "uid"),
                            @FieldResult (name = "firstName", column = "first_name"),
                            @FieldResult (name = "lastName", column = "last_name")
                    }
                ),
        }
    )

@Entity
@Table(name = "my_packages", schema = "package_schema")
public class PackageMappingModel {

    @Id
    private Long packageId;
    private String packageName;
    private Date createDate;
    private Date pkgModifiedDate;
    private String pkgDescription;
    private String pkgUserId;

    @ManyToOne(fetch = FetchType.EAGER, optional = true)
    @JoinColumn(name = "uid", nullable = true)
    private UsersEntity users;

    /*
        -------
        Getter and setters for the properties
    */
}

我的 UsersEntity

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "users", schema="usersschema")
public class UsersEntity {

    @Id
    @Column(name = "uid")
    private String pkgUserId;
    private String firstName;
    private String lastName;

    /*
        -------
        Getter and setters for the properties
    */
}

我用的是 PackageEntityQuery 如下

List<PackageMappingModel> packageList = em.createNamedQuery("PackageEntityQuery")
                .setParameter("userId", userId)
                .getResultList();

当我试图得到结果集时,我得到了下面的异常

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query] with root cause
org.postgresql.util.PSQLException: The column name uid27_11_0_ was not found in this ResultSet.
    at org.postgresql.jdbc.PgResultSet.findColumn(PgResultSet.java:2593)
    at org.postgresql.jdbc.PgResultSet.getString(PgResultSet.java:2467)
    at com.zaxxer.hikari.pool.HikariProxyResultSet.getString(HikariProxyResultSet.java)
    at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$2.doExtract(VarcharTypeDescriptor.java:62)
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:243)
    at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:329)
    at org.hibernate.type.ManyToOneType.hydrate(ManyToOneType.java:184)
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:3012)
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1746)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1672)
    at org.hibernate.loader.Loader.getRow(Loader.java:1561)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:731)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:990)
    at org.hibernate.loader.Loader.doQuery(Loader.java:948)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)
    at org.hibernate.loader.Loader.doList(Loader.java:2689)
    at org.hibernate.loader.Loader.doList(Loader.java:2672)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2506)

更新:正在动态创建uid27\u 11\u 0\列。如何避免这种情况。
提前谢谢!!!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题