Hibernate之one-to-one外键关联映射详解编程语言
2023-06-13 09:20:42 时间
在hibernate一对一实体映射中,常用有两种方式,一种就是把一对一看作多对一的一个特例,即通过外键参考。
另一种是通过主键参考,限制两个数据表中的主键使用相同的值。
po类
Person.java
package po; public class Person { 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;
Card.java
package po; public class Card { private int id; //身份证ID private String number; //身份证号码 private Person person; //一个身份证号对应一个人 public int getId() { return id; public void setId(int id) { this.id = id; public String getNumber() { return number; public void setNumber(String number) { this.number = number; public Person getPerson() { return person; public void setPerson(Person person) { this.person = person;
property name="number" /property !-- 是多对一的一种特例 unique=true设置为唯一关联 -- many-to-one name="person" unique="true" column="person" /many-to-one /class /hibernate-mapping
Person.hbm.xml
hibernate-mapping class name="po.Person" table="person" id name="id" type="integer" generator / /id property name="name" / /class /hibernate-mapping
hibernate.cfg.xml
hibernate-configuration session-factory property name="dialect" org.hibernate.dialect.MySQLDialect /property property name="connection.url" jdbc:mysql://localhost:3306/hibernate /property property name="connection.username" root /property property name="connection.password" 1 /property property name="connection.driver_class" com.mysql.jdbc.Driver /property property name="myeclipse.connection.profile" mysql /property property name="show_sql" true /property property name="format_sql" true /property mapping resource="po/Person.hbm.xml"/ mapping resource="po/Card.hbm.xml"/ /session-factory /hibernate-configuration
Test.java
package po; import java.util.Date; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.junit.Test;
Configuration config = new Configuration().configure(); SchemaExport export = new SchemaExport(config); export.create(true, true); @Test public void testSave(){ //测试 添加数据 Configuration config = new Configuration().configure(); SessionFactory factory = config.buildSessionFactory(); Session session = factory.openSession(); session.beginTransaction();
Person person = new Person(); //先有person然后才能有省份证 person.setName("zhangsan"); session.save(person); //一对一外键关联时 必须先保存person
public void testGetPerson(){ Configuration config = new Configuration().configure(); SessionFactory factory = config.buildSessionFactory(); Session session = factory.openSession(); session.beginTransaction(); Card card = (Card)session.get(Card.class,2 ); //取出Card System.out.println("CardNumber: "+card.getNumber()); Person person = card.getPerson(); //通过card.getPerson()方法 返回person System.out.println("PersonName: "+person.getName()); //取出person的name session.getTransaction().commit(); session.close();
person和card分别生成sql为:
| card | CREATE TABLE `card` ( `id` int(11) NOT NULL auto_increment, `number` varchar(255) default NULL, `person` int(11) default NULL, PRIMARY KEY (`id`), UNIQUE KEY `person` (`person`), KEY `FK2E7B10BD1DCD99` (`person`), CONSTRAINT `FK2E7B10BD1DCD99` FOREIGN KEY (`person`) REFERENCES `person` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
相关文章
- Hibernate二级缓存问题[通俗易懂]
- Spring JPA 查询的时候提示错 org.hibernate.TransientObjectException
- Hibernate学习笔记三 多表详解编程语言
- hibernate的merge()详解编程语言
- hibernate 三种状态的转换详解编程语言
- hibernate+oracle+主键varchar2类型,增加序列策略注解失败详解编程语言
- SSH框架(Struts2+Spring+Hibernate)搭建整合详细步骤
- Hibernate Criteria接口 addOrder方法:设置结果集的排序规则
- Hibernate connection.username属性:指定数据库连接用户名
- Hibernate default_entity_mode属性:指定默认实体表现模式
- Hibernate hibernate.order_updates属性:为SQL更新排序
- Hibernate hibernate.transaction.factory_class属性:设置事务管理类型
- 高效使用Hibernate连接MySQL数据库(hibernate连接mysql数据库)
- 《Hibernate快速开始 – 批量处理》
- Hibernate管理Session和批量操作分析