在使用 PostgreSQL 执行 NamedNativeQuery 连接查询时,排除主键后遇到错误。

huangapple go评论72阅读模式
英文:

While executing NamedNativeQuery Join query after excluding the primary key using postgresql facing error

问题

我不需要在结果输出中包含id(主键),但在查询和结果集中排除id后出现错误。org.postgresql.util.PSQLException: 在此结果集中未找到列名id。jpa,因此需要帮助格式化此实体类。

**注意:**可以假设已进行以下方法调用entityManager.createNamedQuery("getPublisherInfoList",PublisherInfoResponseEntity.class).getResultList();

@NamedNativeQueries(value = {
    @NamedNativeQuery(
        name = "getPublisherInfoList",
        query = "SELECT publisher.publisher_name, publisher.contact_name, publisher.contact_email, publisher.contact_phone, publisher.managed_services, " +
                "annual.customers, annual.employees, annual.revenue " +
                "FROM publisher_portal.publisher_informations publisher JOIN publisher_portal.publisher_annual annual " +
                "ON publisher.publisher_name=annual.publisher_name",
        resultSetMapping = "PublisherInfoResponseMappings"
    ),
    
    @NamedNativeQuery(
        name = "getPublisherByName",
        query = "SELECT publisher.publisher_name, publisher.contact_name, publisher.contact_email, publisher.contact_phone, " +
                "publisher.managed_services, annual.customers, annual.employees, annual.revenue " +
                "FROM publisher_portal.publisher_informations publisher JOIN publisher_portal.publisher_annual annual " +
                "ON publisher.publisher_name=annual.publisher_name where publisher.publisher_name= :publisherName",
        resultSetMapping = "PublisherInfoResponseMappings"
    )
})
    
@SqlResultSetMapping(name = "PublisherInfoResponseMappings", entities = {
    @EntityResult(entityClass = PublisherInfoResponseEntity.class, fields = {
        @FieldResult(name = "publisher_name", column = "publisher_name"),
        @FieldResult(name = "contact_name", column = "contact_name"),
        @FieldResult(name = "contact_phone", column = "contact_phone"),
        @FieldResult(name = "contact_email", column = "contact_email"),
        @FieldResult(name = "managed_services", column = "managed_services"),
    })
})

@Entity
@Table(name="publisher_informations",schema="publisher_portal")
public class PublisherInfoResponseEntity {
    
    @Id
    private Long id;
    
    private String publisher_name;
    
    private String contact_name;
    
    private String contact_phone;
    
    private String contact_email;
    
    private String managed_services;
    
    //setters and getters
    
}
英文:

I m not required id (primary key) in the resultant output but after excluding id in query and resultset getting error.org.postgresql.util.PSQLException: The column name id was not found in this ResultSet. jpa
SO need help in formatting this entity class.

Note: Assumption should be made like the below metthod call has been made entityManager.createNamedQuery("getPublisherInfoList",PublisherInfoResponseEntity.class).getResultList();

@NamedNativeQueries(value = {
@NamedNativeQuery(
name = "getPublisherInfoList",
query = "SELECT publisher.publisher_name,publisher.contact_name,publisher.contact_email,publisher.contact_phone,publisher.managed_services,\n" +
"annual.customers,annual.employees,annual.revenue,"
"FROM publisher_portal.publisher_informations publisher JOIN publisher_portal.publisher_annual annual\n" +
"ON publisher.publisher_name=annual.publisher_name",
resultSetMapping = "PublisherInfoResponseMappings"
),
@NamedNativeQuery(
name = "getPublisherByName",
query = "SELECT publisher.publisher_name,publisher.contact_name,publisher.contact_email,publisher.contact_phone,\n" +
"publisher.managed_services,annual.customers,annual.employees,annual.revenue,\n" +
"FROM publisher_portal.publisher_informations publisher JOIN publisher_portal.publisher_annual annual\n" +
"ON publisher.publisher_name=annual.publisher_name where publisher.publisher_name= :publisherName",
resultSetMapping = "PublisherInfoResponseMappings"
)
})
@SqlResultSetMapping(name = "PublisherInfoResponseMappings", entities = {
@EntityResult(entityClass = PublisherInfoResponseEntity.class, fields = {
@FieldResult(name = "publisher_name", column = "publisher_name"),
@FieldResult(name = "contact_name", column = "contact_name"),
@FieldResult(name = "contact_phone", column = "contact_phone"),
@FieldResult(name = "contact_email", column = "contact_email"),
@FieldResult(name = "managed_services", column = "managed_services"),
})
})
@Entity
@Table(name="publisher_informations",schema="publisher_portal")
public class PublisherInfoResponseEntity {
@Id
private Long id;
private String publisher_name;
private String contact_name;
private String contact_phone;
private String contact_email;
private String managed_services;
//setters and getters
}

答案1

得分: 0

