1.单向 N:1 有中间表
例如:一个地方有多个人住.
每个同学记住老师,比老师记住每个同学简单.
(在 n 的一端进行配置效率高)
N 1
Person : Address
Student : Teacher
注意:这次是有中间表
注意:比较 有无中间表的 配置,发现 采用 Join 和 many-to-one 进行配置
中间表
Person
Address
public class Address {
private Integer aid;
private String addressDetail;
//set get ...
}
public class Person {
private Integer pid;
private String name;
private int age;
private Address address; //单向(N:1) 反过来(1:1)
//set get...
}
# person address N:1µÄ Öмä±í
CREATE TABLE mytest.person_address
(
persson_id INT NOT NULL,
address_id INT,
PRIMARY KEY (persson_id),
KEY FK23F8B90A67DFD9E4 (address_id),
KEY FK23F8B90A5A57D6C9 (persson_id),
CONSTRAINT FK23F8B90A5A57D6C9 FOREIGN KEY (persson_id) REFERENCES person (PID),
CONSTRAINT FK23F8B90A67DFD9E4 FOREIGN KEY (address_id) REFERENCES address (AID)
);
#Address
DROP TABLE IF EXISTS mytest.address;
CREATE TABLE mytest.address
(
AID INT NOT NULL,
ADDRESSDESC VARCHAR (255),
PRIMARY KEY (AID)
);
#Person
DROP TABLE IF EXISTS mytest.person;
CREATE TABLE mytest.person
(
PID INT NOT NULL,
NAME VARCHAR (255),
AGE INT,
PRIMARY KEY (PID)
);
<!--person-->
<hibernate-mapping package="com.sh.study.model">
<class name="Person" table="PERSON">
<id name="pid" type="java.lang.Integer" column="PID">
<generator class="increment"/>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<!-- Person 和 address 没有中间表的 N:1 配置 -->
<!--name就是 Person中 的那个 address
cascade="all"
<many-to-one name="address"
class="Address" column="address_id">
</many-to-one>
-->
<!-- person 和 address 有中间表的 N:1 配置 -->
<join table="person_address">
<key column="persson_id"/>
<many-to-one name="address" cascade="all" class="Address" column="address_id"/>
</join>
</class>
</hibernate-mapping>
<!--address-->
<hibernate-mapping package="com.sh.study.model">
<class name="Address" table="ADDRESS">
<id name="aid" type="java.lang.Integer" column="AID">
<generator class="increment"/>
</id>
<property name="addressdesc" type="java.lang.String">
<column name="ADDRESSDESC" />
</property>
</class>
</hibernate-mapping>
//test
public class TestHibernate {
private ApplicationContext act;
private SessionFactory factory;
@Before
public void init(){
act = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
factory= act.getBean("sessionFactory",SessionFactory.class);
}
//测试 无 五间表的 N:1 关系
@Test
public void test1() {
Session session=factory.getCurrentSession();
Transaction tx=session.beginTransaction();
Person p=new Person();
p.setAge(12);
p.setName("Yeeku");
Address address=new Address();
address.setAddressdesc("北京海淀区");
p.setAddress(address);
//持久化对象
session.persist(p);
session.flush();
tx.commit();
//如果不是使用的SessionFactory.getSession()来获得Session。
//而是使用SessionFactory.getCurrentSession()方法来获得Session时,
//当事务结束的时候,不管是提交还是回滚事务,hibernate会自动关闭Session的,
//session.close();
}
//测试 无 中间表的 N:1 关系
@Test
public void test2() {
Session session=factory.getCurrentSession();
Transaction tx=session.beginTransaction();
Person p=new Person();
p.setAge(12);
p.setName("Yeeku");
Address address=new Address();
address.setAddressdesc("北京海淀区");
p.setAddress(address);
//持久化对象
session.persist(p);
//修改 Person的 地址
Address address1=new Address();
address1.setAddressdesc("上海虹口");
p.setAddress(address1);
tx.commit();
}
}
注意:cascade="all"
<!--如果Person配置-->
<hibernate-mapping package="com.sh.study.model">
<class name="Person" table="PERSON">
<id name="pid" type="java.lang.Integer" column="PID">
<generator class="increment"/>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<!--如果少了 下面这个
cascade="all"
-->
<many-to-one name="address"
class="Address" column="address_id">
</many-to-one>
</class>
</hibernate-mapping>
会出现异常:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.sh.study.model.Address