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: Queries and "column not found" error
PostPosted: Mon Sep 06, 2010 6:34 pm 
Beginner
Beginner

Joined: Thu May 20, 2010 12:31 pm
Posts: 28
I'm really having a hard time getting a DB design to work with Hibernate. I have several kinds of composite keys and some IDs as well, but nearly anything that could be considered exotic. The latest strangeness of Hibernate producing an error on the native query:

Code:
PlayerStat ps = (PlayerStat)em.createNativeQuery("select * from PlayerStats", PlayerStat.class).getResultList().get(0);


Hibernate chokes with an error that appears to be completely unrelated (there's only a multi-column many-to-one association in PlayerStats):

Code:
java.sql.SQLException: Column 'score' not found.
   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
   com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1144)
   com.mysql.jdbc.ResultSetImpl.getBytes(ResultSetImpl.java:1932)
   org.hibernate.type.AbstractBynaryType.get(AbstractBynaryType.java:103)
   org.hibernate.type.SerializableType.get(SerializableType.java:56)
   org.hibernate.type.NullableType.nullSafeGet(NullableType.java:186)
   org.hibernate.type.NullableType.nullSafeGet(NullableType.java:175)
   org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
   org.hibernate.type.ComponentType.hydrate(ComponentType.java:588)
   org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:303)
   org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1142)
...


Somehow Hibernate seems to be expecting a column here, but again the association is multi-column. Regarding this issue I stumbled across: http://docs.jboss.org/hibernate/core/3. ... l#d0e13696

A paragraph here reads:

Quote:
If the entity is mapped with a many-to-one to another entity it is required to also return this when performing the native query, otherwise a database specific "column not found" error will occur. The additional columns will automatically be returned when using the * notation...


Well, this is nice to know, but how do you avoid this error?

Here's the design: http://www.kawoolutions.com/media/hiber ... rstats.jpg
Please have a look at the PlayerStats table being connected to two other tables via a composite key of composite keys. Here's its class:

Code:
@Entity
@Table(name = "PlayerStats")
@IdClass(value = PlayerStatId.class)
public class PlayerStat implements Serializable
{
   @Id
   @Column(name = "game_id", insertable = false, updatable = false)
   private Integer gameId;

   @Id
   @Column(name = "is_home", insertable = false, updatable = false)
   private Boolean isHome;

   @Id
   @Column(name = "player_id", insertable = false, updatable = false)
   private Integer playerId;

   @Id
   @Column(name = "roster_id", insertable = false, updatable = false)
   private Integer rosterId;

   @Column(name = "jersey_nbr")
   private Integer jerseyNbr = null;

   @Column(name = "is_starter")
   private Boolean isStarter = null;

   @Column(name = "pf")
   private Integer pf;

   @ManyToOne
   @PrimaryKeyJoinColumns(value = {@PrimaryKeyJoinColumn(name = "game_id", referencedColumnName = "game_id"), @PrimaryKeyJoinColumn(name = "is_home", referencedColumnName = "is_home")})
   private Score score = null;

   @ManyToOne
   @PrimaryKeyJoinColumns(value = {@PrimaryKeyJoinColumn(name = "player_id", referencedColumnName = "player_id"), @PrimaryKeyJoinColumn(name = "roster_id", referencedColumnName = "roster_id")})
   private TeamMember teamMember = null;

   public PlayerStat()
   {
   }
...
}


I have no idea what Hibernate is doing here. I really need some help on this.

Karsten


Top
 Profile  
 
 Post subject: Re: Queries and "column not found" error
PostPosted: Tue Sep 07, 2010 3:58 pm 
Newbie

Joined: Mon Aug 02, 2010 11:14 pm
Posts: 10
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/tutorial.html

that link has examples on the hibernateutil which I would suggest doing. I remember seeing EntityManager in some jpa stuff I did but haven't worked with it long enough to know how to use it, but hibernate suggests using hibernateUtil and it's mapping xml files. But anywho, I'll get to some suggestions.

Quote:
@ManyToOne
@PrimaryKeyJoinColumns(value = {@PrimaryKeyJoinColumn(name = "game_id", referencedColumnName = "game_id"), @PrimaryKeyJoinColumn(name = "is_home", referencedColumnName = "is_home")})
private Score score = null;


I see you are using field level annotations. This is a bit of performance problem as reflection will be used to find the fields instead of using the getter and setter properties to make method calls, however not as big as people may make it out to be, but where you'll really find an issue is if you have to do validations. But the part that got me was private Score score = null. Not sure if I've seen this.

I would say go ahead and make getter and setters for these properties. Then make the HibernateUtils by reading through that link I gave you. And see how things go. Also if you can put up how your tables look, just the table name and it's foreign key don't need much more than that, I just wanna see what type of a relationship you are trying to forge. ManyToOne I've seen generally getting done by mappedby="..." where "..." is the name of the field that was the onetomany relationship in the parent table object. Let me know what you think and clarify if I've misunderstood something and we can try to work out a solution.

Ramon


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.