![](/img/trans.png)
[英]Spring @EnableScheduling and @Schedule annotation don't work
[英]Spring transient annotation work for save but don't work for select
我正在显示带有最后价格寄存器的服务名称的网格表。 当我在Service类中使用@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,则保存并选择工作,但所有价格均为零。 currentPrice将变为空。
如果我删除@Transient,则选择正确。 它以每个服务的最后注册价格加载服务。
但是当我存到银行时,它返回一个错误,表明它找不到currentPrice列(因为它确实不存在)。
我在论坛和Internet上进行了搜索,但没有找到解决方案。
我怎么解决这个问题?
感谢@ M.Prokhorov技巧,我能够如下解决我的问题:
在ServiceDaoImpl类中,我停止使用findAllWithPrice方法来仅使用findAll :
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.