一对一的另一种解决方式就是主键关联,在这种关联关系中,要求两个对象的主键必须保持一致,通过两个表的主键建立关联关系须外键参与。 没法传图 我就用这个表示吧 看id
Copany Address
id<Pk> id<Pk><Fk>
name home
基于主键的映射策略:指一端的主键生成器使用 foreign 策略,表明根据”对方”的主键来生成自己的主键,自己并不能独立生成主键. <param> 子元素指定使用当前持久化类的哪个属性作为 “对方”
<generator class="foreign"> <param name="property">idcharbean</param> </generator>
采用foreign主键生成器策略的一端增加 one-to-one 元素映射关联属性,其 one-to-one 属性还应增加 constrained=“true” 属性;另一端(company)增加one-to-one元素映射关联属性。
constrained(约束):指定为当前持久化类对应的数据库表的主键添加一个外键约束,引用被关联的对象(“对方”)所对应的数据库表主键
<one-to-one name="idcharbean" class="Idcharbean" constrained="true"></one-to-one>
无有外键方(Idcharbean)配置 bean:
package com.java.bean; public class Idcharbean { private int id; private int nameber; private Person person; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getNameber() { return nameber; } public void setNameber(int nameber) { this.nameber = nameber; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }
hm.xml 配置
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.java.bean"> <class name="Idcharbean" table="t_idcahr"> <id name="id" type="int" column="id"> <generator class="identity"/> <!--这个地方的配置是一对一的关键哟--> </id> <property name="nameber" type="int" column="nameber" length="20"/> <one-to-one name="person" class="Person"></one-to-one> </class> </hibernate-mapping>
有外键方(Person)配置 bean
package com.java.bean; public class Person { private int id; private String name; private Idcharbean idcharbean; 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; } public Idcharbean getIdcharbean() { return idcharbean; } public void setIdcharbean(Idcharbean idcharbean) { this.idcharbean = idcharbean; } }
hbm.xml配置
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.java.bean"> <class name="Person" table="t_person"> <id name="id" type="int" column="id"> <generator class="foreign"> <param name="property">idcharbean</param> <!--这个就是共同主键--> </generator> </id> <property name="name" type="string" column="name" length="20"/> <one-to-one name="idcharbean" class="Idcharbean" constrained="true"></one-to-one> </class> </hibernate-mapping>
关于 foregin:表示主键策略使用foreign
*表示根据对方主键生成自己的主键
*param子元素指定使用前持久化
关于 constrained="true"
*指定为当前持久化类的对应表的主键添加外键约束 应用被关联的对象 "对方"所对应的数据库表主键
配置完成后写个增加 玩一下
public class Idchardao { private static SessionFactory sf; static { try { Configuration configuration = new Configuration(); configuration.configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sf = configuration.buildSessionFactory(serviceRegistry); }catch (Exception e){ throw e; } } public void add(Idcharbean idcharbean){ Session s = sf.openSession(); Transaction t =null; try { t = s.beginTransaction(); s.save(idcharbean); t.commit(); }catch (Exception e){ t.rollback(); }finally { s.close(); } } }
ps需要两个类dao里都有add哟
然后是test文件 添加一下
@Test public void testAdd() throws Exception { Idcharbean idcharbean = new Idcharbean(); idcharbean.setNameber(1111); Person person = new Person(); person.setName("马虎"); idcharbean.setPerson(person); person.setIdcharbean(idcharbean); new Idchardao().add(idcharbean); new Psersondao().add(person); }
然后再写个 获取id可以获取到关联的对方id的小测试文件
public Idcharbean Get(){
Session s = sf.openSession();
Transaction t = null;
Idcharbean idcharbean = null;
try {
t = s.beginTransaction();
idcharbean = (Idcharbean) s.get(Idcharbean.class,1);
t.commit();
}catch (Exception e){
t.rollback();
}finally {
s.close();
}
return idcharbean;
}
不如在搞个 移除关系 说动手就动手
//解除关系 public void Remove() { Session s = sf.openSession(); Transaction t = null; try { t = s.beginTransaction(); Person person = (Person) s.get(Person.class, 1); person.setIdcharbean(null); t.commit(); } catch (Exception e) { t.rollback(); } finally { s.close(); } }
既然都移除了不如再来个删除玩玩
//删除 public void delet(){ Session s = sf.openSession(); Transaction t = null; try { t = s.beginTransaction(); Person person = (Person) s.get(Person.class,1); s.delete(person); /* Idcharbean idcharbean = (Idcharbean) s.get(Idcharbean.class,1); s.delete(idcharbean);*/ t.commit(); }catch (Exception e){ t.rollback(); }finally { s.close(); } }
哟 写完了 更多的方法 我在多对多中搞吧= = 话说为啥 我多对多还没写就写一对一了 我是不是疯了