Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
978 views
in Technique[技术] by (71.8m points)

hibernate - @formula to fetch an entity object

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 in thread "main" org.hibernate.MappingException: Could not determine type for: com.test.bean.Article, at table: Menu, for columns: [org.hibernate.mapping.Formula( (select ar from article ar where ar.id=1) )]

exception seems to say that it's not able to resolve type

Updating Entities

   @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 contain mapping.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...