Hello,
My problem is that I want to force an HibernateDAOException whenever I try to save an object which should have a property with a distinct value but it has not (login). At the moment, I get a JDBCException thrown due to the restriction imposed by the constraint in the database. This JDBSException is not sent back in my framework (and I think it shouldn't). So I can't make the exception treatment in order to make the frontend know which is the data error to be corrected. The question is: "How do I replicate the same constraint existent in the database in my Hibernate mapping?"
I use MySQL 5.0.22-log and hibernate-3.1
My SQL Create Script is:
CREATE TABLE `dbo_STS_RECURS` (
`CD_RECURS` int(11) NOT NULL default '0',
`CD_TP_RECURS` int(11) NOT NULL default '0',
`DS_REGIME_RECURS` varchar(50) NOT NULL,
`DT_ADMS_RECURS` datetime NOT NULL default '0000-00-00 00:00:00',
`DT_DESLIG_RECURS` datetime default NULL,
`NR_CENCUS_RECURS` int(11) NOT NULL default '0',
`NR_RAMAL_RECURS` varchar(4) default '',
`TX_LOGIN_RECURS` varchar(20) NOT NULL default '''',
`TX_SENHA_RECURS` varchar(30) NOT NULL default '''',
PRIMARY KEY (`CD_RECURS`),
UNIQUE KEY `IX_STS_RECURS_01` (`TX_LOGIN_RECURS`),
KEY `FK_STS_RECURS_01` (`CD_TP_RECURS`),
KEY `FK_STS_RECURS_02` (`CD_RECURS`),
CONSTRAINT `FK_STS_RECURS` FOREIGN KEY (`CD_TP_RECURS`) REFERENCES `dbo_STS_TP_RECURS` (`CD_TP_RECURS`) ON UPDATE CASCADE,
CONSTRAINT `FK_STS_RECURS_02` FOREIGN KEY (`CD_RECURS`) REFERENCES `dbo_STS_PESSOA` (`CD_PESSOA`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
My mapping hbm is:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="br.com.adj.jproject.model.entity">
<subclass
name="RecursoTO"
extends="PessoaTO"
discriminator-value="1">
<join table="dbo_STS_RECURS">
<key column="CD_RECURS"/>
<property
name="login"
type="string"
column="TX_LOGIN_RECURS"
not-null="true"
unique="true"
index="IX_STS_RECURS_O1"
/>
<property
name="ramal"
type="string"
column="NR_RAMAL_RECURS"
not-null="false"
/>
<property
name="senha"
type="string"
column="TX_SENHA_RECURS"
not-null="true"
/>
<property
name="admissao"
type="date"
column="DT_ADMS_RECURS"
not-null="true"
/>
<property
name="desligamento"
type="date"
column="DT_DESLIG_RECURS"
not-null="false"
/>
<property
name="centrocusto"
type="string"
column="NR_CENCUS_RECURS"
not-null="true"
/>
<many-to-one
name="funcaoTO"
column="CD_TP_RECURS"
class="br.com.adj.jproject.model.entity.FuncaoTO"
not-null="true"
insert="true"
update="true"
lazy="false"
/>
<many-to-one
name="regimeVO"
column="DS_REGIME_RECURS"
class="br.com.adj.jproject.model.entity.valueObject.RegimeVO"
not-null="true"
insert="true"
update="true"
lazy="false"
/>
</join>
</subclass>
</hibernate-mapping>
My hibernate properties is:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="java:hibernate/SessionFactory">
<property name="hibernate.connection.datasource">java:jdbc/JPROJECT</property>
<property name="hibernate.default_schema">JPROJECT</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.cglib.use_reflection_optimizer">false</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.use_outer_join">false</property>
<mapping resource="br/com/adj/jproject/model/persistence/rdbms/hibernate/hbm/RecursoTO.hbm.xml"/>
</session-factory>
</hibernate-configuration>
|