@formula to get an object object
I want to get an object object using formula annotation
@Formula(value="(select ar from article ar where ar.id=1)")
private Article article;
Tried many paths. Pls help
Exception on thread "main" org.hibernate.MappingException: Could not determine type for: com.test.bean. Article, in table: Menu, for columns: [org.hibernate.mapping.Formula ((select ar from ar article, where ar.id = 1))]
the exception seems to say it cannot resolve the type
Updating objects
@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 also contains a mapping.
+1
source to share
1 answer
I faced the same problem and below is what I used to solve it.
Although I can see the same thing in the comments in the posted code, but it worked for me.
@ManyToOne
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(formula=@JoinFormula(value="(<YOUR_QUERY>)", referencedColumnName="id")),
})
private Article article;
+3
source to share