@formula获取实体对象 [英] @formula to fetch an entity object

查看:126
本文介绍了@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

线程主"中的异常org.hibernate.MappingException:无法确定类型:com.test.bean.Article,在表:Menu中,对于列:[org.hibernate.mapping.Formula(文章ar,其中ar.id = 1))]

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

更新实体

   @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获取实体对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