本文介绍了@formula获取实体对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用公式注释来获取实体的对象
I wan to fetch an object of entity using formula annotation
@Formula(value="(select ar from article ar where ar.id=1)")
private Article article;
尝试了很多方式.请帮助
Tried lot's of way. Pls help
异常似乎表明它无法解析类型
exception seems to say that it's not able to resolve type
更新实体
@Entity
public class Menu {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
// @Formula(value="article.id=1")
// @JoinColumnOrFormula(formula=@JoinFormula("(select ar from article ar where ar.id=1)"),column=@JoinColumn(insertable=false,updatable=false))
// @Transient
// @OneToOne(fetch=FetchType.EAGER,targetEntity=Article.class)
// @JoinColumn(insertable=false,updatable=false,columnDefinition="(select ar from article ar where ar.id=1)")
@Formula(value = "(SELECT ar.id from article ar where ar.id=1)")
// @OneToOne(targetEntity=Article.class,fetch=FetchType.EAGER)
// @Fetch(FetchMode.SELECT)
private Article article;
@Formula(value = "(SELECT count(*) from article ar where ar.id=1)")
private Integer count;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Article getArticle() {
return article;
}
public void setArticle(Article article) {
this.article = article;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
@Override
public String toString() {
return "Menu [id=" + id + ", article=" + article + ", count=" + count
+ "]";
}
}
@Entity(name = "article")
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String uid;
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Article [id=" + id + ", uid=" + uid + ", content=" + content
+ "]";
}
}
cfg还包含映射.
推荐答案
我遇到了同样的问题,下面是我用来解决它的方法.
虽然我可以在发布的代码中的注释中看到相同的内容,但是它对我有用.
I faced the same issue and below is what I used to solve it.
Though I can see the same in comments in the code posted by but it worked for me.
@ManyToOne
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(formula=@JoinFormula(value="(<YOUR_QUERY>)", referencedColumnName="id")),
})
private Article article;
这篇关于@formula获取实体对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!