Java Spring瞬态注释适用于“保存”,但不适用于“选择”

Java Spring瞬态注释适用于“保存”,但不适用于“选择”,java,hibernate,jpa,Java,Hibernate,Jpa,我正在显示一个网格表,其中包含带有最后一个价格寄存器的服务名称。问题是在我的服务类中使用@Transient时发生的 在这种情况下: 我这样做: public List<Service> findAllWithPrice() { NativeQuery<Service> query = this.getCurrentSession() .createSQLQuery(

我正在显示一个网格表,其中包含带有最后一个价格寄存器的服务名称。问题是在我的服务类中使用@Transient时发生的

在这种情况下:

我这样做:

public List<Service> findAllWithPrice() {
    NativeQuery<Service> query = 
            this.getCurrentSession()
                .createSQLQuery(
                    "select s.*, FORMAT((select ps.price from priceServices ps where ps.idService = s.id order by ps.dateRegister DESC limit 1),2) as currentPrice from service s");
    query.addEntity( Service.class );

    return query.getResultList();
}

/*********************/

@Entity
@Table(name = "service")
public class Service  {
    /****/
    @Transient
    private String currentPrice;

    public String getCurrentPrice() {
        if ( currentPrice == null ) {
            return "$ 0.0";
        }
        return currentPrice;
    }
}
如果我离开@Transient,则保存并选择工作,但所有价格均为零。当前价格为零

如果我删除@Transient,选择就会正确。它加载每个服务的最后注册价格

但是当我保存到银行时,它返回一个错误,说它没有找到currentPrice列,因为它确实不存在

我在论坛和互联网上搜索了这里,但没有找到解决方案


如何解决此问题?

多亏了@M.Prokhorov提示,我可以按如下方式解决我的问题:

在我的ServiceDaoImpl类中,我停止使用findAllWithPrice方法来仅使用findAll:


我猜你指的是JPA,不是Spring。float也不能为null。请编辑该问题,使其成为一个问题。谢谢!我会编辑这个帖子@M.ProkhorovOk,这样更好。现在,@Transient表示该字段是。您实际需要的是JPA的计算字段,如这里所述:如果您有一个字段,您应该这样做。最好有一个正确答案的问题,它会更好地出现在搜索中。
public List<Service> findAll() {
    return this.getCurrentSession().createQuery("from Service", Service.class).getResultList();
}
@Entity
@Table(name = "service")
public class Service  {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    /****/
    @Formula("(FORMAT((select ps.price from priceServices ps where ps.idService = id order by ps.dataRegister DESC limit 1),2))")
    private String currentPrice;

    public String getCurrentPrice() {
        if ( currentPrice == null ) {
            return "$ 0.0";
        }
        return currentPrice;
    }
}