【问题标题】:JPA query results in No IdentifierJPA 查询导致无标识符
【发布时间】:2016-12-29 07:18:33
【问题描述】:

我在 spring 中使用 JPA 查询,我的子类扩展了 Baseclass,其中仅包含一个 Id 并且我的子类具有下面给出的 JPA 查询使用的所有变量:

基类:

@MappedSuperclass
@Table(name = "partcost")   
@NoArgsConstructor
@AllArgsConstructor
@Data
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Pg6p0012_01PartCostBaseQueryModel implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    String part_no;
}

子类:

@Entity
@Table(name = "partcost")   
@NoArgsConstructor
@AllArgsConstructor
@Data
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Pg6p0012_01PartCost1QueryModel extends Pg6p0012_01PartCostBaseQueryModel implements Serializable {
private static final long serialVersionUID = 1L;
private String stock_take_cost ;
private String cost_type ;
}

当我在 JPA Query 下方点击时:

@Repository
@Transactional
public interface Pg6p0012_01PartcostRepository extends JpaRepository<Pg6p0012_01PartCostBaseQueryModel, String>  {

@Query(value = "SELECT stock_take_cost,cost_type FROM partcost where part_no = :p_part_no", nativeQuery = true)
public List<Pg6p0012_01PartCost1QueryModel>getPartcost1Result(@Param("p_part_no") String p_part_no);
}

它的抛出错误:没有这样的列名 这很清楚,因为查询只返回一列,但模型有两列。

如何解决这个问题?请提出建议。

【问题讨论】:

  • 当 JPQL 非常适合这种情况时,用户会使用“本机查询”(SQL)。
  • @Neil Stockton:这是一个老项目,我不应该改变结构
  • 尝试用?1 替换您的:p_part_no 并删除@Param 注释?

标签: java sql spring spring-data spring-data-jpa


【解决方案1】:

你让 part_no 瞬态。这意味着它没有保留在数据库中。因此,您没有收到此类列名错误。从 part_no 上方的基类中删除 @Transient。

并且还用

注释你的基类
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)

Hibernate 支持三种基本的继承映射策略:

table per class hierarchy
table per subclass
table per concrete class

【讨论】:

  • 我也尝试删除@Transient,但错误仍然存​​在,因为模型类有两个字段,但查询仅获取一列,因此映射正在创建此错误
  • 我认为您的数据库中没有创建表“table_name”。请检查一下。并且还使用每个类层次结构表、每个子类表、每个具体类表的三种类型之一,因为 hibernate 支持三种基本继承映射策略
  • table_name 实际上是虚拟字段,它是表中存在的其他内容
  • Ok 更新你的真实代码实体基类子类存储库服务全部。然后我会检查。
猜你喜欢
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-24
  • 2012-11-14
  • 2019-08-27
相关资源
最近更新 更多