使用类一级的 @SecondaryTable@SecondaryTables注解可以实现单个实体到多个表的映射. 使用 @Column或者 @JoinColumn注解中的table参数可指定某个列所属的特定表.

使用类一级的 @SecondaryTable@SecondaryTables注解可以实现单个实体到多个表的映射. 使用 @Column或者 @JoinColumn注解中的table参数可指定某个列所属的特定表.

用例代码如下:

  • 数据库DDL语句

1,CAT表

  1. create table CAT
  2. (
  3. id VARCHAR2(32 CHAR) not null,
  4. create_time TIMESTAMP(6),
  5. update_time TIMESTAMP(6),
  6. cat_name VARCHAR2(255 CHAR),
  7. first_name VARCHAR2(255 CHAR),
  8. last_name VARCHAR2(255 CHAR),
  9. version NUMBER(10) not null
  10. )

2,CAT_INFO表

  1. create table CAT_INFO
  2. (
  3. address VARCHAR2(255 CHAR),
  4. birthday TIMESTAMP(6),
  5. cat_id VARCHAR2(32 CHAR) not null
  6. )

3,CAT_INFO_2表

  1. create table CAT_INFO_2
  2. (
  3. gender NUMBER(10),
  4. mobile VARCHAR2(255 CHAR),
  5. cat_id VARCHAR2(32 CHAR) not null
  6. )
  • hibernate.cfg.xml
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE hibernate-configuration
  3. PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <!-- 数据库驱动配置 -->
  8. <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
  9. <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
  10. <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
  11. <property name="connection.username">wxuatuser</property>
  12. <property name="connection.password">xlh</property>
  13. <property name="show_sql">true</property>
  14. <!-- 自动执行DDL属性是update,不是true -->
  15. <property name="hbm2ddl.auto">update</property>
  16. <!-- hibernate实体类 -->
  17.  
  18. <mapping class="a6_SecondaryTable.CatSecondaryTables"/>
  19.  
  20. </session-factory>
  21. </hibernate-configuration>
  • java类

实体类 - 基类

  1. package model;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. import javax.persistence.Column;
  5. import javax.persistence.GeneratedValue;
  6. import javax.persistence.Id;
  7. import javax.persistence.MappedSuperclass;
  8. import org.hibernate.annotations.GenericGenerator;
  9. /**
  10. * 实体类 - 基类
  11. */
  12. @MappedSuperclass
  13. public class BaseEntity implements Serializable {
  14.  
  15. private static final long serialVersionUID = -6718838800112233445L;
  16.  
  17. private String id;// ID
  18. private Date create_time;// 创建日期
  19. private Date update_time;// 修改日期
  20. @Id
  21. @Column(length = 32, nullable = true)
  22. @GeneratedValue(generator = "uuid")
  23. @GenericGenerator(name = "uuid", strategy = "uuid")
  24. public String getId() {
  25. return id;
  26. }
  27. public void setId(String id) {
  28. this.id = id;
  29. }
  30. @Column(updatable = false)
  31. public Date getCreate_time() {
  32. return create_time;
  33. }
  34. public void setCreate_time(Date create_time) {
  35. this.create_time = create_time;
  36. }
  37. public Date getUpdate_time() {
  38. return update_time;
  39. }
  40. public void setUpdate_time(Date update_time) {
  41. this.update_time = update_time;
  42. }
  43. @Override
  44. public int hashCode() {
  45. return id == null ? System.identityHashCode(this) : id.hashCode();
  46. }
  47. @Override
  48. public boolean equals(Object obj) {
  49. if (this == obj) {
  50. return true;
  51. }
  52. if (obj == null) {
  53. return false;
  54. }
  55. if (getClass().getPackage() != obj.getClass().getPackage()) {
  56. return false;
  57. }
  58. final BaseEntity other = (BaseEntity) obj;
  59. if (id == null) {
  60. if (other.getId() != null) {
  61. return false;
  62. }
  63. } else if (!id.equals(other.getId())) {
  64. return false;
  65. }
  66. return true;
  67. }
  68. }

