--oracle数据库
//idcard表里面有个pid ,因为有了人才会有身份证。有个先后顺序吧。
1,建表脚本
-- 删除表
DROP TABLE person ;
DROP TABLE idcard ;
-- 创建表
CREATE TABLE person
(
pid VARCHAR(32) PRIMARY KEY NOT NULL,
name VARCHAR(20)
) ;
CREATE TABLE idcard
(
id VARCHAR(32) PRIMARY KEY NOT NULL ,
name VARCHAR(20) NOT NULL ,
age INT ,
pid VARCHAR(32)
) ;
commit ;
2、############################# pojo类
//<1>IdCard 类
package org.onetoone4.com;
public class IdCard {
private String id ;
private String name ;
private int age;
private Person person ;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
//<2>Person 类
package org.onetoone4.com;
public class Person {
private String pid;
private String name;
private IdCard idcard;
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public IdCard getIdcard() {
return idcard;
}
public void setIdcard(IdCard idcard) {
this.idcard = idcard;
}
}
3、pojo对应的hibernate配置文件
<1>IdCard.hbm.xml
<hibernate-mapping>
<class name="org.onetoone4.com.Person" table="person">
<id name="pid" type="java.lang.String">
<column name="pid" length="32" />
<generator class="uuid.hex"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" not-null="true" />
</property>
<one-to-one name="idcard"
class="org.onetoone4.com.IdCard" property-ref="person">
</one-to-one>
</class>
</hibernate-mapping>
4、测试类 PersonGroupOperate
public class PersonGroupOperate {
private Session session;
public PersonGroupOperate() {
this.session = new Configuration().configure().buildSessionFactory()
.openSession();
}
public void insert(IdCard ic) {
this.session.save(ic);
this.session.beginTransaction().commit();
}
public void insert(Person per) {
this.session.save(per);
this.session.beginTransaction().commit();
}
/**
* @param args
*/
public static void main(String[] args) {
PersonGroupOperate pgo = new PersonGroupOperate();
Person per = new Person();
per.setName("LXH");
IdCard ic = new IdCard();
ic.setName("MLDN.GUEST");
ic.setAge(25);
ic.setPerson(per);
per.setIdcard(ic);
pgo.insert(per);
pgo.insert(ic) ;
}
}