-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate generating invalid query on @JoinColumns
PostPosted: Tue Feb 25, 2014 5:02 pm 
Newbie

Joined: Tue Feb 25, 2014 12:02 pm
Posts: 3
EDIT: See post #2 for the cause of the problem.

Since migrating from Glassfish to Wildfly (and thus Hibernate 4.2.3 to 4.3.1) I am experiencing an issue that wasn't present before the migration. When a particular named query gets executed, sometimes we get an exception, other times it works fine. The query in question is:

SELECT ase FROM AgentStateEntity ase WHERE (ase.agent = :agent OR ase.agent IS NULL) AND ase.pbxEvent.eventTime >= :date ORDER BY ase.pbxEvent DESC

A snippet of the enormous stack trace is:
HHH000327: Error performing load command : org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'primarykey'.

By debugging, I verified that the values for the :agent and :date are valid so the query should execute without problem. It was and still is working on GF with Hibernate 4.2.3. The jdbc driver is the same and most recent for SQL Server 2008.

Here are the important parts of the entities that are involved in the query.

Code:
@MappedSuperclass
public abstract class ResourceStateEntity implements Serializable, IResourceState, IDTO
{
   @ManyToOne
   @JoinColumn(name = "pbxeventforeignkey", nullable = false, insertable = true, updatable = false)
   private PbxEventEntity   pbxEvent;
}

@NamedQuery(name = "AgentStateEntity.getCurrentState", query = "SELECT ase FROM AgentStateEntity ase WHERE (ase.agent = :agent OR ase.agent IS NULL) AND ase.pbxEvent.eventTime >= :date ORDER BY ase.pbxEvent DESC"))
@Entity
@Table(name = "dbo.agentstates")
public class AgentStateEntity extends ResourceStateEntity implements Serializable, IAgentState
{
   @Id
   @Column(name = "primarykey", nullable = false, insertable = true, updatable = false)
   private long            primaryKey;
   
   @ManyToOne
   @JoinColumn(name = "agentforeignkey", nullable = true, insertable = true, updatable = false)
   private AgentEntity         agent            = null;
}

@Entity
@Table(name = "dbo.pbxevents")
public class PbxEventEntity implements IPbxEvent, Serializable, IDTO
{
   private static final long serialVersionUID   = 1L;

   @Id
   @Column(name = "primarykey", nullable = false)
   private long primaryKey = 0L;

   @Temporal(TemporalType.TIMESTAMP)
   @Column(name = "eventtime", nullable = false)
   private Calendar eventTime = null;
}

@Entity
@DiscriminatorValue("1")
public class AgentEntity extends PbxResourceEntity implements IAgent
{
   //primarykey in base class
}

@Entity
@Table(name = "dbo.pbxresources")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
public class PbxResourceEntity implements IResource, Identifiable, Serializable
{
   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   @Column(name = "primarykey", nullable = false, insertable = true, updatable = false)
   private int primaryKey = 0;
}


Last edited by Cain05 on Wed Feb 26, 2014 3:38 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Invalid column name 'primarykey' when using named query
PostPosted: Wed Feb 26, 2014 11:46 am 
Newbie

Joined: Tue Feb 25, 2014 12:02 pm
Posts: 3
Ok so I found the cause of the problem. ResourceStateEntity, the base class of AgentStateEntity, has a one to one mapping that is being loaded when the AgentStateEntity is retrieved from the database.

Code:
@OneToOne
@JoinColumns({ 
      @JoinColumn(name="connectionstateconnectionforeignkey", referencedColumnName="connectionforeignkey"), 
      @JoinColumn(name="connectionstatepbxeventforeignkey", referencedColumnName="pbxeventforeignkey") 
    })
   private ConnectionStateEntity connectionState;


If I remove this mapping then the error goes away. ConnectionStateEntity and it's primary key looks like this:

Code:
@Entity
@Table(name = "dbo.connectionstates")
public class ConnectionStateEntity extends ConnectState implements Serializable, IConnectionState, IDTO
{
   private static final long serialVersionUID = 1L;