实体类

  1. package a6_SecondaryTable;
  2. import java.util.Date;
  3. import javax.persistence.AttributeOverride;
  4. import javax.persistence.AttributeOverrides;
  5. import javax.persistence.Column;
  6. import javax.persistence.Embedded;
  7. import javax.persistence.Entity;
  8. import javax.persistence.PrimaryKeyJoinColumn;
  9. import javax.persistence.SecondaryTable;
  10. import javax.persistence.SecondaryTables;
  11. import javax.persistence.Table;
  12. import javax.persistence.Version;
  13. import model.BaseEntity;
  14. import org.hibernate.annotations.DynamicInsert;
  15. import org.hibernate.annotations.DynamicUpdate;
  16.  
  17. @Entity
  18. @DynamicInsert
  19. @DynamicUpdate
  20. @Table(name="CAT")
  21. @SecondaryTables( value = {
  22. @SecondaryTable(name="CAT_INFO",pkJoinColumns=@PrimaryKeyJoinColumn(name="CAT_ID")),
  23. @SecondaryTable(name="CAT_INFO_2",pkJoinColumns=@PrimaryKeyJoinColumn(name="CAT_ID")) })
  24. public class CatSecondaryTables extends BaseEntity{
  25. /**
  26. * 实体类
  27. */
  28. private static final long serialVersionUID = -2776330321385582872L;
  29.  
  30. private String cat_name;
  31. private Name name;
  32. private int version;
  33. private String address;
  34. private Date birthday;
  35. private Integer gender;
  36. private String mobile;
  37.  
  38. @Version
  39. public int getVersion() {
  40. return version;
  41. }
  42.  
  43. public void setVersion(int version) {
  44. this.version = version;
  45. }
  46.  
  47. public String getCat_name() {
  48. return cat_name;
  49. }
  50.  
  51. public void setCat_name(String cat_name) {
  52. this.cat_name = cat_name;
  53. }
  54.  
  55. @Embedded
  56. @AttributeOverrides({
  57. @AttributeOverride(name = "first_name", column = @Column(name = "first_name")),
  58. @AttributeOverride(name = "last_name", column = @Column(name = "last_name")) })
  59. public Name getName() {
  60. return name;
  61. }
  62.  
  63. public void setName(Name name) {
  64. this.name = name;
  65. }
  66. @Column(name="ADDRESS", table="CAT_INFO")
  67. public String getAddress() {
  68. return address;
  69. }
  70.  
  71. public void setAddress(String address) {
  72. this.address = address;
  73. }
  74. @Column(name="BIRTHDAY", table="CAT_INFO")
  75. public Date getBirthday() {
  76. return birthday;
  77. }
  78.  
  79. public void setBirthday(Date birthday) {
  80. this.birthday = birthday;
  81. }
  82. @Column(name="GENDER", table="CAT_INFO_2")
  83. public Integer getGender() {
  84. return gender;
  85. }
  86.  
  87. public void setGender(Integer gender) {
  88. this.gender = gender;
  89. }
  90. @Column(name="MOBILE", table="CAT_INFO_2")
  91. public String getMobile() {
  92. return mobile;
  93. }
  94.  
  95. public void setMobile(String mobile) {
  96. this.mobile = mobile;
  97. }
  98. }

Dao

  1. package daoUtil;
  2. import org.hibernate.HibernateException;
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.Transaction;
  6. import org.hibernate.cfg.Configuration;
  7. import org.hibernate.service.ServiceRegistry;
  8. import org.hibernate.service.ServiceRegistryBuilder;
  9.  
  10. public class HibernateUtil {
  11.  
  12. private static final SessionFactory sessionFactory;
  13.  
  14. static {
  15. try {
  16. Configuration cfg = new Configuration().configure();
  17. ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
  18. .applySettings(cfg.getProperties()).buildServiceRegistry();
  19. sessionFactory = cfg.buildSessionFactory(serviceRegistry);
  20. } catch (Throwable ex) {
  21. throw new ExceptionInInitializerError(ex);
  22. }
  23. }
  24.  
  25. public static Session getSession() throws HibernateException {
  26. return sessionFactory.openSession();
  27. }
  28.  
  29. public static Object save(Object obj){
  30. Session session = HibernateUtil.getSession();
  31. Transaction tx = null;
  32. try {
  33. tx = session.beginTransaction();
  34. session.save(obj);
  35. tx.commit();
  36. } catch (RuntimeException e) {
  37. if (tx != null) {
  38. tx.rollback();
  39. }
  40. throw e;
  41. } finally {
  42. session.close();
  43. }
  44. return obj;
  45. }
  46.  
  47. public static void delete(Class<?> clazz,String id){
  48. Session session = HibernateUtil.getSession();
  49. Transaction tx = null;
  50. try {
  51. tx = session.beginTransaction();
  52. Object obj = session.get(clazz,id);
  53. session.delete(obj);
  54. tx.commit();
  55. } catch (RuntimeException e) {
  56. if (tx != null) {
  57. tx.rollback();
  58. }
  59. throw e;
  60. } finally {
  61. session.close();
  62. }
  63. }
  64. }

main

  1. package a6_SecondaryTable;
  2. import java.util.Date;
  3. import daoUtil.HibernateUtil;
  4.  
  5. public class Test_SecondaryTables {
  6.  
  7. public static void main(String[] args) {
  8.  
  9. Name name = new Name();
  10. CatSecondaryTables cat = new CatSecondaryTables();
  11. cat.setCat_name("test7SecondaryTables2");
  12. cat.setName(name);
  13. cat.setAddress("中华人民共和国");
  14. cat.setBirthday(new Date());
  15. cat.setGender(1);
  16. cat.setMobile("13012345678");
  17.  
  18. HibernateUtil.save(cat);
  19. System.out.println(cat.getId());
  20.  
  21. CatSecondaryTables cat1 = (CatSecondaryTables)HibernateUtil.getSession().get(CatSecondaryTables.class, cat.getId());
  22. System.out.println(cat1.getId());
  23. }
  24. }

