`

Hibernate annotation 联合主键

 
阅读更多

Hibernate annotation 联合主键

 

 

package com.hibernate.entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Husband implements Serializable {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2476364405175138004L;

	
	private int id;
	
	private String name;
	
	private Wife wife;
	
	
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	@OneToOne
	public Wife getWife() {
		return wife;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setWife(Wife wife) {
		this.wife = wife;
	}
	
	
}

 

package com.hibernate.entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;

@Entity
@IdClass(WifePK.class)//这种设置主键的方式最简单(在下面联合主键的键上设@Id)
public class Wife implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5270130768836331730L;

	private int id;
	
	private String name;
	
	@Id
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Id
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}

 

Wife的主键类

package com.hibernate.entity;

import java.io.Serializable;


public class WifePK implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5270130768836331730L;

	
	private int id;
	
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
	
}

 

 

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

	<session-factory>
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<property name="connection.url">
			jdbc:mysql://localhost/test
		</property>
		<property name="connection.username">root</property>
		<property name="connection.password">root</property>
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>

		<!-- JDBC connection pool (use the built-in) -->
		<property name="connection.pool_size">1</property>



		<!-- Enable Hibernate's automatic session context management -->
		<property name="current_session_context_class">thread</property>

		<!-- Disable the second-level cache  -->
		<property name="cache.provider_class">
			org.hibernate.cache.NoCacheProvider
		</property>

		<!-- Echo all executed SQL to stdout -->
		<property name="show_sql">true</property>

		<property name="format_sql">true</property>
		<mapping class="com.hibernate.entity.Husband" />
		<mapping class="com.hibernate.entity.Wife" />
	</session-factory>

</hibernate-configuration>
 

 

然后用junit4调试:

package com.hibernate.test;


import org.hibernate.SessionFactory;

public class ORMappingTest {
	
	private static SessionFactory sessionFactory;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		sessionFactory.close();
	}
	
	@Test
	public void testSchemaExport() {
		new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
	}

}
 

生成的SQL文日志:

    create table Husband (
        id integer not null auto_increment,
        name varchar(255),
        wife_id integer,
        wife_name varchar(255),
        primary key (id)
    )

    create table Wife (
        id integer not null,
        name varchar(255) not null,
        primary key (id, name)
    )

    alter table Husband 
        add index FKAEEA401B2DB1BA15 (wife_id, wife_name), 
        add constraint FKAEEA401B2DB1BA15 
        foreign key (wife_id, wife_name) 
        references Wife (id, name)
 

 

分享到:
评论

相关推荐

    最全的Hibernate Annotation API文档

    Hibernate支持单表继承、联合继承和多表继承。可以使用`@Inheritance`、`@DiscriminatorValue`等注解进行配置。 九、转换器(Converters) 使用`@Converter`注解可以自定义类型转换,处理自定义类型或枚举类型与...

    Hibernate Annotation 学习笔记

    - Hibernate支持单表继承(Single Table Inheritance, STI)、联合继承(Union Subclassing, USI)和表-per类继承(Table Per Concrete Class, TPC)。 - `@Inheritance(strategy = InheritanceType.SINGLE_TABLE)...

    Hibernate一对一单向外键关联 (联合主键annotation)

    本篇将详细讲解如何使用Hibernate进行一对一单向外键关联,并且该关联涉及到联合主键的注解配置。 首先,一对一关联可以分为两种类型:单向和双向。在单向一对一关联中,只有一个实体知道另一个实体的存在,而另一...

    Hibernate Annotation应用

    7. **继承映射**:Hibernate支持单表继承(SINGLE_TABLE)、联合继承(JOINED)和表-per-hierarchy(TABLE_PER_CLASS)。使用`@Inheritance`和`@DiscriminatorColumn`、`@DiscriminatorValue`注解可以定义继承策略。...

    hibernate_annotation

    《Hibernate Annotation 中文帮助文档详解》 Hibernate是一个流行的Java持久化框架,它简化了数据库操作,使得开发者可以更专注于业务逻辑而不是数据库交互。在Hibernate中,Annotation是用于替代传统XML配置的一种...

    Hibernate annotation 详解

    自Hibernate 3开始,引入了注解(Annotation)支持,使得开发者无需XML配置就能实现对象与数据库表之间的映射。本文将深入探讨Hibernate注解的使用方法和常见注解,旨在帮助开发者更好地理解和应用Hibernate注解。 ...

    hibernate-annotation-3.2.1

    4. **继承策略**:Hibernate支持单表继承(`@Inheritance(strategy = InheritanceType.SINGLE_TABLE)`)、联合继承(`@Inheritance(strategy = InheritanceType.JOINED)`)和分表继承(`@Inheritance(strategy = ...

    Hibernate笔记 马士兵

    第1课 课程内容 6 第2课 Hibernate UML图 6 第3课 风格 7 第4课 资源 7 第5课 环境准备 7 ...三、 联合主键 24 1、xml方式 24 2、annotation方式 27 第14课 Hibernate核心开发接口(重点) 29 ........

    hibernate所有开发包

    3. Inheritance:Hibernate支持多种继承映射策略,如单表继承、联合继承和表-per-hierarchy。 四、关联映射 1. OneToMany/ManyToOne:一对多和多对一关系映射,使用@OneToMany和@ManyToOne注解,@JoinColumn定义...

    马士兵hibernate笔记

    第13课专注于ID主键的生成策略,包括Xml方式和AnnotateOn方式,如AUTO、IDENTITY、SEQUENCE、TABLE等策略,并讨论了联合主键的配置。 第14课是核心内容,介绍了Hibernate的主要开发接口,如`Configuration...

    Hibernate关联关系的CRUD和集合映射(annotation)

    5. **@JoinTable**:在多对多关系中,用于定义中间表的详细信息,包括联合主键等。 ### 集合映射的加载策略 Hibernate提供了多种加载策略,例如: - **EAGER**:集合会在主对象加载时一起加载,适合小规模数据。 ...

    Hibernate annotations大全

    1. Hibernate Annotation 概述 Hibernate Annotations是Hibernate框架中的一种元数据表示方式,它允许开发者在Java实体类上直接添加注解,替代传统的XML配置文件来描述对象关系映射(ORM)。这种方式使得ORM配置更加...

    Hibernate数据访问技术大全

    最后,书中可能还会讨论一些高级主题,如Hibernate的联合映射(Association Mapping)、继承映射(Inheritance Mapping)、复合主键(Composite Key)以及自定义类型(Custom Types)。这些特性允许开发者构建更复杂...

    Hibernate ORM 5.2.7.Final User Guide

    - **Annotation配置**: 使用@Entity、@Table等注解,以及@EntityListeners等,实现对实体的元数据注解配置。 **3. 数据库映射** - **实体类映射**: 如何定义实体类,包括属性、构造函数、setter/getter方法等。 - *...

    hibernate3.5中文参考

    11. **继承映射**:Hibernate支持单表继承、联合表继承和表-per-class-hierarchy映射策略,使得继承结构能够平滑地映射到数据库。 12. **延迟加载(Lazy Loading)**:Hibernate的懒加载机制允许对象的关联属性在...

    Hibernate src

    8. **继承映射**:Hibernate支持单表继承、联合继承和表-per-hierarchy等多种继承策略,使得对象模型的复杂性得以在数据库中妥善反映。 9. **关联映射**:包括一对一(OneToOne)、一对多(OneToMany)、多对一...

    Java持久性和休眠指南(S. Hennebrueder)Guide to Java Persistence and Hibernate (S. Hennebrueder)

    Hibernate支持单表继承、连接继承、联合继承等策略,每种策略各有优势和适用场景。 ### 14. 大对象(LOB)映射 对于大文本数据或二进制数据,Hibernate提供了处理大对象(LOB)的机制。LOB可以映射到数据库中的特殊...

Global site tag (gtag.js) - Google Analytics