歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Hibernate之one-to-one主鍵關聯映射

Hibernate之one-to-one主鍵關聯映射

日期:2017/3/1 10:41:42   编辑:Linux編程

one-to-one映射的例子為一個人擁有一個身份證,一個身份證屬於一個人。

先創建po類

Person.java

  1. package po;
  2. public class Person {
  3. private int id;
  4. private String name; //姓名
  5. public int getId() {
  6. return id;
  7. }
  8. public void setId(int id) {
  9. this.id = id;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. }
Card.java
  1. package po;
  2. public class Card {
  3. private int id; //身份證ID
  4. private String number; //身份證號碼
  5. private Person person; //一個身份證號對應一個人
  6. public int getId() {
  7. return id;
  8. }
  9. public void setId(int id) {
  10. this.id = id;
  11. }
  12. public String getNumber() {
  13. return number;
  14. }
  15. public void setNumber(String number) {
  16. this.number = number;
  17. }
  18. public Person getPerson() {
  19. return person;
  20. }
  21. public void setPerson(Person person) {
  22. this.person = person;
  23. }
  24. }
po類的映射文件

Person.hbm.xml

  1. <hibernate-mapping>
  2. <class name="po.Person" table="person">
  3. <id name="id" type="integer">
  4. <generator class="native" />
  5. </id>
  6. <property name="name" />
  7. </class>
  8. </hibernate-mapping>
Card.hbm.xml
  1. <hibernate-mapping>
  2. <class name="po.Card" table="card">
  3. <id name="id" type="integer">
  4. <!-- 主鍵生成方式為foreign -->
  5. <generator class="foreign">
  6. <param name="property">person</param>
  7. </generator>
  8. </id>
  9. <property name="number"></property>
  10. <!-- !!!constrained=true表示生成外鍵約束 -->
  11. <one-to-one name="person" constrained="true"></one-to-one>
  12. </class>
  13. </hibernate-mapping>
hibernate.cfg.xml
  1. <hibernate-configuration>
  2. <session-factory>
  3. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  4. <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
  5. <property name="connection.username">root</property>
  6. <property name="connection.password">1</property>
  7. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  8. <property name="myeclipse.connection.profile">mysql</property>
  9. <property name="show_sql">true</property>
  10. <property name="format_sql">true</property>
  11. <mapping resource="po/Person.hbm.xml"/>
  12. <mapping resource="po/Card.hbm.xml"/>
  13. </session-factory>
  14. </hibernate-configuration
測試類Test.java
  1. package po;
  2. import java.util.Date;
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5. import java.util.Set;
  6. import org.hibernate.Session;
  7. import org.hibernate.SessionFactory;
  8. import org.hibernate.cfg.Configuration;
  9. import org.hibernate.tool.hbm2ddl.SchemaExport;
  10. import org.junit.Test;
  11. public class Test {
  12. @Test
  13. public void testCreateDB(){
  14. //生成表結構
  15. Configuration config = new Configuration().configure();
  16. SchemaExport export = new SchemaExport(config);
  17. export.create(true, true);
  18. }
  19. @Test
  20. public void testSave(){
  21. //測試 添加數據
  22. Configuration config = new Configuration().configure();
  23. SessionFactory factory = config.buildSessionFactory();
  24. Session session = factory.openSession();
  25. session.beginTransaction();
  26. Person person = new Person(); //先有person然後才能有省份證
  27. person.setName("zhangsan");
  28. Card card = new Card();
  29. card.setNumber("4114031111222223333");
  30. card.setPerson(person); //此身份證屬於此人
  31. session.save(card); //保存card
  32. session.getTransaction().commit();
  33. }
  34. @Test
  35. public void testGetPerson(){
  36. Configuration config = new Configuration().configure();
  37. SessionFactory factory = config.buildSessionFactory();
  38. Session session = factory.openSession();
  39. session.beginTransaction();
  40. Card card = (Card)session.get(Card.class,1 ); //取出Card
  41. System.out.println("CardNumber: "+card.getNumber());
  42. Person person = card.getPerson(); //通過card.getPerson()方法 返回person
  43. System.out.println("PersonName: "+person.getName()); //取出person的name
  44. session.getTransaction().commit();
  45. session.close();
  46. }
  47. }
hibernate後天生成表結構sql為
  1. create table card (
  2. id integer not null,
  3. number varchar(255),
  4. primary key (id)
  5. )
  6. create table person (
  7. id integer not null auto_increment,
  8. name varchar(255),
  9. primary key (id)
  10. )
  11. alter table card
  12. add index FK2E7B10F83A3F5F (id),
  13. add constraint FK2E7B10F83A3F5F
  14. foreign key (id)
  15. references person (id)
Copyright © Linux教程網 All Rights Reserved