[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-5160) Error on binding values prepared statment in many-to-any relation

Julien Fourment (JIRA) noreply at atlassian.com
Tue Apr 17 08:50:48 EDT 2012


    [ https://hibernate.onjira.com/browse/HHH-5160?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=46318#comment-46318 ] 

Julien Fourment commented on HHH-5160:
--------------------------------------

Hi,

Exact same problem encountered in Hibernate 3.6.8. 
Has this issue been addressed in a different bug report and/or a later version? 

Best regards!
Julien


> Error on binding values prepared statment in many-to-any relation
> -----------------------------------------------------------------
>
>                 Key: HHH-5160
>                 URL: https://hibernate.onjira.com/browse/HHH-5160
>             Project: Hibernate ORM
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 3.3.2, 3.5.1
>         Environment: Tested on Version 3.3.2 and 3.5.1 on Oracle, PostgreSQL and MySQL
>            Reporter: wof
>         Attachments: ManyToAnyBug.zip
>
>
> A Collection of Entities is defined as a ManyToAny relation, e.g.
> @ManyToAny(metaColumn=@Column(name="C_OBJECT_TYPE"))
> @AnyMetaDef(idType="string", metaType="string",
>     metaValues={
>         @MetaValue(targetEntity=Entity1.class, value="Entity1"),
>         @MetaValue(targetEntity=Entity2.class, value="Entity2")
> })
> @JoinTable(name="T_OBJECTS",
>    joinColumns=@JoinColumn(name="C_OWNER_ID"),
>    inverseJoinColumns=@JoinColumn(name="C_OBJECT_ID")
> )
> private Set<EntityBase> objects = new HashSet<EntityBase>();
> Adding to this set works without problems. Removing all elements from the set (i.e. setting the set to null or zero-sized), too. However, when one or more elements are removed from the set with at least one element remaining, an exception is thrown:
> org.hibernate.exception.GenericJDBCException: could not delete collection rows with the cause being java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
> I have narrowed this problem to this: The mapping table consists of 3 columns, C_OWNER_ID, C_OBJECT_TYPE and C_OBJECT_ID. The generated SQL-statement to delete the rows is as folling:
> DELETE FROM t_objects WHERE c_owner_id=? AND c_object_id=?
> However, Hibernate (in org.hibernate.typ.AnyType) tries to bind the actual value for c_object_id with index 3 which causes the exception. I guess Hibernate expects c_object_type to be present in the statement.
> As workaround we changed nullSafeSet in AnyType to:
> 	public void nullSafeSet(PreparedStatement st, Object value,	int index, boolean[] settable, SessionImplementor session)
> 	throws HibernateException, SQLException {
> 		Serializable id;
> 		String entityName;
> 		if (value==null) {
> 			id=null;
> 			entityName=null;
> 		}
> 		else {
> 			entityName = session.bestGuessEntityName(value);
> 			id = ForeignKeys.getEntityIdentifierIfNotUnsaved(entityName, value, session);
> 		}
> 		// metaType is assumed to be single-column type
>         // <ORIGINAL CODE>
>         /*
> 		if ( settable==null || settable[0] ) {
> 			metaType.nullSafeSet(st, entityName, index, session);
> 		}
> 		if (settable==null) {
> 			identifierType.nullSafeSet(st, id, index+1, session);
> 		}
> 		else {
> 			boolean[] idsettable = new boolean[ settable.length-1 ];
> 			System.arraycopy(settable, 1, idsettable, 0, idsettable.length);
> 			identifierType.nullSafeSet(st, id, index+1, idsettable, session);
> 		}
>          */
>         // </ORIGINAL CODE>
>         // <PATCHED CODE>
> 		if ( settable==null || settable[0] ) {
> 			metaType.nullSafeSet(st, entityName, index, session);
> 			boolean[] idsettable = new boolean[ settable.length-1 ];
> 			System.arraycopy(settable, 1, idsettable, 0, idsettable.length);
> 			identifierType.nullSafeSet(st, id, index+1, idsettable, session);
> 		}
>         else if (settable==null) {
> 			identifierType.nullSafeSet(st, id, index+1, session);
> 		}
> 		else {
> 			boolean[] idsettable = new boolean[ settable.length-1 ];
> 			System.arraycopy(settable, 1, idsettable, 0, idsettable.length);
> 			identifierType.nullSafeSet(st, id, index, idsettable, session);
> 		}
>         // </PATCHED CODE>
> 	}
> best regards
> wof

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list