看一下,如果你想在结果集中排除某些列,只需在实体类的特定列/字段上使用 @Jsongnore

解决方案如下

在查询和结果集中包含主键,然后在主键上使用 @Jsongnore,以便准备好的结果集中不包含 id 字段。

注意 你可以在字段或者 getters 方法上应用 @Jsongnore

例如

@NamedNativeQueries(value = {
    @NamedNativeQuery(
        name = "getPublisherInfoList",
        query = "SELECT publisher.id,publisher.publisher_name,publisher.contact_name,publisher.contact_email,publisher.contact_phone,publisher.managed_services,\n" +
                "annual.customers,annual.employees,annual.revenue," +
                "FROM publisher_portal.publisher_informations publisher JOIN publisher_portal.publisher_annual annual\n" +
                "ON publisher.publisher_name=annual.publisher_name",
        resultSetMapping = "PublisherInfoResponseMappings"
    ),
    @NamedNativeQuery(
        name = "getPublisherByName",
        query = "SELECT publisher.id,publisher.publisher_name,publisher.contact_name,publisher.contact_email,publisher.contact_phone,\n" +
                "publisher.managed_services,annual.customers,annual.employees,annual.revenue,\n" +
                "FROM publisher_portal.publisher_informations publisher JOIN publisher_portal.publisher_annual annual\n" +
                "ON publisher.publisher_name=annual.publisher_name where publisher.publisher_name= :publisherName",
        resultSetMapping = "PublisherInfoResponseMappings"
    )
})
@SqlResultSetMapping(name = "PublisherInfoResponseMappings", entities = {
    @EntityResult(entityClass = PublisherInfoResponseEntity.class, fields = {
        @FieldResult(name = "id", column = "id"),
        @FieldResult(name = "publisher_name", column = "publisher_name"),
        @FieldResult(name = "contact_name", column = "contact_name"),
        @FieldResult(name = "contact_phone", column = "contact_phone"),
        @FieldResult(name = "contact_email", column = "contact_email"),
        @FieldResult(name = "managed_services", column = "managed_services"),
    })
})
@Entity
@Table(name="publisher_informations",schema="publisher_portal")
public class PublisherInfoResponseEntity {
    @Id
    @Jsongnore
    private Long id;
    
    private String publisher_name;
    
    private String contact_name;
    
    private String contact_phone;
    
    private String contact_email;
    
    private String managed_services;
    
    //setters and getters
}

注意:假设已经使用 entityManager.createNamedQuery(..) 进行了操作。

英文:

See if you want to exclude some column in resulset simply use @Jsongnore on that particular
column/field in your entity class.

So the solution will be

Include Primary key in query and result set but then use @Jsongnore on primary key so that the prepared resultset won’t have the field id.

Note you can apply @Jsongnore either at field or at getters method

For Example

@NamedNativeQueries(value = {
@NamedNativeQuery(
name = "getPublisherInfoList",
query = "SELECT publisher.id,publisher.publisher_name,publisher.contact_name,publisher.contact_email,publisher.contact_phone,publisher.managed_services,\n" +
"annual.customers,annual.employees,annual.revenue,"
"FROM publisher_portal.publisher_informations publisher JOIN publisher_portal.publisher_annual annual\n" +
"ON publisher.publisher_name=annual.publisher_name",
resultSetMapping = "PublisherInfoResponseMappings"
),
@NamedNativeQuery(
name = "getPublisherByName",
query = "SELECT publisher.id,publisher.publisher_name,publisher.contact_name,publisher.contact_email,publisher.contact_phone,\n" +
"publisher.managed_services,annual.customers,annual.employees,annual.revenue,\n" +
"FROM publisher_portal.publisher_informations publisher JOIN publisher_portal.publisher_annual annual\n" +
"ON publisher.publisher_name=annual.publisher_name where publisher.publisher_name= :publisherName",
resultSetMapping = "PublisherInfoResponseMappings"
)
})
@SqlResultSetMapping(name = "PublisherInfoResponseMappings", entities = {
@EntityResult(entityClass = PublisherInfoResponseEntity.class, fields = {
@FieldResult(name = "id", column = "id")
@FieldResult(name = "publisher_name", column = "publisher_name"),
@FieldResult(name = "contact_name", column = "contact_name"),
@FieldResult(name = "contact_phone", column = "contact_phone"),
@FieldResult(name = "contact_email", column = "contact_email"),
@FieldResult(name = "managed_services", column = "managed_services"),
})
})
@Entity
@Table(name="publisher_informations",schema="publisher_portal")
public class PublisherInfoResponseEntity {
@Id
@Jsongnore
private Long id;
private String publisher_name;
private String contact_name;
private String contact_phone;
private String contact_email;
private String managed_services;
//setters and getters
}

note:assume that entityManager.createNamedQuery(..) has been made

huangapple
  • 本文由 发表于 2020年8月19日 21:16:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63487856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定