Hibernate之one-to-one外键关联映射详解编程语言

在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; 
	} 
	 
	 
	 
	 
} 

Card.hbm.xml

<hibernate-mapping> 
    <class name="po.Card" table="card"> 
        <id name="id" type="integer"> 
            <generator class="native" /> 
        </id> 
        <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 class="native" /> 
		</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; 
 
 
public class PersonTest { 
	 
	@Test 
	public void testCreateDB(){ 
		//生成表结构 
		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 
		 
		 
		Card card = new Card(); 
		card.setNumber("4114031111222223333"); 
		card.setPerson(person);       //此身份证属于此人 
		session.save(card);           //保存card 
		 
		session.getTransaction().commit(); 
		 
	}  
	@Test 
	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 | 
 
 
| person | CREATE TABLE `person` ( 
  `id` int(11) NOT NULL auto_increment, 
  `name` varchar(255) default NULL, 
  PRIMARY KEY  (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/14406.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论