`

035_一对一单向外键关联_annotation

阅读更多

星期六, 一月 09, 2016   17:59

一对一单向关联

1.a)hibernate_0700_One2One_unic_fk

b)Annotation: @OneToOne@JoinColumn

c)xml:<many-to-one/>

 

关系映射

对象

 

表与表之间的关系只有外键。

 

 

关系映射(这里讲的是对象)

 

关系对象的关系:

        一对一、一对多、多对多

 

写在对象里,又出现了单向、多向。

 

加起来7中。

 

一对一:

1.单向(主键、外键)

2.双向()

 

 

 

 

7.集合映射

8.继承关系

9.组件映射

 

 

2.简化问题

   2.1怎么写Annotation

   2.2增删改查CRUD怎么写

 

3.一对一

3.1单向(主键、外键)

3.2

 

 

 

 

 

本节:

一对一:

1.一对一单向、外键关联

 

步骤:

考虑这两个类怎么写

Husband  -----Wife

要在上面加上@Entity保证是实体类

 

站在husband角度有一个wife的引用

 

 

然后在数据库中进行设计表

两张表

husband    id name wife_id

wife           id  name

几种方法:

1.主键关联

2.外键关联

3.关联表-----多对多的时候使用

 

 

运行测试:

       在运行的console中看到自动帮你生成的建表语句,数据库中对应的表。

 

 

 

二、使用powerdesinger根据sql进行生成表

 

1.先数据库配置

数据库-->config    connection 

--->connection profile-->新加一个数据源

:name:  MySql  description:MySql....

还需要在环境变量中进行配置mysql.jar,因为powerdesigner支持不好

-----加了可还是没有成功。

 

 

代码案例:

 

package com.zhuhw.hibernate.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Husband{
	private int id;
	private String name;
	//一个husband里又一个wife的引用
    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.zhuhw.hibernate.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Wife{
	private int id;
	private String name;
    @Id
    @GeneratedValue
	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
}

 

 

 

 

<?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">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        
        <!--
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="connection.username">scoot</property>
        <property name="connection.password">tiger</property>-->
        
        <!-- JDBC connection pool (use the built-in) -->
        <!--<property name="connection.pool_size">1</property>-->

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</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>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>
      
	    <mapping class="com.zhuhw.hibernate.model.Husband"/>
		<mapping class="com.zhuhw.hibernate.model.Wife"/>	    
    </session-factory>

</hibernate-configuration>

 

 

 

 

package com.zhuhw.hibernate.model;

import java.util.Date;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class HibernateORMappingTest {
	
	public static SessionFactory sf = null;
	@BeforeClass
	public  static void beforeClass(){
			sf = new AnnotationConfiguration().configure().buildSessionFactory();
	}
	
	@Test
	public void testOne2One(){
		new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
	}
	@AfterClass
	public static void afterClass(){
		sf.close();
	}
	
	
	
}

 

 

ok刚开始运行不成功是没加@Entity

已解决

 

0
0
分享到:
评论

相关推荐

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

    NULL 博文链接:https://cdxs2.iteye.com/blog/1932507

    Hibernate一对一单向外键关联(annotation/xml)

    NULL 博文链接:https://cdxs2.iteye.com/blog/1930748

    Hibernate_Annotation关联映射

    其中一个实体通过外键关联到另一个实体的主键(注意要模拟一对一关联必须在外键列上添加唯一约束),(3).通过关联表来保存两个实体之间的连接关系(要模拟一对一关联必须在每一个外键上添加唯一约束)。 1.共享主键的...

    Hibernate Annotation 基于外键的单向多对一关联

    NULL 博文链接:https://paladin1988.iteye.com/blog/1633417

    Hibernate注释大全收藏

    Hibernate注释大全收藏 声明实体Bean @Entity public class Flight implements Serializable { Long id; @Id public Long getId() { return id; } public void setId(Long id) { this.id...一对一 使用 @OneToOne...

    Hibernate3的帮助文档

    8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖对象 9.3. 组件作为Map的索引(Components as Map indices ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. ...

    hibernate 框架详解

    一对一(one to one) 8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多...

    hibernate3.04中文文档.chm

    8.4.2. 一对一(one to one) 8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多...

    Hibernate中文详细学习文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    Hibernate教程

    8.2.2. 一对一(one to one) 8.2.3. 一对多(one to many) 8.3. 使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一...

    Hibernate 中文 html 帮助文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    Hibernate参考文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    Hibernate+中文文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    hibernate 体系结构与配置 参考文档(html)

    一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many ...

    HibernateAPI中文版.chm

    7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. ...

    hibernate3.2中文文档(chm格式)

    7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. ...

    最全Hibernate 参考文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    Hibernate3+中文参考文档

    7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一...

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

    10.5.9 对关联实体进行排序 424 10.6 继承关系映射 426 10.6.1 整个类层次对应一张表的 映射策略 427 10.6.2 连接子类的映射策略 430 10.6.3 每个具体类对应一张表的 映射策略 434 10.7 使用抽象实体和非实体父类 ...

Global site tag (gtag.js) - Google Analytics