简体   繁体   English

Spring瞬态注释可用于保存,但不适用于选择

[英]Spring transient annotation work for save but don't work for select

I am showing a grid table whit the service names with the last price register. 我正在显示带有最后价格寄存器的服务名称的网格表。 The problem is happen when I use the @Transient in my Service class. 当我在Service类中使用@Transient时,就会发生问题。

In this case: 在这种情况下:

在此处输入图片说明

I do this: 我这样做:

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

If I leave @Transient both save and select work but ALL prices come as zero. 如果我离开@Transient,则保存并选择工作,但所有价格均为零。 The currentPrice is coming null. currentPrice将变为空。

If I remove @Transient the select comes right. 如果我删除@Transient,则选择正确。 It loads the services with the last registered price for each one. 它以每个服务的最后注册价格加载服务。

But when I save to the bank, it returns an error saying that it did not find the currentPrice column (Because it really doesn't exist). 但是当我存到银行时,它返回一个错误,表明它找不到currentPrice列(因为它确实不存在)。

I searched here in the forum and on the internet but I didn't find a solution. 我在论坛和Internet上进行了搜索,但没有找到解决方案。

How can I solve this problem? 我怎么解决这个问题?

Thanks to the @M.Prokhorov Tip, I was able to solve my problem as follows: 感谢@ M.Prokhorov技巧,我能够如下解决我的问题:

In my ServiceDaoImpl class, I stopped using the findAllWithPrice method to use findAll only: 在ServiceDaoImpl类中,我停止使用findAllWithPrice方法来仅使用findAll

public List<Service> findAll() {
    return this.getCurrentSession().createQuery("from Service", Service.class).getResultList();
}

In my Service class I created a formula to fetch the last recorded price 在服务类中,我创建了一个公式来获取最近记录的价格

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM