Hibernate实战——@Formula注解

一 配置文件




     
           
           com.mysql.jdbc.Driver
           
           jdbc:mysql://localhost/hibernate
           
           root
           
           32147
           
           20
           
           1
           
           5000
           
           100
           3000
           2
           true
           
           org.hibernate.dialect.MySQL5InnoDBDialect
           
           update
           
           true
           
           true
           
           
     

二 PO

package org.crazyit.app.domain;

import javax.persistence.*;
import org.hibernate.annotations.Formula;

@Entity
@Table(name="news_inf")
public class News
{
    // 消息类的标识属性
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    // 消息标题
    private String title;
    // 消息内容
    private String content;
    // 消息全部内容,由系统根据公式生成
    @Formula("(select concat(nt.title,nt.content)"
        + "from news_inf nt where nt.id= id)")
    private String fullContent;

    // id的setter和getter方法
    public void setId(Integer id)
    {
        this.id = id;
    }
    public Integer getId()
    {
        return this.id;
    }

    // title的setter和getter方法
    public void setTitle(String title)
    {
        this.title = title;
    }
    public String getTitle()
    {
        return this.title;
    }

    // content的setter和getter方法
    public void setContent(String content)
    {
        this.content = content;
    }
    public String getContent()
    {
        return this.content;
    }

    // fullContent的setter和getter方法
    public void setFullContent(String fullContent)
    {
        this.fullContent = fullContent;
    }
    public String getFullContent()
    {
        return this.fullContent;
    }
}

三 测试

package lee;

import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.service.*;
import org.hibernate.boot.registry.*;
import org.crazyit.app.domain.*;

public class NewsManager
{
    public static void main(String[] args)
        throws Exception
    {
        // 实例化Configuration,
        Configuration conf = new Configuration()
        // 不带参数的configure()方法默认加载hibernate.cfg.xml文件,
        // 如果传入abc.xml作为参数,则不再加载hibernate.cfg.xml,改为加载abc.xml
            .configure();
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(conf.getProperties()).build();
        // 以Configuration实例创建SessionFactory实例
        SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
        // 创建Session
        Session sess = sf.openSession();
        // 开始事务
        Transaction tx = sess.beginTransaction();
//        // 创建消息对象
//        News n = new News();
//        // 设置消息标题和消息内容
//        n.setTitle("疯狂Java联盟成立了");
//        n.setContent("疯狂Java联盟成立了,"
//            + "网站地址http://www.crazyit.org");
//        // 保存消息
//        sess.save(n);
        News n = (News)sess.get(News.class , 1);
        // 输出fullContent值
        System.out.println(n.getFullContent());
        // 提交事务
        tx.commit();
        // 关闭Session
        sess.close();
        sf.close();
    }
}

四 测试结果

1 首先生成数据

Hibernate实战——@Formula注解_第1张图片

2 查询数据

Hibernate:
    select
        news0_.id as id1_0_0_,
        news0_.content as content2_0_0_,
        news0_.title as title3_0_0_,
        (select
            concat(nt.title,
            nt.content)
        from
            news_inf nt
        where
            nt.id= news0_.id) as formula0_0_
    from
        news_inf news0_
    where
        news0_.id=?
疯狂Java联盟成立了疯狂Java联盟成立了,网站地址http://www.crazyit.org

 

你可能感兴趣的:(Hibernate,Hibernate)