星期六, 一月 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
已解决
?
原文:http://yuzhouxiner.iteye.com/blog/2270042