   @EmbeddedId
   private ConnectionStateEntityPrimaryKey primaryKey = null;

   @ManyToOne
   @JoinColumn(name = "pbxeventforeignkey", nullable = false, insertable = false, updatable = false)
   private PbxEventEntity event = null;

   @ManyToOne
   @JoinColumn(name = "connectionforeignkey", nullable = false, insertable = false, updatable = false)
   private ConnectionEntity connection = null;
}


@Embeddable
public class ConnectionStateEntityPrimaryKey implements Serializable
{
   private static final long serialVersionUID = 1L;

   @Column(name = "connectionforeignkey", nullable = false, insertable = true, updatable = false)
   private long connectionForeignKey = 0L;

   @Column(name = "pbxeventforeignkey", nullable = false, insertable = true, updatable = false)
   private long pbxEventForeignKey = 0L;
}


EDIT: Ok the problem is with the order by clause:

order by events3_.primarykey asc

events3_ refers to callevents, which doesn't have a primarykey column. the two columns it does have are callforeignkey and pbxeventforeignkey

Code:
select connection0_.connectionforeignkey as connecti1_32_0_,
       connection0_.pbxeventforeignkey as pbxevent2_32_0_,
       connection0_.state as state3_32_0_,
       connection1_.primarykey as primaryk1_31_1_,
       connection1_.callforeignkey as callfore6_31_1_,
       connection1_.completetime as complete2_31_1_,
       connection1_.connecttime as connectt3_31_1_,
       connection1_.pbxresourceforeignkey as pbxresou7_31_1_,
       connection1_.starttime as starttim4_31_1_,
       connection1_.talktime as talktime5_31_1_,
       callentity2_.primarykey as primaryk1_23_2_,
       callentity2_.talktime as talktime2_23_2_,
       callentity2_.timetoabandon as timetoab3_23_2_,
       callentity2_.timetoanswer as timetoan4_23_2_,
       events3_.callforeignkey as callfore1_23_3_,
       pbxeventen4_.primarykey as pbxevent2_7_3_,
       pbxeventen4_.primarykey as primaryk1_43_4_,
       pbxeventen4_.cause as cause2_43_4_,
       pbxeventen4_.eventtime as eventtim3_43_4_,
       pbxeventen4_.opcode as opcode4_43_4_,
       pbxresourc5_.primarykey as primaryk2_45_5_,
       pbxresourc5_.abbreviation as abbrevia3_45_5_,
       pbxresourc5_.currentmode as currentm4_45_5_,
       pbxresourc5_.description as descript5_45_5_,
       pbxresourc5_.id as id6_45_5_,
       pbxresourc5_.type as type1_45_5_,
       pbxresourc5_.updateversion as updateve7_45_5_,
       pbxresourc5_.modularmessaging as modularm8_45_5_,
       pbxresourc5_.tsftime as tsftime9_45_5_,
       pbxresourc5_.extensiontype as extensi10_45_5_,
       pbxeventen6_.primarykey as primaryk1_43_6_,
       pbxeventen6_.cause as cause2_43_6_,
       pbxeventen6_.eventtime as eventtim3_43_6_,
       pbxeventen6_.opcode as opcode4_43_6_
from dbo.connectionstates connection0_
inner join dbo.connections connection1_ on
   connection0_.connectionforeignkey=connection1_.primarykey
inner join dbo.calls callentity2_ on
   connection1_.callforeignkey=callentity2_.primarykey
left outer join callevents events3_ on
   callentity2_.primarykey=events3_.callforeignkey
left outer join dbo.pbxevents pbxeventen4_ on
   events3_.pbxeventforeignkey=pbxeventen4_.primarykey
inner join dbo.pbxresources pbxresourc5_ on
   connection1_.pbxresourceforeignkey=pbxresourc5_.primarykey
inner join dbo.pbxevents pbxeventen6_ on
   connection0_.pbxeventforeignkey=pbxeventen6_.primarykey
where connection0_.connectionforeignkey=? and
      connection0_.pbxeventforeignkey=?
order by events3_.primarykey asc


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.