2.Hibernate之联合主键

联合主键

在对象关系映射模型中,使用单独的一个字段作为主键是一种非常好的做法,但是在实际应用中,经常会遇到复合主键的问题,就是使用两个或两个以上的字段作为主键。比如,在一些历史遗留的数据库表中,经常出现复合主键的问题,为了解决这种问题,JPA2.0 中采用的 @EmbeddedId 和 @IdClass 两种方法解决这种问题。它们都需要将用于主键的字段单独放在一个主键类 (primary key class) 里面,必须实现 Serializable 接口,必须拥有无参构造函数。

@EmbeddedId
  • NewsID主键类
import javax.persistence.Embeddable;
import java.io.Serializable;

@Embeddable
public class NewsID implements Serializable {

    private static final long serialVersionUID = -2736473457201541348L;

    private String title;
    private String language;

    public NewsID() {}

    public NewsID(String title, String language) {
        this.title = title;
        this.language = language;
    }

    @Override
    public String toString() {
        return "title: " + this.title + "; language: " + this.language;
    }

    @Override
    public boolean equals(Object obj) {
        return obj != null && this.getClass() == obj.getClass() && this.toString().equals(obj.toString());
    }

    /*set and get*/
}
  • News实体类
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;

@Entity(name = "news")
@Table(name = "t_news")
public class News implements Serializable {

    private static final long serialVersionUID = 7376280230170313244L;

    @EmbeddedId
    private NewsID newsID;

    private String content;

    public News(){}

    public News(String title, String language, String content) {
        this.newsID = new NewsID(title, language);
        this.content = content;
    }

    @Override
    public String toString() {
        return this.newsID.toString() + "; content: " + this.content;
    }
    /*set and get*/
}
@IdClass
  • NewsID主键类
import java.io.Serializable;

/**
 * 使用 @IdClass 这种策略的时候,在复写主键类中的字段的时候务必要保证和主键类中的定义完全一样。
 */
public class NewsID implements Serializable {

    private static final long serialVersionUID = -2736473457201541348L;

    private String title;
    private String language;

    public NewsID() {}

    public NewsID(String title, String language) {
        this.title = title;
        this.language = language;
    }

    @Override
    public String toString() {
        return "title: " + this.title + "; language: " + this.language;
    }

    @Override
    public boolean equals(Object obj) {
        return obj != null && this.getClass() == obj.getClass() && this.toString().equals(obj.toString());
    }
    /*set and get*/
}
  • News实体类
import javax.persistence.*;
import java.io.Serializable;

@Entity(name = "news")
@Table(name = "t_news")
@IdClass(NewsID.class)
public class News implements Serializable {

    private static final long serialVersionUID = 7376280230170313244L;

    @Id
    private String title;

    @Id
    private String language;

    private String content;

    public News(){}

    public News(String title, String language, String content) {
        this.title = title;
        this.language = language;
        this.content = content;
    }

    @Override
    public String toString() {
        return "title: " + this.title + "; language: " + this.language + "; content: " + this.content;
    }
    /*set and get*/
}
  • 测试用例的基类,后续代码中都会用到该类
public class BaseTestCase extends TestCase {

    protected SessionFactory sessionFactory;

    @Override
    public void setUp() throws Exception {
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        this.sessionFactory = configuration.buildSessionFactory();
    }

    @Override
    public void tearDown() throws Exception {
        if(this.sessionFactory != null)
            this.sessionFactory.close();
    }
}
  • 测试代码
public class NewsTest extends BaseTestCase {

        public void testInsert() throws Exception {
        Session session = this.sessionFactory.getCurrentSession();

        session.getTransaction().begin();
        News news = new News("title", "language", "content");
        session.persist(news);
        session.getTransaction().commit();
        session.close();

        session = this.sessionFactory.getCurrentSession();
        session.getTransaction().begin();
        // 如在使用 @EmbeddableId 策略的时候,要使用如下查询语句
        // List<String> newsLanguages = session.createQuery("select n.newsID.language from news n", String.class).getResultList();
        // 如在使用 @IdClass 策略的时候,要使用如下查询语句
        // List<String> newsLanguages = session.createQuery("select n.language from news n", String.class).getResultList();
        // 或者如下查询语句
         List<String> newsLanguages = session.createQuery("select n.id.language from news n", String.class).getResultList();
        ParamUtils.println(newsLanguages);
        session.getTransaction().commit();
        session.close();
    }
}
  • 输出LOG
2017-01-01 15:41:05 DEBUG create table t_news (language varchar(255) not null, title varchar(255) not null, content varchar(255), primary key (language, title))
2017-01-01 15:41:05 DEBUG insert into t_news (content, language, title) values (?, ?, ?)
2017-01-01 15:41:05 DEBUG select news0_.language as col_0_0_ from t_news news0_
2017-01-01 15:41:05 INFO  language
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值