Hibernate,JPA注解@SecondaryTables
使用类一级的 @SecondaryTable或@SecondaryTables注解可以实现单个实体到多个表的映射. 使用 @Column或者 @JoinColumn注解中的table参数可指定某个列所属的特定表.
使用类一级的 @SecondaryTable或@SecondaryTables注解可以实现单个实体到多个表的映射. 使用 @Column或者 @JoinColumn注解中的table参数可指定某个列所属的特定表.
用例代码如下:
- 数据库DDL语句
1,CAT表
create table CAT
(
id VARCHAR2(32 CHAR) not null,
create_time TIMESTAMP(6),
update_time TIMESTAMP(6),
cat_name VARCHAR2(255 CHAR),
first_name VARCHAR2(255 CHAR),
last_name VARCHAR2(255 CHAR),
version NUMBER(10) not null
)
2,CAT_INFO表
create table CAT_INFO
(
address VARCHAR2(255 CHAR),
birthday TIMESTAMP(6),
cat_id VARCHAR2(32 CHAR) not null
)
3,CAT_INFO_2表
create table CAT_INFO_2
(
gender NUMBER(10),
mobile VARCHAR2(255 CHAR),
cat_id VARCHAR2(32 CHAR) not null
)
- hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库驱动配置 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
<property name="connection.username">wxuatuser</property>
<property name="connection.password">xlh</property>
<property name="show_sql">true</property>
<!-- 自动执行DDL属性是update,不是true -->
<property name="hbm2ddl.auto">update</property>
<!-- hibernate实体类 --> <mapping class="a6_SecondaryTable.CatSecondaryTables"/> </session-factory>
</hibernate-configuration>
- java类
实体类 - 基类
package model;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import org.hibernate.annotations.GenericGenerator;
/**
* 实体类 - 基类
*/
@MappedSuperclass
public class BaseEntity implements Serializable { private static final long serialVersionUID = -6718838800112233445L; private String id;// ID
private Date create_time;// 创建日期
private Date update_time;// 修改日期
@Id
@Column(length = 32, nullable = true)
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Column(updatable = false)
public Date getCreate_time() {
return create_time;
}
public void setCreate_time(Date create_time) {
this.create_time = create_time;
}
public Date getUpdate_time() {
return update_time;
}
public void setUpdate_time(Date update_time) {
this.update_time = update_time;
}
@Override
public int hashCode() {
return id == null ? System.identityHashCode(this) : id.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass().getPackage() != obj.getClass().getPackage()) {
return false;
}
final BaseEntity other = (BaseEntity) obj;
if (id == null) {
if (other.getId() != null) {
return false;
}
} else if (!id.equals(other.getId())) {
return false;
}
return true;
}
}
实体类
package a6_SecondaryTable;
import java.util.Date;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.SecondaryTables;
import javax.persistence.Table;
import javax.persistence.Version;
import model.BaseEntity;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate; @Entity
@DynamicInsert
@DynamicUpdate
@Table(name="CAT")
@SecondaryTables( value = {
@SecondaryTable(name="CAT_INFO",pkJoinColumns=@PrimaryKeyJoinColumn(name="CAT_ID")),
@SecondaryTable(name="CAT_INFO_2",pkJoinColumns=@PrimaryKeyJoinColumn(name="CAT_ID")) })
public class CatSecondaryTables extends BaseEntity{
/**
* 实体类
*/
private static final long serialVersionUID = -2776330321385582872L; private String cat_name;
private Name name;
private int version;
private String address;
private Date birthday;
private Integer gender;
private String mobile; @Version
public int getVersion() {
return version;
} public void setVersion(int version) {
this.version = version;
} public String getCat_name() {
return cat_name;
} public void setCat_name(String cat_name) {
this.cat_name = cat_name;
} @Embedded
@AttributeOverrides({
@AttributeOverride(name = "first_name", column = @Column(name = "first_name")),
@AttributeOverride(name = "last_name", column = @Column(name = "last_name")) })
public Name getName() {
return name;
} public void setName(Name name) {
this.name = name;
}
@Column(name="ADDRESS", table="CAT_INFO")
public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
}
@Column(name="BIRTHDAY", table="CAT_INFO")
public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Column(name="GENDER", table="CAT_INFO_2")
public Integer getGender() {
return gender;
} public void setGender(Integer gender) {
this.gender = gender;
}
@Column(name="MOBILE", table="CAT_INFO_2")
public String getMobile() {
return mobile;
} public void setMobile(String mobile) {
this.mobile = mobile;
}
}
Dao
package daoUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder; public class HibernateUtil { private static final SessionFactory sessionFactory; static {
try {
Configuration cfg = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(cfg.getProperties()).buildServiceRegistry();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
} public static Session getSession() throws HibernateException {
return sessionFactory.openSession();
} public static Object save(Object obj){
Session session = HibernateUtil.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(obj);
tx.commit();
} catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
return obj;
} public static void delete(Class<?> clazz,String id){
Session session = HibernateUtil.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Object obj = session.get(clazz,id);
session.delete(obj);
tx.commit();
} catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
}
main
package a6_SecondaryTable;
import java.util.Date;
import daoUtil.HibernateUtil; public class Test_SecondaryTables { public static void main(String[] args) { Name name = new Name();
CatSecondaryTables cat = new CatSecondaryTables();
cat.setCat_name("test7SecondaryTables2");
cat.setName(name);
cat.setAddress("中华人民共和国");
cat.setBirthday(new Date());
cat.setGender(1);
cat.setMobile("13012345678"); HibernateUtil.save(cat);
System.out.println(cat.getId()); CatSecondaryTables cat1 = (CatSecondaryTables)HibernateUtil.getSession().get(CatSecondaryTables.class, cat.getId());
System.out.println(cat1.getId());
}
}
环境:JDK1.6,MAVEN,tomcat,eclipse
源码地址:http://files.cnblogs.com/files/xiluhua/hibernate%40SecondaryTables.rar
Hibernate,JPA注解@SecondaryTables的更多相关文章
- Java、Hibernate(JPA)注解大全
1.@Entity(name=”EntityName”) 必须,name为可选,对应数据库中一的个表 2.@Table(name=””,catalog=””,schema=””) 可选,通常和@Ent ...
- JPA注解@SecondaryTables 实现一个实体映射多张数据库表
参考:http://jingpin.jikexueyuan.com/article/46978.html Annotation Type SecondaryTables(参考:https://docs ...
- hibernate jpa 注解 @Temporal(TemporalType.DATE) 格式化时间日期,页面直接得到格式化类型的值
1.日期: @Temporal(TemporalType.DATE) @Column(name = "applyDate", nullable = false, length = ...
- 【hibernate/JPA】注解方式实现 复合主键【spring boot】
1>hibernate/JPA实现复合主键的思路:是将所有的主键属性封装在一个主键类中,提供给需要复合主键的实体类使用. 2>主键类的几点要求: . 使用复合主键的实体类必须实现Seria ...
- hibernate自带的注解和jpa注解的冠希
hibernate是实现了JPA规范,在我们使用hibernate框架的时候,我们引入了hibernate3或者4这个核心包.hibernate-jpa-2.0-api-1.0.0.Final.jar ...
- Hibernate 和 JPA 注解
转载请注明:Hibernate 和 JPA 注解 | 言曌博客 1.@Entity(name="EntityName") 必须, name为可选,对应数据库中一的个表 2.@Tab ...
- jpa 注解使用说明
1.@Entity(name="EntityName") 必须,name为可选,对应数据库中一的个表 2.@Table(name="",catalog=&quo ...
- Hibernate基于注解方式配置来实现实体和数据库之间存在某种映射关系
实体和数据库之间存在某种映射关系,hibernate根据这种映射关系完成数据的存取.在程序中这种映射关系由映射文件(*.hbm.xml)或者java注解(@)定义. 本文以java注解的形式总结映射关 ...
- Hibernate+JPA (EntityMange讲解)
近年来ORM(Object-Relational Mapping)对象关系映射,即实体对象和数据库表的映射)技术市场人声音鼎沸,异常热闹, Sun在充分吸收现有的优秀ORM框架设计思想的基础上,制定了 ...
随机推荐
- 安装 zsh 、 on-my-zsh 和 autojump
安装 zsh . on-my-zsh 和 autojump zsh 是 linux 上另外一个 shell ,号称是终极 shell .它的配置比较复杂,一般的发行版中,默认没有安装这个 shell ...
- OC类方法和实例方法中的self区别
OC类方法和实例方法中的self Objective-C里面既有实例方法也类方法.类方法(Class Method) 有时被称为工厂方法(Factory Method)或者方便方法(Convenien ...
- [BS] 小知识点总结-01
1. UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle&quo ...
- 项目管理利器——Maven
假设公司要开发一个新的Web项目,使用目前流行的struts2.spring.MyBatis进行新项目开发.那么接下来首先要进行的工作就是各个框架的jar包的下载.大家通常的做法是先到struts2的 ...
- 解决: libcimtd.lib not found, rpcndr.lib not found
在编译Inside COM这本书的代码的时候. 报这个错. 毕竟1996年的代码... 原因很简单: libcimtd.lib 是 VC6时代的东西(对应着iostream.h)...现在的MS编译器 ...
- Xib文件的使用
- ASM磁盘组兼容性设置
磁盘组的兼容性参数:-compatible.asm:最低版本的asm软件,这也会影响asm元数据在磁盘中的结构-compatible.rdbms:最低版本的rdbms软件,决定了rdbms是否能够mo ...
- 《30天自制操作系统》11_day_学习笔记
harib08a: 鼠标的显示问题:我们可以看到,鼠标移到窗口最右侧之后就不能再移动了,而WIN中,鼠标是可以移动到最右边隐藏起来的.怎么办?把鼠标指针显示的范围扩宽就行!我们来修改一下HariMai ...
- 史上最全的css hack(ie6-9,firefox,chrome,opera,safari) (zz)
在这个浏览器百花争鸣的时代,作为前端开发的我们为了我们漂亮的设计能适应各个浏览器可为煞费苦心,主要体现在javascript和css上面.javascript我这次就不谈了,先说说css. ...
- 实验十五_安装新的int 9中断例程
安装一个新的int 9中断例程,功能:在DOS下,按下“A”键后,除非不在松开, 如果松开,就显示满屏幕的“A”:其他的键照常处理. 提示:按下一个键时产生的扫描码称为通码,松开一个键产生的扫描 ...