-->
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.  [ 5 posts ] 
Author Message
 Post subject: Query not mapped/translated correctly
PostPosted: Sat Dec 15, 2007 6:12 pm 
Beginner
Beginner

Joined: Tue Jul 03, 2007 11:11 am
Posts: 22
I have user_identifier table on MySql,
Table has three pk : userId identifierId and value.

And I am trying to query the table by user id but keep getting:
MySQLSyntaxErrorException: Unknown column 'useridenti0_.identifierId' in 'field list'

Seems like hibernate wont translate the filed name "identifierId" correctly (should be 'identifier_Id' in the query insted of identifierId)

On the net people suggested moving the annotations to the private fields instead on the getter itself, but it didn't help.
Any ideas?


Code:
@Id
@Column(name = "USER_ID", nullable = false, length = 10)
private Integer userId;

@Id
@Column(name = "IDENTIFIER_ID", nullable = false, length = 10)
private Integer identifierId;

@Id
@Column(name = "VALUE", nullable = false, length = 20)
private String value;


Hibernate query :

Code:
select
    useridenti0_.identifierId as identifi1_10_,
    useridenti0_.userId as userId10_,
    useridenti0_.value as value10_
from
    schemaName.user_identifier useridenti0_
where
    useridenti0_.userId=?


Last edited by sharonpl on Sun Dec 16, 2007 6:45 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 15, 2007 9:50 pm 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
If you have got a composite primary key it will typically be modelled as a separate class represented by field named id in the entity. You then have to refer to components of the primary key in queries using their full path, e.g. UserIdentifier.id.userId. Without seeing your full entity class definition it is hard to tell you exactly how it should look like.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 16, 2007 6:45 pm 
Beginner
Beginner

Joined: Tue Jul 03, 2007 11:11 am
Posts: 22
Hi again
I found closed bug of the same issue:
http://opensource.atlassian.com/project ... se/EJB-286

As they suggested there I moved the annotations to the PK class, but got the same error.
The Query is generated while ignoring the annotation and column name.

Addressing the filed through the pk like this :
ui.id.userId gave same MySQLSyntaxErrorException

Here is the class source:
Code:
@Entity
@Table(catalog = "schemaName", name = "user_identifier")
@IdClass(com.mycompany.persistence.UserIdentifierPK.class)
public class UserIdentifier {
   @Id
   @Column(name = "USER_ID", nullable = false, length = 10)
   private Integer userId;

   @Id
   @Column(name = "IDENTIFIER_ID", nullable = false, length = 10)
   private Integer identifierId;

   @Id
   @Column(name = "VALUE", nullable = false, length = 20)
   private String value;

   public String getValue() {
      return value;
   }

   public void setValue(String value) {
      this.value = value;
   }

   public Integer getIdentifierId() {
      return identifierId;
   }

   public void setIdentifierId(Integer identifierId) {
      this.identifierId = identifierId;
   }

   public Integer getUserId() {
      return userId;
   }

