Re: Problems with Hibernate Discriminators and 9.0-801.jdbc4 - Mailing list pgsql-jdbc
From | Dave Cramer |
---|---|
Subject | Re: Problems with Hibernate Discriminators and 9.0-801.jdbc4 |
Date | |
Msg-id | AANLkTimMurS6+1EYQPd-FGYUa1Nv0Z54Oe-E=B7u9hwn@mail.gmail.com Whole thread Raw |
In response to | Re: Problems with Hibernate Discriminators and 9.0-801.jdbc4 (sdavidr <david.ricoma@gmail.com>) |
List | pgsql-jdbc |
On Mon, Mar 28, 2011 at 9:05 AM, sdavidr <david.ricoma@gmail.com> wrote: > Ok, that's the hibernate's code that is failing. As it says :Extract the > value from the result set (which is assumed to already have been positioned > to the apopriate row). > > Hibernate could receive a lot of numbers from a "returning *" but doesn't > know what is the correct value. It expects to be the first element, but if > is not, it fails. Normally, the id is the first returned element by > postgres, but in some tables that is not true -> when there is a string > discriminator in the first column. > > In another way, method from postgres driver getGeneratedKeys cannot return > the full table, isn't it? > What code is this ? Is this a hibernate class ? If so which one? > > /** > * Extract the value from the result set (which is assumed to already have > been positioned to the apopriate row) > * and wrp it in the appropriate Java numeric type. > * > * @param rs The result set from which to extract the value. > * @param type The expected type of the value. > * @return The extracted value. > * @throws SQLException Indicates problems access the result set > * @throws IdentifierGenerationException Indicates an unknown type. > */ > public static Serializable get(ResultSet rs, Type type) throws > SQLException, IdentifierGenerationException { > if ( ResultSetIdentifierConsumer.class.isInstance( type ) ) { > return ( ( ResultSetIdentifierConsumer ) type ).consumeIdentifier( rs ); > } > if ( CustomType.class.isInstance( type ) ) { > final CustomType customType = (CustomType) type; > if ( ResultSetIdentifierConsumer.class.isInstance( > customType.getUserType() ) ) { > return ( (ResultSetIdentifierConsumer) customType.getUserType() > ).consumeIdentifier( rs ); > } > } > > Class clazz = type.getReturnedClass(); > if ( clazz == Long.class ) { > return new Long( rs.getLong( 1 ) ); > } > else if ( clazz == Integer.class ) { > return new Integer( rs.getInt( 1 ) ); > } > else if ( clazz == Short.class ) { > return new Short( rs.getShort( 1 ) ); > } > else if ( clazz == String.class ) { > return rs.getString( 1 ); > } > else if ( clazz == BigInteger.class ) { > return rs.getBigDecimal( 1 ).setScale( 0, BigDecimal.ROUND_UNNECESSARY > ).toBigInteger(); > } > else if ( clazz == BigDecimal.class ) { > return rs.getBigDecimal( 1 ).setScale( 0, BigDecimal.ROUND_UNNECESSARY ); > } > else { > throw new IdentifierGenerationException( > "unrecognized id type : " + type.getName() + " -> " + clazz.getName() > ); > } > } > The problem of course is that the driver is trying to solve the general problem which is to return all generated values, whether they be the result of sequences, triggers or rules. The simplest solution is to return all of the columns since it is impossible for the driver to determine what is generated by the backend and what is not. The code above is assuming (incorrectly) that the first column is the identity column. Dave Cramer dave.cramer(at)credativ(dot)ca http://www.credativ.ca
pgsql-jdbc by date: