Fabio Cepeda

I develop a web application using appfuse, springframework, and hibernate, MySQL . I had the following situation:

I needed to persiste a collection of unique integers for an object. Where the primary Id of the collection table was the primary key of the table mapped by Contacto.

public class Contacto implements java.io.Serializable {

private Integer contactoId; //Primary key of this object
Set private Set beneficiarioTitularDe = new HashSet(); // Collection of unique integers.

//Getters and setters omitted for simplicity
}

Here is the db table script

CREATE TABLE `beneficiariosprimariosinmuebles` (
`contactoID` int(10) unsigned NOT NULL default ‘0’,
`inmuebleID` int(10) unsigned NOT NULL default ‘0’,
PRIMARY KEY (`contactoID`, inmuebleID),
KEY `beneficiariosprimariosinmuebles_FKIndex1` (`contactoID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

#
# Foreign keys for table propietariosinmuebles
#

ALTER TABLE `beneficiariosprimariosinmuebles`
ADD FOREIGN KEY (`contactoID`) REFERENCES `contactos` (`contactoID`) ON UPDATE NO ACTION;

Here is the mapping file of hibernate:

Trying to persist this kind of collection was a challenge in the case where the field inmuebleID was null because it was a primay key of my collection table in the database. Reason why I did this in the database was to assure unique integers in the DB. However, for some reason hibernate was not able to delete the record from the database because the inmuebleID was the primary key in the DB table.

The solution I came up with was to assure the Set did not have null values before calling the save method on the onBind method of my contoller.

Advertisements

Vamos a empezar a anotar mis descubrimientos. Tecnologicos y personales para tenerlos de referencia despues. Los personales seran en Español y los Tecnologicos en Ingles 😛 solo para practicar.