读书人

unique foreign key 地图ping of hibe

发布时间: 2012-10-09 10:21:45 作者: rapoo

unique foreign key mapping of hibernate example

First the ddl language of the table used with foreign key as :

CREATE TABLE `t_operation_log` (  `id` BIGINT(24) NOT NULL AUTO_INCREMENT,  `resourceId` BIGINT(24) NOT NULL,  `systemlogId` BIGINT(24) NOT NULL,  `operationTime` DATE DEFAULT NULL,  PRIMARY KEY (`id`),  KEY `resourceId` (`resourceId`),  KEY `systemlogId` (`systemlogId`),  CONSTRAINT `t_operation_log_fk1` FOREIGN KEY (`systemlogId`) REFERENCES `t_system_log` (`id`),  CONSTRAINT `t_operation_log_fk` FOREIGN KEY (`resourceId`) REFERENCES `t_resource` (`id`))ENGINE=InnoDBAUTO_INCREMENT=4 CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'?

then the xml of the mapping is:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>    <class name="com.alba.permission.domain.Resource" table="t_resource" dynamic-insert="true" dynamic-update="true">    <id name="id" column="id" type="java.lang.Long">        <generator type="string" not-null="true" /><property name="resourceType" type="string" not-null="true" /></class><class name="com.alba.permission.domain.OperationLog" table="t_operation_log" dynamic-insert="true" dynamic-update="true">    <id name="id" column="id" type="java.lang.Long">        <generator type="java.util.Date" />              <many-to-one name="resource" column="RESOURCEID" unique="true" />        <many-to-one name="systemLog" column="SYSTEMLOGID" unique="true"/></class><class name="com.alba.permission.domain.SystemLog" table="t_system_log" dynamic-insert="true" dynamic-update="true">    <id name="id" column="id" type="java.lang.Long">        <generator type="string" not-null="true" /><property name="logonTime" type="java.util.Date" not-null="false" /><property name="logoffTime" type="java.util.Date" not-null="false" /><property name="logonUserId" type="java.lang.Long" not-null="true"/></class></hibernate-mapping>

?

then test code are:

         Resource resource = new Resource();        OperationLog operationLog = new OperationLog();        SystemLog systemLog = new SystemLog();        resource.setUrl(operationUrl);        resource.setResourceType("Button");        facade.getPermissionService().addOperationLog(resource);        systemLog.setLogonUserIp(currentHostIp);        systemLog.setLogonUserId(user.getId());        facade.getPermissionService().addOperationLog(systemLog);        operationLog.setOperationTime(new Date());        operationLog.setResource(resource);        operationLog.setSystemLog(systemLog);                facade.getPermissionService().addOperationLog(operationLog);

?The problem may happend:

????? 1.if before add operationLog ,systemLog and resource log doesnot add,the exception will be thrown

????? 2.here should use many to one,not one to one mapping,remembering

?????

读书人网 >软件架构设计

热点推荐