   public void setUserId(Integer userId) {
      this.userId = userId;
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 16, 2007 9:24 pm 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
Looks like a bug in Hibernate. As a workaround you may want to try and use the @EmbeddedId strategy for composite keys instead of @IdClass. I can say for sure that @EmbeddedId works as I use it in my projects. I typically define the primarykey class as an inner 'public static' class which keeps all definitions for that one entity nicely together. Setting up the @EmbeddedId structures is slightly more complicated because of the need for @AttributeOverrides annotations. Here is a sample of a two column all PK table (most of the code as generated by the Eclipse Hibernate reverse engineering tools):

Code:
@Entity
@Table(name = "task_owner")
public class TaskOwner
  implements java.io.Serializable {
  private Id id;

  public TaskOwner() {
  }

  @AttributeOverrides(
    {
      @AttributeOverride(name = "taskId", column = @Column(name = "task_id", nullable = false)),
      @AttributeOverride(name = "ownerId", column = @Column(name = "owner_id", nullable = false))
    }
  )
  @EmbeddedId
  public Id getId()   {
    return this.id;
  }

  public void setId(final Id id)  {
    this.id = id;
  }

  @Embeddable
  public static class Id
    implements java.io.Serializable  {
    private long ownerId;
    private long taskId;

    public Id() {
    }

    public Id(final long taskId, final long ownerId)  {
      this.taskId = taskId;
      this.ownerId = ownerId;
    }


    @Column(name = "task_id", nullable = false)
    public long getTaskId() {
      return this.taskId;
    }

    public void setTaskId(final long taskId) {
      this.taskId = taskId;
    }

    @Column(name = "owner_id", nullable = false)
    public long getOwnerId() {
      return this.ownerId;
    }

    public void setOwnerId(final long ownerId) {
      this.ownerId = ownerId;
    }

    @Override
    public boolean equals(final Object other) {
      if ((this == other)) {
        return true;
      }

      if ((other == null)) {
        return false;
      }

      if (!(other instanceof Id)) {
        return false;
      }

      final Id castOther = (Id)other;

      return (this.getTaskId() == castOther.getTaskId()) && (this.getOwnerId() == castOther.getOwnerId());
    }

    @Override
    public int hashCode() {
      int result = 17;

      result = (37 * result) + (int)this.getTaskId();
      result = (37 * result) + (int)this.getOwnerId();
      return result;
    }
  } // end class Id

} // end class TaskOwner


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 17, 2007 5:38 pm 
Beginner
Beginner

Joined: Tue Jul 03, 2007 11:11 am
Posts: 22
I am still getting:
Code:
MySQLSyntaxErrorException: Unknown column 'useridenti0_.identifierId' in 'field list'


I removed the PK class and replace the UserIdentifier class with the attached class,
And refractor all code references from:
UserIdentifier.getUserId()
To
UserIdentifier.getId().getUserId()

Also in the query :
select ui from UserIdentifier ui where ui.id.userId = ?1
instead of
select ui from UserIdentifier ui where ui.userId = ?1



Thank you very much for the code & response,
Sharon


Code:
@Entity
@Table(name = "user_identifier")
public class UserIdentifier implements Serializable {

  private Id id;

  public UserIdentifier() {
  }

  @AttributeOverrides(
    {
      @AttributeOverride(name = "userId", column = @Column(name = "user_id", nullable = false)),
      @AttributeOverride(name = "identifierId", column = @Column(name = "identifier_Id", nullable = false)),
     @AttributeOverride(name = "value", column = @Column(name = "value", nullable = false))
    }
  )
  @EmbeddedId
  public Id getId()   {
    return this.id;
  }

  public void setId(final Id id)  {
    this.id = id;
  }

  @Embeddable
  public static class Id
    implements java.io.Serializable  {
    private long identifierId;
    private long userId;
    private String value;

    public Id() {
    }

    public Id(final long userId, final long identifierId)  {
      this.userId = userId;
      this.identifierId = identifierId;
    }


    @Column(name = "user_id", nullable = false)
    public long getUserId() {
      return this.userId;
    }

    public void setUserId(final long userId) {
      this.userId = userId;
    }

    @Column(name = "identifier_Id", nullable = false)
    public long getIdentifierId() {
      return this.identifierId;
    }

    public void setIdentifierId(final long identifierId) {
      this.identifierId = identifierId;
    }

   @Column(name = "value", nullable = false)
   public String getValue() {
     return value;
   }

   public void setValue(String value) {
    this.value = value;
   }

    @Override
    public boolean equals(final Object other) {
      if ((this == other)) {
        return true;
      }

      if ((other == null)) {
        return false;
      }

      if (!(other instanceof Id)) {
        return false;
      }

      final Id castOther = (Id)other;

      return (this.getUserId() == castOther.getUserId()) &&
           (this.getIdentifierId() == castOther.getIdentifierId() &&
           this.getValue().equals(castOther.getValue()));
    }

    @Override
    public int hashCode() {
      int result = 17;

      result = (37 * result) + (int)this.getUserId();
      result = (37 * result) + (int)this.getIdentifierId();
     result = (37 * result) + this.getValue().hashCode();
      return result;
    }
  } // end class Id

} // end class TaskOwner


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