有這樣一個應用場景,有一張用戶表(APM_USER),一張部門表(APM_DEPT)。用戶和部門之間的關係是多對一(many to one)。考慮到其他一些特殊情況。雖然實際情況,部門對用戶是多對一(many to one)。實際上,刪除一個部門的時候,爲保險起見,不需要級聯刪除用戶信息,查出部門對聯時,也不需要查出部門內的所有員工。因此做成了單向多對一關聯。
具體建表SQL如下:
-- Create table
create table APM_USER
(
USER_ID VARCHAR2(18) not null,
USER_NAME VARCHAR2(300),
DEPT_ID VARCHAR2(18),
USER_PASS VARCHAR2(300),
USER_STATUS VARCHAR2(20),
EMAIL VARCHAR2(300),
MOBILE_PHONE VARCHAR2(300),
FCU VARCHAR2(18),
FCD VARCHAR2(18),
FCT VARCHAR2(20),
LUU VARCHAR2(18),
LUD VARCHAR2(18),
LUT VARCHAR2(20)
)
tablespace AMSTBS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Add comments to the columns
comment on column APM_USER.USER_ID
is '用戶編號';
comment on column APM_USER.USER_NAME
is '用戶名稱';
comment on column APM_USER.DEPT_ID
is '所在部門';
comment on column APM_USER.USER_PASS
is '用戶密碼';
comment on column APM_USER.USER_STATUS
is '用戶狀態';
comment on column APM_USER.EMAIL
is '郵件地址';
comment on column APM_USER.MOBILE_PHONE
is '手機號';
comment on column APM_USER.FCU
is '登記人';
comment on column APM_USER.FCD
is '登記部門';
comment on column APM_USER.FCT
is '登記時間';
comment on column APM_USER.LUU
is '更新人';
comment on column APM_USER.LUD
is '更新部門';
comment on column APM_USER.LUT
is '更新時間';
-- Create/Recreate primary, unique and foreign key constraints
alter table APM_USER
add constraint PK_APM_USER primary key (USER_ID)
using index
tablespace AMSTBS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
alter table APM_USER
add constraint FK_APM_USER_REFERENCE_APM_DEPT foreign key (DEPT_ID)
references APM_DEPT (DEPT_ID);
-- Create table
create table APM_DEPT
(
DEPT_ID VARCHAR2(18) not null,
DEPT_NAME VARCHAR2(300),
DEPT_LEVEL INTEGER,
DEPT_STATUS VARCHAR2(20),
FCU VARCHAR2(18),
FCD VARCHAR2(18),
FCT VARCHAR2(20),
LUU VARCHAR2(18),
LUD VARCHAR2(18),
LUT VARCHAR2(20)
)
tablespace AMSTBS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Add comments to the columns
comment on column APM_DEPT.DEPT_ID
is '部門號';
comment on column APM_DEPT.DEPT_NAME
is '部門名';
comment on column APM_DEPT.DEPT_LEVEL
is '部門級別';
comment on column APM_DEPT.DEPT_STATUS
is '部門狀態';
comment on column APM_DEPT.FCU
is '登記人';
comment on column APM_DEPT.FCD
is '登記部門';
comment on column APM_DEPT.FCT
is '登記時間';
comment on column APM_DEPT.LUU
is '更新人';
comment on column APM_DEPT.LUD
is '更新部門';
comment on column APM_DEPT.LUT
is '更新時間';
-- Create/Recreate primary, unique and foreign key constraints
alter table APM_DEPT
add constraint PK_APM_DEPT primary key (DEPT_ID)
using index
tablespace AMSTBS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
hibernate映射文件配置如下:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2015-3-13 8:24:33 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.amarsoft.apm.model.User" table="APM_USER"> <id name="userId" type="string"> <column name="USER_ID" length="18" /> <generator class="assigned" /> </id> <!-- 單向一對多關聯,不作級聯刪除或更新操作,對部門實體的操作,還是手動進行。查出用戶對象的同時,一定要查出部門對象 --> <many-to-one name="dept" class="com.amarsoft.apm.model.Dept" fetch="select" cascade="none" lazy="false"> <column name="DEPT_ID" length="18"> </column> </many-to-one> <property name="userName" type="string"> <column name="USER_NAME" length="300"> </column> </property> <property name="userPass" type="string"> <column name="USER_PASS" length="300"> </column> </property> <property name="userStatus" type="string"> <column name="USER_STATUS" length="20"> </column> </property> <property name="email" type="string"> <column name="EMAIL" length="300"> </column> </property> <property name="mobilePhone" type="string"> <column name="MOBILE_PHONE" length="300"> </column> </property> <property name="fcu" type="string"> <column name="FCU" length="18"> </column> </property> <property name="fcd" type="string"> <column name="FCD" length="18"> </column> </property> <property name="fct" type="string"> <column name="FCT" length="20"> </column> </property> <property name="luu" type="string"> <column name="LUU" length="18"> </column> </property> <property name="lud" type="string"> <column name="LUD" length="18"> </column> </property> <property name="lut" type="string"> <column name="LUT" length="20"> </column> </property> </class> </hibernate-mapping> <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2015-3-13 8:24:33 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.amarsoft.apm.model.Dept" table="APM_DEPT"> <id name="deptId" type="string"> <column name="DEPT_ID" length="18" /> <generator class="assigned" /> </id> <property name="deptName" type="string"> <column name="DEPT_NAME" length="300"> </column> </property> <property name="deptLevel" type="big_decimal"> <column name="DEPT_LEVEL" precision="22" scale="0"> </column> </property> <property name="deptStatus" type="string"> <column name="DEPT_STATUS" length="20"> </column> </property> <property name="fcu" type="string"> <column name="FCU" length="18"> </column> </property> <property name="fcd" type="string"> <column name="FCD" length="18"> </column> </property> <property name="fct" type="string"> <column name="FCT" length="20"> </column> </property> <property name="luu" type="string"> <column name="LUU" length="18"> </column> </property> <property name="lud" type="string"> <column name="LUD" length="18"> </column> </property> <property name="lut" type="string"> <column name="LUT" length="20"> </column> </property> </class> </hibernate-mapping>
用戶類,部門類代碼如下:
package com.amarsoft.apm.model;
/**
* 用戶類
* @author yangsong
*
*/
public class User implements java.io.Serializable {
private static final long serialVersionUID = 3393279804743607950L;
private String userId;
private Dept dept;
private String userName;
private String userPass;
private String userStatus;
private String email;
private String mobilePhone;
private String fcu;
private String fcd;
private String fct;
private String luu;
private String lud;
private String lut;
public User() {
}
public User(String userId) {
this.userId = userId;
}
public User(String userId, Dept dept, String userName, String userPass,
String userStatus, String email, String mobilePhone, String fcu,
String fcd, String fct, String luu, String lud, String lut) {
this.userId = userId;
this.dept = dept;
this.userName = userName;
this.userPass = userPass;
this.userStatus = userStatus;
this.email = email;
this.mobilePhone = mobilePhone;
this.fcu = fcu;
this.fcd = fcd;
this.fct = fct;
this.luu = luu;
this.lud = lud;
this.lut = lut;
}
public String getUserId() {
return this.userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Dept getDept() {
return this.dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPass() {
return this.userPass;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
public String getUserStatus() {
return this.userStatus;
}
public void setUserStatus(String userStatus) {
this.userStatus = userStatus;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobilePhone() {
return this.mobilePhone;
}
public void setMobilePhone(String mobilePhone) {
this.mobilePhone = mobilePhone;
}
public String getFcu() {
return this.fcu;
}
public void setFcu(String fcu) {
this.fcu = fcu;
}
public String getFcd() {
return this.fcd;
}
public void setFcd(String fcd) {
this.fcd = fcd;
}
public String getFct() {
return this.fct;
}
public void setFct(String fct) {
this.fct = fct;
}
public String getLuu() {
return this.luu;
}
public void setLuu(String luu) {
this.luu = luu;
}
public String getLud() {
return this.lud;
}
public void setLud(String lud) {
this.lud = lud;
}
public String getLut() {
return this.lut;
}
public void setLut(String lut) {
this.lut = lut;
}
}
package com.amarsoft.apm.model;
import java.math.BigDecimal;
/**
* 部門類
* @author yangsong
*
*/
public class Dept implements java.io.Serializable {
private static final long serialVersionUID = -1210910164577152302L;
private String deptId;
private String deptName;
private BigDecimal deptLevel;
private String deptStatus;
private String fcu;
private String fcd;
private String fct;
private String luu;
private String lud;
private String lut;
public Dept() {
}
public Dept(String deptId) {
this.deptId = deptId;
}
public Dept(String deptId, String deptName, BigDecimal deptLevel,
String deptStatus, String fcu, String fcd, String fct, String luu,
String lud, String lut) {
this.deptId = deptId;
this.deptName = deptName;
this.deptLevel = deptLevel;
this.deptStatus = deptStatus;
this.fcu = fcu;
this.fcd = fcd;
this.fct = fct;
this.luu = luu;
this.lud = lud;
this.lut = lut;
}
public String getDeptId() {
return this.deptId;
}
public void setDeptId(String deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return this.deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public BigDecimal getDeptLevel() {
return this.deptLevel;
}
public void setDeptLevel(BigDecimal deptLevel) {
this.deptLevel = deptLevel;
}
public String getDeptStatus() {
return this.deptStatus;
}
public void setDeptStatus(String deptStatus) {
this.deptStatus = deptStatus;
}
public String getFcu() {
return this.fcu;
}
public void setFcu(String fcu) {
this.fcu = fcu;
}
public String getFcd() {
return this.fcd;
}
public void setFcd(String fcd) {
this.fcd = fcd;
}
public String getFct() {
return this.fct;
}
public void setFct(String fct) {
this.fct = fct;
}
public String getLuu() {
return this.luu;
}
public void setLuu(String luu) {
this.luu = luu;
}
public String getLud() {
return this.lud;
}
public void setLud(String lud) {
this.lud = lud;
}
public String getLut() {
return this.lut;
}
public void setLut(String lut) {
this.lut = lut;
}
}
測試代碼如下:
@Test
public void testUser(){
DateFormat df = new SimpleDateFormat("yyy/MM/dd HH:mm:ss");
try {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
SessionFactory sf = configuration.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
//新增測試
User user = new User();
user.setUserId("demo001");
user.setUserName("示例測試");
user.setFct(df.format(new Date()));
Dept dept = new Dept();
dept.setDeptId("10");
dept.setDeptName("示例總部");
dept.setFct(df.format(new Date()));
user.setDept(dept);
session.saveOrUpdate(dept);//部門對象自行保存,防止通過user對象級聯後被篡改
session.saveOrUpdate(user);
//查詢實體
String hql = "from User where userId=:userId";
Query query=session.createQuery(hql);
query.setString("userId", user.getUserId());
User user1 = (User)query.uniqueResult();
Assert.assertNotNull(user1);
Assert.assertEquals(user.getUserName(), user1.getUserName());
Assert.assertNotNull(user1.getDept());
Assert.assertEquals(user.getDept().getDeptName(), user1.getDept().getDeptName());
tx.commit();
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
自己做個筆記記錄下,同時,也給有需要或學習的同學參考。