@formula获取实体对象

时间:2014-12-14 08:52:16

标签: hibernate hibernate-mapping

我想使用公式注释

获取实体对象
@Formula(value="(select ar from article ar where ar.id=1)")
private Article article;

尝试了很多方法。请帮忙

  

线程“main”中的异常org.hibernate.MappingException:无法确定:com.test.bean.Article的类型,在表:Menu,对于列:[org.hibernate.mapping.Formula((select ar from article ar其中ar.id = 1))]

异常似乎说它无法解析类型

更新实体

   @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还包含映射。

1 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,以下就是我用来解决它的问题 虽然我可以在发布的代码中看到相同的内容,但它对我有用。

@ManyToOne
    @JoinColumnsOrFormulas({
            @JoinColumnOrFormula(formula=@JoinFormula(value="(<YOUR_QUERY>)", referencedColumnName="id")),
    })
private Article article;