环境:JDK1.6,MAVEN,tomcat,eclipse

源码地址:http://files.cnblogs.com/files/xiluhua/hibernate%40SecondaryTables.rar

Hibernate,JPA注解@SecondaryTables的更多相关文章

  1. Java、Hibernate(JPA)注解大全

    1.@Entity(name=”EntityName”) 必须,name为可选,对应数据库中一的个表 2.@Table(name=””,catalog=””,schema=””) 可选,通常和@Ent ...

  2. JPA注解@SecondaryTables 实现一个实体映射多张数据库表

    参考:http://jingpin.jikexueyuan.com/article/46978.html Annotation Type SecondaryTables(参考:https://docs ...

  3. hibernate jpa 注解 @Temporal(TemporalType.DATE) 格式化时间日期,页面直接得到格式化类型的值

    1.日期: @Temporal(TemporalType.DATE) @Column(name = "applyDate", nullable = false, length = ...

  4. 【hibernate/JPA】注解方式实现 复合主键【spring boot】

    1>hibernate/JPA实现复合主键的思路:是将所有的主键属性封装在一个主键类中,提供给需要复合主键的实体类使用. 2>主键类的几点要求: . 使用复合主键的实体类必须实现Seria ...

  5. hibernate自带的注解和jpa注解的冠希

    hibernate是实现了JPA规范,在我们使用hibernate框架的时候,我们引入了hibernate3或者4这个核心包.hibernate-jpa-2.0-api-1.0.0.Final.jar ...

  6. Hibernate 和 JPA 注解

    转载请注明:Hibernate 和 JPA 注解 | 言曌博客 1.@Entity(name="EntityName") 必须, name为可选,对应数据库中一的个表 2.@Tab ...

  7. jpa 注解使用说明

    1.@Entity(name="EntityName") 必须,name为可选,对应数据库中一的个表 2.@Table(name="",catalog=&quo ...

  8. Hibernate基于注解方式配置来实现实体和数据库之间存在某种映射关系

    实体和数据库之间存在某种映射关系,hibernate根据这种映射关系完成数据的存取.在程序中这种映射关系由映射文件(*.hbm.xml)或者java注解(@)定义. 本文以java注解的形式总结映射关 ...

  9. Hibernate+JPA (EntityMange讲解)

    近年来ORM(Object-Relational Mapping)对象关系映射,即实体对象和数据库表的映射)技术市场人声音鼎沸,异常热闹, Sun在充分吸收现有的优秀ORM框架设计思想的基础上,制定了 ...

随机推荐

  1. nginx的内存管理

    先来看内存池的实现,nginx的内存池实现的非常简单. 这里内存池的一些图表可以看老朱同学的slides : http://blog.zhuzhaoyuan.com/2009/09/nginx-int ...

  2. 关于开源授权协议 GPL 和 LGPL

    GPL 是 GNU General Public License (GNU 通用公共许可证)的缩写形式:LGPL 是 GNU Lesser General Public License (GNU 宽通 ...

  3. SWIFT 闭包的简单使用二

    import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: ...

  4. Android中的Handler机制

    直接在UI线程中开启子线程来更新TextView显示的内容,运行程序我们会发现,如下错 误:android.view.ViewRoot$CalledFromWrongThreadException: ...

  5. 常用<meta>标签

    页面关键词 <meta name="keywords" content="your tags" /> 页面描述 <meta name=&quo ...

  6. mongodb远程连接以及备份、还原、导出、导入

    一.远程连接mongodb 连接命令:mongo -u username -p pwd 192.168.41.215:27017/database(用户名对应的数据库) 二.mongodump备份数据 ...

  7. [转]MySQL数据库引擎

    经常用MySQL数据库,但是,你在用的时候注意过没有,数据库的存储引擎,可能有注意但是并不清楚什么意思,可能根本没注意过这个问题,使用了默认的数据库引擎,当然我之前属于后者,后来成了前者,然后就有了这 ...

  8. Java基础(54):java四种内部类详解(转)

    一般来说,有4中内部类:常规内部类.静态内部类.局部内部类.匿名内部类.  一.常规内部类:常规内部类没有用static修饰且定义在在外部类类体中. 1.常规内部类中的方法可以直接使用外部类的实例变量 ...

  9. android开发之如何使TabHost的TabWidget位于屏幕下方

    更改TabHost里的第一个LinearLayout为RelativeLayout.并在TabWidget中添加android:layout_alignParentBottom="true& ...

  10. ZOJ 3645 BiliBili(高斯消元)

    Shirai Kuroko is a Senior One student. Almost everyone in Academy City have super powers, and Kuroko ...