Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
973 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 Answer

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...