Hello,
I'm getting more and more frustrated by Hibernate doing unexplainable things. I get the following exception:
Code:
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not load an entity: [com.kawoolutions.bbstats.model.Team#component[clubId,ordinalNbr,teamtypeCode]{teamtypeCode=mo20, clubId=101, ordinalNbr=1}]
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1235)
org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:631)
org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:585)
com.kawoolutions.bbstats.servlet.BasketballStatsServlet.doGet(BasketballStatsServlet.java:186)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
org.hibernate.exception.GenericJDBCException: could not load an entity: [com.kawoolutions.bbstats.model.Team#component[clubId,ordinalNbr,teamtypeCode]{teamtypeCode=mo20, clubId=101, ordinalNbr=1}]
org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
org.hibernate.loader.Loader.loadEntity(Loader.java:1937)
org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:86)
org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:76)
org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3270)
org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:496)
org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:477)
org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227)
org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:285)
org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
org.hibernate.impl.SessionImpl.get(SessionImpl.java:997)
org.hibernate.impl.SessionImpl.get(SessionImpl.java:990)
org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:610)
org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:585)
com.kawoolutions.bbstats.servlet.BasketballStatsServlet.doGet(BasketballStatsServlet.java:186)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.sql.SQLException: Invalid value for getInt() - 'DE'
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.getInt(ResultSetImpl.java:2788)
com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2813)
org.hibernate.type.IntegerType.get(IntegerType.java:51)
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.type.ManyToOneType.hydrate(ManyToOneType.java:157)
org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2267)
org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1423)
org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1351)
org.hibernate.loader.Loader.getRow(Loader.java:1251)
org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:619)
org.hibernate.loader.Loader.doQuery(Loader.java:745)
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
org.hibernate.loader.Loader.loadEntity(Loader.java:1933)
org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:86)
org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:76)
org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3270)
org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:496)
org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:477)
org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227)
org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:285)
org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
org.hibernate.impl.SessionImpl.get(SessionImpl.java:997)
org.hibernate.impl.SessionImpl.get(SessionImpl.java:990)
org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:610)
org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:585)
com.kawoolutions.bbstats.servlet.BasketballStatsServlet.doGet(BasketballStatsServlet.java:186)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
The SQL for the table is:
Code:
CREATE Teams
(
club_id INTEGER NOT NULL,
teamtype_code CHAR(4) NOT NULL,
ordinal_nbr SMALLINT NOT NULL,
PRIMARY KEY (club_id, teamtype_code, ordinal_nbr),
FOREIGN KEY (club_id) REFERENCES Clubs (id),
FOREIGN KEY (teamtype_code) REFERENCES TeamTypes (code)
);
Here's the entity and composite key class:
Code:
@Entity
@Table(name = "Teams")
public class Team implements Serializable
{
@EmbeddedId
private TeamId id;
@ManyToOne
@JoinColumn(name = "club_id", referencedColumnName = "id", insertable = false, updatable = false)
private Club club = null;
@ManyToOne
@JoinColumn(name = "teamtype_code", referencedColumnName = "code", insertable = false, updatable = false)
private TeamType teamType = null;
@OneToMany(mappedBy = "team")
private Set<Roster> rosters = new HashSet<Roster>();
...
Code:
@Embeddable
public class TeamId implements Serializable
{
@Column(name = "club_id")
private Integer clubId;
@Column(name = "teamtype_code")
private String teamtypeCode;
@Column(name = "ordinal_nbr")
private Integer ordinalNbr;
...
If you look at the stack trace line
Code:
org.hibernate.exception.GenericJDBCException: could not load an entity: [com.kawoolutions.bbstats.model.Team#component[clubId,ordinalNbr,teamtypeCode]{teamtypeCode=mo20, clubId=101, ordinalNbr=1}]
you can see the order of columns is [clubId, ordinalNbr, teamtypeCode] and {teamtypeCode=mo20, clubId=101, ordinalNbr=1}, but the table (I checked the DB x times) and the class are club_id, teamtype_code, ordinalNbr. My suspicion here is that Hibernate scrambles the columns for whatever reason and where expecting club_id (getInt()) it calls that on teamtype_code and excepts... BTW I'm using a simple call to:
Code:
Team team = em.find(Team.class, new TeamId(101, "mo20", 1));
What the hell is Hibernate doing? I have no idea. I'm beginning to believe Hiberate is a poor ORM tool, especially when it comes to mappings and composite primary keys.
Does anyone have an idea what to try to fix this?
Karsten