Hi:
I´m adding annotations to a project that used XDoclet, and I´m having trouble migrating a many to many association from XML to annotations.
This is a extract from my POJO without annotations:
Code:
**
 * @hibernate.class 
 *   table="SEGU_COLECCION_FACULTADES"
 *   proxy="com.bursatec.seguridad.persistence.model.ColeccionFacultades"
 */
public class ColeccionFacultadesImpl implements ColeccionFacultades {
    private static final long serialVersionUID = 1L;
    /**
     * El identificador de la colección de facultades
     */
    private Long id;
    /**
     * Mapa de las facultades asociadas
     */
    private Map facultades;
    
    public Map getFacultades() {
        return this.facultades;
    }
    public void setFacultades(Map facultades) {
        this.facultades = facultades;
    }...
And here is the POJO with annotations
Code:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "SEGU_COLECCION_FACULTADES")
@SequenceGenerator(name = "foliador", sequenceName = "SEQ_SEGU_COLECCION_FACULTADES")
public class ColeccionFacultadesImpl implements ColeccionFacultades {
    @Transient
    private static final long serialVersionUID = 1L;
    /**
     * El identificador de la colección de facultades
     */
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "foliador")
    @Column(name = "ID_COL")
    private Long id;
    /**
     * Mapa de las facultades asociadas
     */
    @MapKey( name = "nombre")
    @ManyToMany(targetEntity = com.bursatec.seguridad.persistence.model.impl.FacultadImpl.class, cascade = {CascadeType.ALL})
    @JoinTable(name = "SEGU_FACULTAD_COLECCION", joinColumns = {@JoinColumn(name = "ID_COL")}, inverseJoinColumns = {@JoinColumn(name = "ID_FACULTAD")})
    @IndexColumn(name = "FACULTAD_KEY", nullable = false, columnDefinition="VARCHAR2 NOT NULL")
    private Map facultades;
...
In the XDoclet version, the association is mapped with this file:
<map name="facultades" cascade="save-update"
	table="SEGU_FACULTAD_COLECCION">
	<key column="ID_COL" />
	<index column="FACULTAD_key" type="string" />
	<many-to-many
		class="com.bursatec.seguridad.persistence.model.impl.FacultadImpl"
		column="ID_FACULTAD" />
</map>
The table "SEGU_FACULTAD_COLECCION" has a not nullable column named "FACULTAD_key", in the XDoclet version it works fine inserting the right values in the db, as the following log shows:
2007-11-14 10:36:01,724 [main] DEBUG org.hibernate.SQL - insert into SEGU_FACULTAD_COLECCION (ID_COL, FACULTAD_key, ID_FACULTAD) values (?, ?, ?)
2007-11-14 10:36:01,724 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - reusing prepared statement
2007-11-14 10:36:01,724 [main] DEBUG org.hibernate.SQL - insert into SEGU_FACULTAD_COLECCION (ID_COL, FACULTAD_key, ID_FACULTAD) values (?, ?, ?)
2007-11-14 10:36:01,724 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - reusing prepared statement
2007-11-14 10:36:01,724 [main] DEBUG org.hibernate.SQL - insert into SEGU_FACULTAD_COLECCION (ID_COL, FACULTAD_key, ID_FACULTAD) values (?, ?, ?)
2007-11-14 10:36:01,724 [main] DEBUG org.hibernate.persister.collection.AbstractCollectionPersister - done inserting collection: 3 rows inserted
2007-11-14 10:36:01,724 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - Executing batch size: 3
2007-11-14 10:36:01,724 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - success of batch update unknown: 0
2007-11-14 10:36:01,724 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - success of batch update unknown: 1
2007-11-14 10:36:01,724 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - success of batch update unknown: 2
2007-11-14 10:36:01,724 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2007-11-14 10:36:01,739 [main] DEBUG org.hibernate.transaction.JDBCTransaction - re-enabling autocommit
2007-11-14 10:36:01,739 [main] DEBUG org.hibernate.transaction.JDBCTransaction - committed JDBC Connection
2007-11-14 10:36:01,739 [main] DEBUG org.hibernate.jdbc.ConnectionManager - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
2007-11-14 10:36:01,739 [main] DEBUG org.hibernate.impl.SessionImpl - disconnecting session
2007-11-14 10:36:01,739 [main] DEBUG org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2007-11-14 10:36:01,755 [main] DEBUG org.hibernate.jdbc.ConnectionManager - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
But in the annotations version the "FACULTAD_key" column is ignored and an exception is raised:
2007-11-14 10:25:37,382 [main] DEBUG org.hibernate.SQL - insert into SEGU_FACULTAD_COLECCION (ID_COL, ID_FACULTAD) values (?, ?)
Hibernate: insert into SEGU_FACULTAD_COLECCION (ID_COL, ID_FACULTAD) values (?, ?)
2007-11-14 10:25:37,398 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2007-11-14 10:25:37,398 [main] DEBUG org.hibernate.util.JDBCExceptionReporter - could not insert collection: [com.bursatec.seguridad.persistence.model.impl.ColeccionFacultadesImpl.facultades#1010652] [insert into SEGU_FACULTAD_COLECCION (ID_COL, ID_FACULTAD) values (?, ?)]
java.sql.SQLException: ORA-01400: cannot insert NULL into ("ADMINWLES2"."SEGU_FACULTAD_COLECCION"."FACULTAD_KEY")
The value of the Map is assigned in child classes of the POJO (in the addFacultad method):
Code:
@Entity
@Table(name = "SEGU_ROL")
@PrimaryKeyJoinColumn(name = "ID_COL")
public class RolImpl extends ColeccionFacultadesImpl implements Rol {
   
   
    @Column(name = "NOMBRE", length = 20)
    private String nombre;
   
   
    public String getNombre() {
        return this.nombre;
    }
   
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
   
   
    public void addFacultad(Facultad facultad) {
       if(this.getFacultades() == null) {
          this.setFacultades(new HashMap());
       }
       this.getFacultades().put(facultad.getNombre(), facultad);
    }
}
This is my configuration:
Hibernate Core 3.2.2.ga
Annotations 3.3.0.ga
Oracle Dialect
I´ve been looking in the documentation and the forums for a couple of days, I believe the the "translated" mapping is right (but maybe I´m wrong) since I can retrieve the Map from a test case that selects it, the problem is in the insertion part.
Any help would be apreciated.