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#d0e13696A 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.jpgPlease 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