-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: INSERT INTO TYPE ISSUE
PostPosted: Wed Jun 13, 2007 3:56 pm 
Beginner
Beginner

Joined: Wed Oct 29, 2003 10:43 am
Posts: 38
Location: Chicago
Iget the following:
13-06-2007 14:52:11:637 ERROR InsertIntoTest main insertion type [org.hibernate.type.LongType@2e1f1f] and selection type [org.hibernate.type.IntegerType@1fd6bea] at position 0 are not compatible [Insert into Population( sessionCacheNumber, itemNumber ) SELECT 1, itemTable.itemNumber FROM ItemTable itemTable]
org.hibernate.QueryException: insertion type [org.hibernate.type.LongType@2e1f1f] and selection type [org.hibernate.type.IntegerType@1fd6bea] at position 0 are not compatible [Insert into Population( sessionCacheNumber, itemNumber ) SELECT 1, itemTable.itemNumber FROM ItemTable itemTable]
at org.hibernate.hql.ast.tree.IntoClause.validateTypes(IntoClause.java:97)
at org.hibernate.hql.ast.tree.InsertStatement.validate(InsertStatement.java:34)
at org.hibernate.hql.ast.HqlSqlWalker.postProcessInsert(HqlSqlWalker.java:603)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.insertStatement(HqlSqlBaseWalker.java:491)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:253)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at InsertIntoTest.main(InsertIntoTest.java:31)

code:
Session session =
new AnnotationConfiguration( )
.addAnnotatedClass( Population.class )
.addAnnotatedClass( ItemTable.class )
.configure( "TestHibernate.cfg.xml" )
.buildSessionFactory( ).openSession( );
Transaction transaction = session.beginTransaction( );

session.createQuery(
"Insert into Population( sessionCacheNumber, itemNumber ) " +
"SELECT 1, itemTable.itemNumber FROM ItemTable itemTable" )
.executeUpdate( );


transaction.commit( );
session.close( );

ItemTable.java
@Entity
@Table( name = "item_table" )
public class ItemTable implements java.io.Serializable
{
@Id
@Column( name="item_No", length=65 )
private int itemNumber;

@Column( name="description", length=80 )
private String description;
}

Population.java
@Entity
@IdClass( PopulationPk.class )
@Table( name = "Population" )
public class Population extends PopulationBase
{

@Id
private Long sessionCacheNumber;

@Id
private String itemNumber;
}

PopulationPk.java
@Embeddable
public class PopulationPk extends PopulationBase
{
@Column( name="session_cache_number", precision=18 )
private Long sessionCacheNumber;

@Column( name="item_No", length=65 )
private String itemNumber;
}

PopulationBase.java
public abstract Long getSessionCacheNumber( );

public abstract void setSessionCacheNumber( Long sessionCacheNumber );

public abstract String getItemNumber( );

public abstract void setItemNumber( String itemNumber );

public String toString( )
{
return new ToStringBuilder( this )
.append( "sessionCacheNumber", getSessionCacheNumber( ) )
.append( "itemNumber", getItemNumber( ) )
.toString( );
}

public boolean equals( Object other )
{
if ( !(other instanceof PopulationBase) ) return false;
PopulationBase castOther = (PopulationBase) other;
return new EqualsBuilder( )
.append( getSessionCacheNumber( ), castOther.getSessionCacheNumber( ) )
.append( getItemNumber( ), castOther.getItemNumber( ) )
.isEquals( );
}

public int hashCode( )
{
return new HashCodeBuilder( )
.append( getSessionCacheNumber( ) )
.append( getItemNumber( ) )
.toHashCode( );
}

I need to generate the insert into and dynamically set the sessionCacheNumber. Is there a way to get hibernate to accept it as a long?

_________________
LET IT BE :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 2:19 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,

>>> "SELECT 1, itemTable.itemNumber FROM ItemTable itemTable"
Replace with
"SELECT 1L, itemTable.itemNumber FROM ItemTable itemTable"

--------------------------------------------
Rate the reply if you find it helpful


Top
 Profile  
 
 Post subject: Re: INSERT INTO TYPE ISSUE
PostPosted: Wed Apr 28, 2010 10:51 am 
Newbie

Joined: Thu Jun 25, 2009 7:22 am
Posts: 9
Hi,

I've also an type issue in an insert statement, but mine seems to be harder to solve.

I simplified the example as much as possible. Image the following 2 classes:
Type(int id, ...)
Obj(int id, Type type, ...)

What I want to do is to insert a new Obj with a certain value for "type". In SQL I can write
Code:
insert into Obj(type, ...) select 1, ...

in case that I know the value of type has to be 1.

In HQL this seems to be much harder. I cannot simply write
Code:
insert into Obj(type, ...) select 1, ...

without getting a org.hibernate.QueryException: insertion type [org.hibernate.type.ManyToOneType(Type)] and selection type [org.hibernate.type.IntegerType@314af9f7] at position 0 are not compatible [...]

I know that it's possible to cast int to long via "cast(1 as long)". However casting 1 to Type does not work. In this case I get a null pointer exception when I during the creation of the query.

The only workaround I found is to include the Type in the select statement:
Code:
insert into Obj(type, ...) select t, ... from ..., Type t where t.id = 1

But then I assume an unnecessary join is going to be constructed. For a better performance I would like to get rid of this join. Thus, is it somehow possible to work directly with the IDs of persistent objects instead of having to resolve the objects which leads to an unnecessary join?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.