gpt4 book ai didi

java - JPA 查询结果为 No Identifier

转载 作者:行者123 更新时间:2023-12-01 09:04:44 25 4
gpt4 key购买 nike

我在 spring 中使用 JPA 查询,我的子类扩展了仅包含 Id 的 Baseclass,并且我的子类具有下面给出的 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 查询时:

@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);
}

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

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

最佳答案

您将part_no设置为transient。这意味着它没有保留在数据库中。因此您将收到 no such columns name 错误。从part_no上方的基类中删除@Transient。

并用

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

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

table per class hierarchy
table per subclass
table per concrete class

关于java - JPA 查询结果为 No Identifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41374924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com